java实现接收form表单传送的文件

时间:2022-12-10 19:55:38

需要的jar包:commons-fileupload-1.2.1.jar、commons-io-2.0.jar

jar包地址:http://download.csdn.net/download/qq_36375934/10243433

<form action="CreateIndex" method="post"  enctype="multipart/form-data">
	<input type="file" name="fileupload">
	<input type="submit" value="提交">
</form>

@WebServlet("/CreateIndex")
public class CreateIndex extends HttpServlet {
	private static final long serialVersionUID = 1L;
	    // 重写doPost方法,处理事件识别请求  
	    protected void doPost(HttpServletRequest request,  
	            HttpServletResponse response) throws ServletException, IOException {  
	    	request.setCharacterEncoding("utf-8");  
	        response.setCharacterEncoding("utf-8");  
	        //1、创建一个DiskFileItemFactory工厂  
	        DiskFileItemFactory factory = new DiskFileItemFactory();  
	        //2、创建一个文件上传解析器  
	        ServletFileUpload upload = new ServletFileUpload(factory);  
	        //解决上传文件名的中文乱码  
	        upload.setHeaderEncoding("UTF-8");   
	        factory.setSizeThreshold(1024 * 500);//设置内存的临界值为500K  
	        File linshi = new File("E:\\linshi");//当超过500K的时候,存到一个临时文件夹中  
	        factory.setRepository(linshi);  
	        upload.setSizeMax(1024 * 1024 * 500);//设置上传的文件总的大小不能超过500M  
	        try {  
	            // 1. 得到 FileItem 的集合 items  
	            List<FileItem> /* FileItem */items = upload.parseRequest(request);  
	  
	            // 2. 遍历 items:  
	            for (FileItem item : items) {  
	                // 若是一个一般的表单域, 打印信息  
	                if (item.isFormField()) {  
	                    String name = item.getFieldName();  
	                    String value = item.getString("utf-8");  
	  
	                    System.out.println(name + ": " + value);  
	                      
	                      
	                }  
	                // 若是文件域则把文件保存到 e:\\files 目录下.  
	                else {  
	                    String fileName = item.getName();  
	                    long sizeInBytes = item.getSize();  
	                    System.out.println(fileName);  
	                    System.out.println(sizeInBytes);  
	  
	                    InputStream in = item.getInputStream();  
	                    byte[] buffer = new byte[1024];  
	                    int len = 0;  
	  
	                    fileName = "e:\\files\\" + fileName;//文件最终上传的位置  
	                    System.out.println(fileName);  
	                    OutputStream out = new FileOutputStream(fileName);  
	  
	                    while ((len = in.read(buffer)) != -1) {  
	                        out.write(buffer, 0, len);  
	                    }  
	  
	                    out.close();  
	                    in.close();  
	                    System.out.println("上传完成");
	                }  
	            }  
	  
	        } catch (FileUploadException e) {  
	            e.printStackTrace();  
	        }  
	    }  

}