Spring MVC上传文件和下载文件
Spring web MVC 框架提供了模型-视图-控制的体系结构和可以用来开发灵活、松散耦合的 web 应用程序的组件。MVC 模式导致了应用程序的不同方面(输入逻辑、业务逻辑和 UI 逻辑)的分离,同时提供了在这些元素之间的松散耦合。
上传文件
- 首先在
jsp
中创建表格
<form action="/upload.do" method="post" enctype="multipart/form-data">
<%--上传文件--%>
请选择文件<input type="file" name="file"><br>
请选择文件<input type="file" name="file"><br>
<button>提交</button>
</form>
- 在
springmvc.xml
中注入文件流
<!--注入文件流-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<--上传文件大小-->
<property name="maxUploadSize" value="10000000"></property>
</bean>
- 创建
UploadController
控制器
package cn.zbw.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
@Controller
public class UploadController {
@RequestMapping("/upload.do")
public String upload(MultipartFile file[], HttpServletRequest request) throws IOException {
for (int i = 0; i < file.length; i++) {
// 上传路径
String realPath = request.getServletContext().getRealPath("/upload/");
// 获取文件名字
String name = file[i].getOriginalFilename();
// 修改文件名字
String newName = new Date().getTime() + new Random().nextInt(9999999) + name;
File file1 = new File(realPath + newName);
file[i].transferTo(file1);
}
return "success";
}
}
- 上传成功返回到
success
页面
<%--
Created by IntelliJ IDEA.
User: 冰冰赵博文
Date: 2021/03/18
Time: 上午 11:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功页面</title>
</head>
<body>
ok成功上传
</body>
</html>
结果是:
上传成功并且对文件重命名:
下载文件
- 创建
jsp
页面
<%--下载文档与图片 --%>
<a href="/download/考教分离系统.doc">下载文档</a>
<a href="download.do?fileName=冰冰1.jpg">下载图片</a>
这里需要注意一点,下载图片时不能直接写路径否则就是查看了图片,并没有进行下载,所以需要先找到它再进行下载。
- 创建
DownloadController
控制器
在进行下载的时候要注意的一点就是在转流之前,要进行转格式要把
UTF-8转换为iso-8859-1
,这是要记得的一点。
package cn.zbw.controller;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@Controller
public class DownloadController {
@RequestMapping("/download.do")
public ResponseEntity<byte[]> download(@RequestParam("fileName") String fileName, HttpServletRequest request) throws IOException {
// 获取下载路径
String realPath = request.getServletContext().getRealPath("/download/");
File file = new File(realPath + fileName);
// 转格式
String newName=new String(fileName.getBytes("UTF-8"), "iso-8859-1");
// 转流
HttpHeaders hh = new HttpHeaders();
hh.setContentDispositionFormData("attachment",newName);
hh.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),hh, HttpStatus.CREATED);
}
}
结果是:
成功下载