文件下载:
通过ResponseEntity实现
@RequestMapping("/testDown")
public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException {
//获取ServletContext对象
ServletContext servletContext = session.getServletContext();
//获取服务器中文件的真实路径
String realPath = servletContext.getRealPath("/static/img/panda.jpg");
//创建输入流
InputStream is = new FileInputStream(realPath);
//创建字节数组
byte[] bytes = new byte[is.available()];
//将流读到字节数组中
is.read(bytes);
//创建HttpHeaders对象设置响应头信息
MultiValueMap<String, String> headers = new HttpHeaders();
//设置要下载方式以及下载文件的名字
headers.add("Content-Disposition", "attachment;filename=panda.jpg");
//设置响应状态码
HttpStatus statusCode = HttpStatus.OK;
//创建ResponseEntity对象
ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers, statusCode);
//关闭输入流
is.close();
return responseEntity;
}
使用时只需更改文件所在路径即可String realPath = servletContext.getRealPath("/static/img/panda.jpg");
图片放在webapp中的img文件夹下
文件上传:
首先导入依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
然后再mvc.xml文件中配置,id一定要是multipartResolver
<bean id="MultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> </bean> CommonsMultipartResolver
然后写前端和后端
表单配置时一定要有enctype="multipart/form-data
<form action="shangchuan" enctype="multipart/form-data" method="post"> <input type="file" name="wenjian"> <input type="submit" value="提交"> </form>
后端参数列表中的MultipartFile wenjian一定要对应前端的name
@RequestMapping(value = "/shangchuan")
public String test(MultipartFile wenjian,HttpSession session) throws IOException {
// 拿到文件名Filename
String Filename = wenjian.getOriginalFilename();
// 获得文件的真实路径photoPath
ServletContext servletContext = session.getServletContext();
String photoPath = servletContext.getRealPath("wenjian");
// 创建photoPath的file对象
File file = new File("photoPath");
// 判断文件所对应的目录是否存在
if (!file.exists()){
file.mkdir();
}
// 最终路径为真实路径+文件名
String finalpath=photoPath+File.pathSeparator+Filename;
// 上传文件
wenjian.transferTo(new File(finalpath));
return "success";
}
中间创建photoPath的file对象那一块相当于加了一个判断文件目录在不在
解决文件重名覆盖的问题:
加一段代码。截取.之后的,随机生成前面的进行拼接,就不会重复
String houzhui = Filename.substring(Filename.lastIndexOf("."));
String qianmian = UUID.randomUUID().toString();
Filename=houzhui+qianmian;
uuid:唯一标识码,一串码碰撞概率很低。
总代码:
@RequestMapping(value = "/shangchuan")
public String test(MultipartFile wenjian,HttpSession session) throws IOException {
// 拿到文件名Filename
String Filename = wenjian.getOriginalFilename();
// 解决文件名重复
String houzhui = Filename.substring(Filename.lastIndexOf("."));
String qianmian = UUID.randomUUID().toString();
Filename=houzhui+qianmian;
// 获得文件的真实路径photoPath
ServletContext servletContext = session.getServletContext();
String photoPath = servletContext.getRealPath("wenjian");
// 创建photoPath的file对象
File file = new File("photoPath");
// 判断文件所对应的目录是否存在
if (!file.exists()){
file.mkdir();
}
// 最终路径为真实路径+文件名
String finalpath=photoPath+File.pathSeparator+Filename;
// 上传文件
wenjian.transferTo(new File(finalpath));
return "success";
}
该文详细介绍了如何使用SpringMVC的ResponseEntity处理文件下载,包括设置响应头和关闭输入流。同时,文章也阐述了文件上传的步骤,包括依赖配置、前端表单设置和后端处理,以及如何解决文件重名覆盖问题,通过生成UUID作为文件名前缀确保唯一性。
1361

被折叠的 条评论
为什么被折叠?



