16. 文件上传和下载
1、导入上传下载所需jar文件
<!-- 文件的上传和下载 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
2、在springmvc.xml中配置文件解析器
<!-- 文件解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="10485760"></property>
</bean>
3、编写service业务类
控制器类
@RequestParam(“fileName”) CommonsMultipartFile file接收文件
file.getOriginalFilename();获得原始文件名
file.getContentType();获得文件类型
file.getInputStream();获得输入流对象
文件下载
设置响应内容
response.setContentType("applicatoin/octet-stream");代表任意的二进制数据
response.setHeader(“content-disposition", "attachment;filename="+
new String(filename.getBytes("utf-8"),“ISO-8859-1"));
Content-disposition,attachment; filename= :告诉浏览器下载文件的名称,弹出文件下载框
filename:文件名称,如果为中文,则会出现乱码。
使用fileName = new String(fileName.getBytes(), "ISO8859-1")
通过response对象获取OutputStream对象
response.getOutputStream();
示例:
package com.company.springmvc.service;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@Controller
@RequestMapping("/fileManage")
public class FileManage {
@RequestMapping("/toFileUpload")
public ModelAndView toFileUpload(){
ModelAndView mv = new ModelAndView("file/fileUpload");
return mv;
}
@RequestMapping("/fileUpload")
public ModelAndView fileUpload(@RequestParam("fileupload") CommonsMultipartFile file){
String path = "";
String filename = "";
try {
filename = file.getOriginalFilename();
path = "D:\\Program Files\\apache-tomcat-8.5.53\\webapps\\userFile\\"+filename;
File f = new File(path);
file.transferTo(f);
} catch (Exception e) {
e.printStackTrace();
}
ModelAndView mv = new ModelAndView("file/uploadOver");
mv.addObject("imgPath", filename);
return mv;
}
@RequestMapping("/downLoad")
public ModelAndView downLoad(String fileName , HttpServletResponse response){
String path = "D:\\Program Files\\apache-tomcat-8.5.53\\webapps\\userFile\\" + fileName;
try {
FileInputStream in = new FileInputStream(path);
//下载文件响应设置
//以二进制字符流响应
response.setContentType("application/octet-stream");
//调用浏览器下载器,选择文件存放位置
response.setHeader("content-disposition","filename="+ new String( "我的图像.jpg".getBytes("utf-8"),"ISO-8859-1"));
OutputStream out = response.getOutputStream();
byte [] b = new byte[1024];
int length = 0;
while ((length = in.read(b)) != -1){
out.write(b, 0, length);
}
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
ModelAndView mv = new ModelAndView("file/uploadOver");
mv.addObject("imgPath", fileName);
return mv;
}
}
3、编写jsp
表单:
method="post"
enctype="multipart/form-data"
fileUpload.jsp
<%--
Created by IntelliJ IDEA.
User: God_86
Date: 2020/6/30
Time: 10:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<h1>文件上传</h1>
<form action="${path}/fileManage/fileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="fileupload">
<input type="submit" value="上传">
</form>
</body>
</html>
uploadOver.jsp
<%--
Created by IntelliJ IDEA.
User: God_86
Date: 2020/6/30
Time: 10:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传完成</title>
</head>
<body>
<h1>文件上传完成</h1>
<a href="${path}/fileManage/downLoad?fileName=${imgPath}">
<img src="http://127.0.0.1:8888/userFile/${imgPath}">
</a>
</body>
</html>