一、文件上传于下载的宗旨
文件上传于下载的宗旨:文件从哪里(源头)来放哪里去(目标位置)
二、文件上传
1.单文件上传
首先导入依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
在springMVC核心配置文件中配置文件上传解析器(CommonsMultipartResolver)
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 必须和用户JSP 的pageEncoding属性一致,以便正确解析表单的内容 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 文件最大大小(字节) 1024*1024*50=50M-->
<property name="maxUploadSize" value="52428800"></property>
<!--resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常-->
<property name="resolveLazily" value="true"/>
</bean>
后台Controller
private static final String DEAULT_PATH="/uploads/";
/**
* 文件上传
* @return
*/
@RequestMapping("/upload")
public String upload(MultipartFile bfile,HttpServletRequest request){
try{
//获取文件名
String Filename = bfile.getOriginalFilename();
//获取图片路径
String path=DEAULT_PATH+Filename;
//获取保存图片的绝地路径
String filePath = transfor(path, request);
//将上传的图片保存到指定位置(读流写流)
bfile.transferTo(new File(filePath));
}catch (Exception e){
e.printStackTrace();
}
return "redirect:/book/queryBookPage";
}
/**
* 将相对路径转为绝对路径
* @param relativePath
* @return
*/
private String transfor(String relativePath, HttpServletRequest request){
return request.getServletContext().getRealPath(relativePath);
}
前端form表单
<form action="${path}/bookFile/upload" method="post" enctype="multipart/form-data">
<label>书本编号:</label><input type="text" name="bookId" value="${param.bookId}" readonly="readonly"><br>
<label>书本图片1:</label><input type="file" name="bfile" value=""><br>
<input type="submit" value="上传">
</form
可以去项目地址的target/ssm/uploads路径下查看图片是否上传成功
2.多文件上传
多文件上传,只需要将页面修改为多个文件上传项,将方法参数MultipartFile类型修改为MultipartFile[]即可
前端: 加上multiple就可以实现多文件选项
<input type="file" id="upload" name="file" multiple/> <br />
后端:
/**
* 文件上传
* @return
*/
@RequestMapping("/upload")
public String upload(MultipartFile[] bfile,HttpServletRequest request){
try{
for (MultipartFile bfile : bfiles) {
//获取文件名
String Filename = bfile.getOriginalFilename();
//获取图片路径
String path=DEAULT_PATH+Filename;
//获取保存图片的绝地路径
String filePath = transfor(path, request);
//将上传的图片保存到指定位置(读流写流)
bfile.transferTo(new File(filePath));
}
}catch (Exception e){
e.printStackTrace();
}
return "redirect:/book/queryBookPage";
}
文件路径
三、文件下载
@RequestMapping(value="/download")
public ResponseEntity<byte[]> download(@RequestParam String fileId, HttpServletRequest request){
try{
//先根据文件id查询对应图片信息
BookFile bookFile = bookFileService.selectByPrimaryKey(fileId);
//数据库中图片的相对路径转换成绝对路径
String transfor = transfor(bookFile.getUrl(), request);
//下载关键代码
File file=new File(transfor);
//http头信息
HttpHeaders headers = new HttpHeaders();
String downloadFileName = new String(bookFile.getRealName().getBytes("UTF-8"),"iso-8859-1");//设置编码
headers.setContentDispositionFormData("attachment", downloadFileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//MediaType:互联网媒介类型 contentType:具体请求中的媒体类型信息
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
本文参考链接:https://blog.csdn.net/weixin_57504000/article/details/124027677