JavaWeb文件上传下载(基于commons-fileupload-1.2.1)
文件的上传和下载
简介
文件的上传和下载, 是非常常见的功能。 很多的系统中, 或者软件中都经常使用文件的上传和下载。
比如:QQ 头像,就使用了上传。
邮箱中也有附件的上传和下载功能。
OA
文件上传介绍
基本步骤
- 要有一个 form 标签, method=post 请求
- form 标签的 encType 属性值必须为 multipart/form-data 值
- 在 form 标签中使用 input type=file 添加上传的文件
- 编写服务器代码(Servlet 程序) 接收, 处理上传的数据。
encType=multipart/form-data
表示提交的数据,以多段(每一个表单项一个数据段) 的形式进行拼接, 然后以二进制流的形式发送给服务器
文件上传, HTTP 协议的说明
commons-fileupload.jar 常用 API 介绍说明
注意:
commons-fileupload.jar 需要依赖 commons-io.jar 这个包, 所以两个包我们都要引入。
commons-fileupload.jar 和 commons-io.jar 包中, 我们常用的类介绍:
ServletFileUpload 类, 用于解析上传的数据。
FileItem 类, 表示每一个表单项。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| boolean ServletFileUpload.isMultipartContent(HttpServletRequest request);
public List<FileItem> parseRequest(HttpServletRequest request)
boolean FileItem.isFormField()
String FileItem.getFieldName()
String FileItem.getName();
void FileItem.write( file );
|
代码示例:
上传文件的表单:
1 2 3 4 5 6
| <form action="http://192.168.31.74:8080/09_EL_JSTL/uploadServlet" method="post" enctype="multipart/form-data"> 用户名: <input type="text" name="username" /> <br> 头像: <input type="file" name="photo" > <br> <input type="submit" value="上传"> </form>
|
解析上传的数据的代码:
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
|
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (ServletFileUpload.isMultipartContent(req)) {
FileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory); try {
List<FileItem> list = servletFileUpload.parseRequest(req);
for (FileItem fileItem : list) { if (fileItem.isFormField()) {
System.out.println("表单项的 name 属性值: " + fileItem.getFieldName());
System.out.println("表单项的 value 属性值: " + fileItem.getString("UTF-8")); } else {
System.out.println("表单项的 name 属性值: " + fileItem.getFieldName()); System.out.println("上传的文件名: " + fileItem.getName()); fileItem.write(new File("e:\\" + fileItem.getName())); } } } catch (Exception e) { e.printStackTrace(); } } }
|
文件下载
下载的常用 API 说明:
1 2 3 4 5
| response.getOutputStream(); servletContext.getResourceAsStream(); servletContext.getMimeType(); response.setContentType(); response.setHeader("Content-Disposition", "attachment; fileName=1.jpg");
|
这个响应头告诉浏览器。这是需要下载的。而 attachment 表示附件,也就是下载的一个文件。 fileName=后面,
表示下载的文件名。
完成上面的两个步骤,下载文件是没问题了。 但是如果我们要下载的文件是中文名的话。你会发现,下载无法正确显示出正确的中文名。
原因是在响应头中,不能包含有中文字符,只能包含 ASCII 码。
附件中文名乱码问题解决方案:
方案一: URLEncoder 解决 IE 和谷歌浏览器的 附件中文名问题。
如果客户端浏览器是 IE 浏览器 或者 是谷歌浏览器。 我们需要使用URLEncoder 类先对中文名进行 UTF-8 的编码操作。
因为 IE 浏览器和谷歌浏览器收到含有编码后的字符串后会以 UTF-8 字符集进行解码显示。
1 2 3 4
| String str = "attachment; fileName=" + URLEncoder.encode("中文.jpg", "UTF-8");
response.setHeader("Content-Disposition", str);
|
方案二: BASE64 编解码 解决 火狐浏览器的附件中文名问题
如果客户端浏览器是火狐浏览器。 那么我们需要对中文名进行 BASE64 的编码操作。
这时候需要把请求头 Content-Disposition: attachment; filename=中文名
编码成为: Content-Disposition: attachment; filename==?charset?B?xxxxx?=
=?charset?B?xxxxx?= 现在我们对这段内容进行一下说明。
=?
charset
Bx
xxx
?=
BASE64 编解码操作:
因为火狐使用的是 BASE64 的编解码方式还原响应中的汉字。 所以需要使用 BASE64Encoder 类进行编码操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| String str = "attachment; fileName=" + "=?utf-8?B?" + new BASE64Encoder().encode("中文.jpg".getBytes("utf-8")) + "?=";
response.setHeader("Content-Disposition", str); 那么我们如何解决上面两种不同编解码方式呢。 我们只需要通过判断请求头中 User-Agent 这个请求头携带过来的 浏览器信息即可判断出是什么浏览器。 如下: String ua = request.getHeader("User-Agent");
if (ua.contains("Firefox")) {
String str = "attachment; fileName=" + "=?utf-8?B?" + new BASE64Encoder().encode("中文.jpg".getBytes("utf-8")) + "?=";
response.setHeader("Content-Disposition", str); } else {
String str = "attachment; fileName=" + URLEncoder.encode("中文.jpg", "UTF-8");
response.setHeader("Content-Disposition", str); }
|
@Write By Jerry Zhang