一、文件上传的原理分析
1、文件上传的必要前提
a、表单的method必须是post
b、表单的enctype属性必须是multipart/form-data类型的。
enctype默认值:application/x-www-form-urlencoded
作用:告知服务器,请求正文的MIME类型
application/x-www-form-urlencoded : username=abc&password=123
ServletRequest.getParameter(String name);该方法是专门读取该类型的方法
multipart/form-data:
2、借助commons-fileupload组件实现文件的上传
a、拷贝jar包:commons-fileupload.jar commons-io.jar
b、实现原理
3、乱码问题
a、普通字段的乱码
FileItem.getString(String charset);编码要和客户端一致
b、上传的中文文件名乱码
request.setCharacterEncoding("UTF-8");编码要和客户端一致
4、具体实现
前台upload.jsp代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<%@ page language= "java" import= "java.util.*" pageEncoding= "UTF-8" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > <html> <head> <title>文件上传</title> <meta http-equiv= "pragma" content= "no-cache" > <meta http-equiv= "cache-control" content= "no-cache" > <meta http-equiv= "expires" content= "0" > <!-- <link rel= "stylesheet" type= "text/css" href= "styles.css" > --> </head> <body> <form action= "${pageContext.request.contextPath}/servlet/UploadServlet3" method= "post" enctype= "multipart/form-data" > name:<input name= "name" /><br/> file1:<input type= "file" name= "f1" /><br/> file2:<input type= "file" name= "f2" /><br/> <input type= "submit" value= "上传" > </form> </body> </html> |
后台servlet代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package com.itheima.servlet; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.List; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.io.FilenameUtils; //详解 public class UploadServlet3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding( "UTF-8" ); response.setContentType( "text/html;charset=UTF-8" ); PrintWriter out = response.getWriter(); System. out .print(request.getRemoteAddr()); boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (!isMultipart){ throw new RuntimeException( "请检查您的表单的enctype属性,确定是multipart/form-data" ); } DiskFileItemFactory dfif = new DiskFileItemFactory(); ServletFileUpload parser = new ServletFileUpload(dfif); // parser.setFileSizeMax(3*1024*1024);//设置单个文件上传的大小 // parser.setSizeMax(6*1024*1024);//多文件上传时总大小限制 List<FileItem> items = null ; try { items = parser.parseRequest(request); } catch (FileUploadBase.FileSizeLimitExceededException e) { out .write( "上传文件超出了3M" ); return ; } catch (FileUploadBase.SizeLimitExceededException e){ out .write( "总文件超出了6M" ); return ; } catch (FileUploadException e) { e.printStackTrace(); throw new RuntimeException( "解析上传内容失败,请重新试一下" ); } //处理请求内容 if (items!= null ){ for (FileItem item:items){ if (item.isFormField()){ processFormField(item); } else { processUploadField(item); } } } out .write( "上传成功!" ); } private void processUploadField(FileItem item) { try { String fileName = item.getName(); //用户没有选择上传文件时 if (fileName!= null &&!fileName.equals( "" )){ fileName = UUID.randomUUID().toString()+ "_" +FilenameUtils.getName(fileName); //扩展名 String extension = FilenameUtils.getExtension(fileName); //MIME类型 String contentType = item.getContentType(); if (contentType.startsWith( "image/" )){ //分目录存储:日期解决 // Date now = new Date(); // DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); // // String childDirectory = df.format(now); //按照文件名的hashCode计算存储目录 String childDirectory = makeChildDirectory(getServletContext().getRealPath( "/WEB-INF/files/" ),fileName); String storeDirectoryPath = getServletContext().getRealPath( "/WEB-INF/files/" +childDirectory); File storeDirectory = new File(storeDirectoryPath); if (!storeDirectory.exists()){ storeDirectory.mkdirs(); } System. out .println(fileName); item.write( new File(storeDirectoryPath+File.separator+fileName)); //删除临时文件 } } } catch (Exception e) { throw new RuntimeException( "上传失败,请重试" ); } } //计算存放的子目录 private String makeChildDirectory(String realPath, String fileName) { int hashCode = fileName.hashCode(); int dir1 = hashCode&0xf; // 取1~4位 int dir2 = (hashCode&0xf0)>>4; //取5~8位 String directory = "" +dir1+File.separator+dir2; File file = new File(realPath,directory); if (!file.exists()) file.mkdirs(); return directory; } private void processFormField(FileItem item) { String fieldName = item.getFieldName(); //字段名 String fieldValue; try { fieldValue = item.getString( "UTF-8" ); } catch (UnsupportedEncodingException e) { throw new RuntimeException( "不支持UTF-8编码" ); } System. out .println(fieldName+ "=" +fieldValue); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
5、关于临时文件问题
a、DiskFileItemFactory
public void setRepository(File repository):设置临时文件的存放目录
public void setSizeThreshold(int sizeThreshold):设置缓存的大小
b、
文件上传时,自己用IO流处理,一定要在流关闭后删除临时文件。FileItem.delete()
建议使用:FileItem.writer(File f).会自动删除临时文件。
6、限制文件的大小
a、
ServletFileUpload.setFileSizeMax(3*1024*1024);//设置单个文件上传的大小
b、
ServletFileUpload.setSizeMax(6*1024*1024);//多文件上传时总大小限制
以上所述是小编给大家介绍的commons fileupload实现文件上传的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/lls1413/archive/2016/10/14/upload_file.html