通俗易懂的Servlet3.0文件上传

1、文件上传

       三点注意:

        前台
        1.请求方式post
        2.enctype="multipart/form-data"
        后台
        3.一定要加上注解   @MultipartConfig

前台页面:

用户名:
头像:

后台页面:

   三步搞定:
        1、得到Part文件对象,request.getPart(String name);  name对应前台文件输入框的name属性的值
        2、得到文件上传真实路径  path
        3、输出文件  part.write(path)

@WebServlet("/upload")
@MultipartConfig  //支持文件上传
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uname = request.getParameter("uname");
		System.out.println("用户名:" + uname);
		// 1、得到 文件 part 对象
		Part part = request.getPart("imageFile");
		
		// 得到上传文件名称
		String submittedFileName = part.getSubmittedFileName();
		System.out.println(submittedFileName);
		// 2、得到文件上传路径
		String realPath = request.getServletContext().getRealPath("/upload/" + submittedFileName);
		// 3、输出文件
		part.write(realPath);
	}

}

此时中文会出现乱码,创建一个过滤器,用来解决乱码问题,可以通用

package com.mage.upload;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

/**
 * GET:
	请求:
	   	Tomcat8及以上,不需要处理
	   	Tomcat7及以下,需要处理乱码
	   	new String(request.getParameter(name).getBytes("ISO-8859-1"),"UTF-8");
	响应:
	   	response.setContentType("text/html;charset=UTF-8");
	   
	POST:
		请求:
	        Tomcat8及以上,需要处理乱码,request.setCharacterEncoding("UTF-8");
			Tomcat7及以下,需要处理乱码,request.setCharacterEncoding("UTF-8");
		响应:
		   	response.setContentType("text/html;charset=UTF-8");
		   	
 
 */
@WebFilter("*")
public class EncodeFilter implements Filter {


	public void destroy() {
	}


	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		// 基于Http
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)resp;
		
		// 设置POST请求编码以及响应编码
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		// 获取服务器版本
		String serverInfo = request.getServletContext().getServerInfo();
		// Apache Tomcat/8.0.52
		System.out.println(serverInfo);
		
		// 得到版本号  **左闭右开**
		String info = serverInfo.substring(serverInfo.indexOf("/")+1,serverInfo.indexOf("/")+2);
		
		// 判断Tomcat版本是否是7及以下
		if(info != null && Integer.parseInt(info) <= 7){
			// 判断请求方式
			String method = request.getMethod();
			System.out.println(method);
			if("get".equalsIgnoreCase(method)){
				// 是get请求
				// 	   	Tomcat7及以下,需要处理乱码
			   	//        new String(request.getParameter(name).getBytes("ISO-8859-1"),"UTF-8");
				// 无法得到用户的参数,所以定义一个内部类MyWrapper,继承HttpServletRequestWrapper,重写getParameter方法
				//new String(request.getParameter(name).getBytes("ISO-8859-1"),"UTF-8");
				// myWrapper其实就是重写了getParameter方法的request对象,而重写后的方法里解决了乱码问题,所以用户调用的时候不会乱码
				HttpServletRequest myWrapper = new MyWrapper(request);
				// 放行
				chain.doFilter(myWrapper, response);
				return;
			}
		}

		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

	/**
	 * 定义内部类MyWrapper继承HttpServletRequestWrapper
	 *    目的:重写getParameter方法
	 * @author Cushier
	 *
	 */
	class MyWrapper extends HttpServletRequestWrapper{

		HttpServletRequest request;
		
		public MyWrapper(HttpServletRequest request) {
			super(request);
			this.request = request;
		}

		@Override
		public String getParameter(String name) {
			String value = null;
			try {
				if(request.getParameter(name)!=null){
					value = new String(request.getParameter(name).getBytes("ISO-8859-1"),"UTF-8");
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return value;
		}
		
	}
}

 

你可能感兴趣的:(Servlet3.0上传文件)