【SpringMVC】文件上传和下载
程序员文章站
2022-06-02 15:07:51
...
本文目录
文件上传
第一步:创建SpringMVC工程
参考 【SpringMVC】SpringMVC入门实例创建一个SpringMVC工程
创建好的工程目录如下:
在pom.xml
文件中添加文件上传相关的依赖包
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
第二步:添加页面表单信息
修改login.jsp
如***意表单的enctype必须是multipart/form-data
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/index/add" method="post" enctype="multipart/form-data">
<div>
名字:<input name="name" >
</div>
<div>
年龄:<input name="age" >
</div>
<tr>
<td>用户头像</td>
<td><input type="file" name="userimg"></td>
</tr>
<div>
<input type="submit" value="添加用户">
</div>
</form>
</body>
</html>
第三步:Controller中接收数据
新建LoginControl
@Controller
@RequestMapping("/index")
public class LoginControl {
@RequestMapping(value = "/login")
public String login(){
return "login";
}
@RequestMapping(value = "/add")
public String add(String username, String password
, MultipartFile userimg)throws Exception, IOException {
userimg.transferTo(new File("d:/imgs/","test.png"));
return "success";
}
}
在Spring的配置文件中添加bean
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
id="multipartResolver" >
<!-- 设置上传文件信息参数 -->
<!-- 设置文件上传的最大尺寸 -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
访问请求地址 添加文件即可
文件下载
方法1:基于ResponseEntity实现
在LoginControl
添加download
方法
@RequestMapping(value ="/download/{fileName}/{fileType}", method = RequestMethod.GET)
public ResponseEntity<byte[]> download(HttpServletRequest request,
@PathVariable String fileName,@PathVariable String fileType) throws IOException {
// 需要下载的文件
File file = new File("D:/imgs/"+fileName+"." + fileType);
byte[] body = null;
InputStream is = new FileInputStream(file);
body = new byte[is.available()];
is.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attchement;filename=" + file.getName());
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
return entity;
}
访问请求地址 ,成功下载文件
方式2:Java通用下载实现
在LoginControl
添加download1
方法
@RequestMapping(value ="/download1/{fileName}/{fileType}", method = RequestMethod.GET)
public void download1(HttpServletRequest request, HttpServletResponse response
, @PathVariable String fileName,@PathVariable String fileType) throws IOException{
File file = new File("D:/imgs/"+fileName+"." + fileType);
//设置响应头和客户端保存文件名
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName=" + file.getName());
try {
//打开本地文件流
InputStream inputStream = new FileInputStream(file);
//**下载操作
OutputStream os = response.getOutputStream();
//循环写入输出流
byte[] b = new byte[2048];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
// 这里主要关闭。
os.close();
inputStream.close();
} catch (Exception e){
throw e;
}
}