springmvc 文件下载上传
文件上传
- 引入文件上传相关依赖
<!--commons-fileupload-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
-
webapp目录创建upload文件夹
upload文件夹会部署到服务器上
-建议upload文件夹中创建一个空的init.txt文件,idea中不会把空目录部署到服务器上 -
创建form表单
<form action="" method="post" enctype="multipart/form-data" >
<input type="file" name="upFile">
<input type="submit" value="提交">
</form>
注意:
- form 的method提交方式必须为 “post”
- enctype为 “multipart/form-data”
- input标签中name属性值与Controller中文件参数名一致
- 开发Controller------upload
@RequestMapping("upload")
@RequestMapping("file")
public String upload(MultipartFile upFile, HttpServletRequest request) throws IOException {
//上传文件
//根据upload相对路径获取部署到服务之后绝对路径
String realPath=request.getSession().getServletContext().getRealPath("/upload");
//获取文件名称
String fileName=upFile.getOriginalFilename();
//将文件上传到upload对应路径
upFile.transferTo(new File(realPath, fileName));
return "show";
}
- 在springmvc.xml配置文件中加入文件上传解析器配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
注意:
- id的值固定写死 multipartResolver
文件上传优化(修改上传文件名,创建日期文件夹存储)
@RequestMapping("upload")
public String upload(MultipartFile upFile, HttpServletRequest request) throws IOException {
//上传文件
//根据upload相对路径获取部署到服务之后绝对路径
String realPath=request.getSession().getServletContext().getRealPath("/upload");
System.out.println(realPath);
//修改文件原始名称
String extension = FilenameUtils.getExtension(upFile.getOriginalFilename());
//设置UUID为文件名,replace(替换)"-",
String newFileName= UUID.randomUUID().toString().replace("-","")+"."+extension;
//修改成当天日期目录
LocalDate now=LocalDate.now();
File dateDir=new File(realPath,now.toString());
//当天日期目录文件夹不存在则创建
if(!dateDir.exists()) dateDir.mkdir();
//将文件上传到upload对应的日期目录中
upFile.transferTo(new File(dateDir,newFileName));
return "show";
}
文件下载
-
将需要下载的文件放入指定下载目录中
如在wabapp中创建一个新的文件夹down -
在页面中提供一个文件下载链接
<a href="${pageContext.request.contextPath}/file/downLoad?fileName=123.jpg">123.jpg</a>
- 开发Controller----downLoad
@RequestMapping("downLoad")
public void downLoad(String fileName,HttpServletResponse response,HttpServletRequest request) throws IOException {
//根据下载相对目录获取下载目录在服务器部署之后的绝对路径
String realPath=request.getSession().getServletContext().getRealPath("/down");
//通过文件输入流读取文件
FileInputStream is=new FileInputStream(new File(realPath,fileName));
//获取响应输出流
//如果下载txt文件出现乱码
//设置 response.setContentType("text/plain;charset=UTF-8");
//设置setHeader响应类型 attachment(附件类型) inline(在线打开)默认
response.setHeader("content-disposition","attachment;fileName"+fileName);
//处理输出流复制
ServletOutputStream os = response.getOutputStream();
//文件拷贝 len用来判断什么时候读完文件
int len;
byte[] b=new byte[1024];
while (true){
len=is.read(b);
if(len==-1)break;
os.write(b,0,len);
}
//释放资源
is.close();
os.close();
return "show";
}
文件下载优化
1.附件下载/在线打开
<a href="${pageContext.request.contextPath}/file/downLoad?fileName=123.jpg">在线打开</a>
<a href="${pageContext.request.contextPath}/file/downLoad?fileName=123.jpg&openStyle=attachment">附件下载</a>
- 开发Collection
使用IOUtils工具类
处理中文文件名
@RequestMapping("downLoad")
public void downLoad(String openStyle,String fileName,HttpServletResponse response,HttpServletRequest request) throws IOException {
//判断下载方式 attachment(附件类型) inline(在线打开)
openStyle= (openStyle==null) ? "inline":"attachment";
//根据下载相对目录获取下载目录在服务器部署之后的绝对路径
String realPath=request.getSession().getServletContext().getRealPath("/down");
//通过文件输入流读取文件
FileInputStream is=new FileInputStream(new File(realPath,fileName));
//设置setHeader响应类型 attachment(附件) inline(在线打开)默认
//如果fileName文件名含有中文 对fileName进行编码 URLEncoder.encode(fileName,"UTF-8")
response.setHeader("content-disposition",openStyle+";fileName"+fileName);
//处理输出流复制
ServletOutputStream os = response.getOutputStream();
//处理下载流复制
IOUtils.copy(is,os);
//关闭流
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}