MultipartResolver 用于处理文件上传。
当收到请求时,DispatcherServlet 的 checkMultipart() 方法会调用 MultipartResolver 的 isMultipart() 方法判断请求中是否包含文件。
如果请求数据中包含文件,则调用 MultipartResolver 的 resolveMultipart() 方法对请求的数据进行解析,
然后将文件数据解析成 MultipartFile 并封装在 MultipartHttpServletRequest (继承了 HttpServletRequest) 对象中,最后传递给 Controller
MultipartResolver 是一个接口,它的实现类分为 CommonsMultipartResolver 类和 StandardServletMultipartResolver 类。
MultipartFile 封装了请求数据中的文件,此时这个文件存储在内存中或临时的磁盘文件中,
需要将其转存到一个合适的位置,因为请求结束后临时存储将被清空。
在 MultipartFile 接口中有如下方法:
String getName(); // 获取参数的名称
String getOriginalFilename(); // 获取文件的原名称
String getContentType(); // 文件内容的类型
boolean isEmpty(); // 文件是否为空
long getSize(); // 文件大小
byte[] getBytes(); // 将文件内容以字节数组的形式返回
InputStream getInputStream(); // 将文件内容以输入流的形式返回
void transferTo(File dest); // 将文件内容传输到指定文件中
spring 文件上传
1、用第三方,添加依赖Jar包
commons-fileupload.jar
commons-io.jar
2、springmvc配置,CommonsMultipartResolver对象,
CommonsMultipartResolver它是专门处理文件上传的一个类
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设定文件上传的最大值-->
<property name="maxUploadSize" value="1024000"></property>
<!-- 缓存大小,设定文件上传时写入内存的最大值,如果小于这个参数不会生成临时文件,默认为10240 -->
<property name="maxInMemorySize" value="4096"></property>
<!-- 设置编码格式-->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 上传文件的临时路径 -->
<property name="uploadTempDir" value="fileUpload/temp"></property>
<!-- 延迟文件解析 -->
<property name="resolveLazily" value="true"/>
</bean>
3、展示页面。form表单,设置enctype=“multipart/form-data” 。 设置后,后台通过getprameter无法获取到参数
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
<p><input type="file" name="files"></p>
<input type="submit" value="上传">
</form>
4、后台,获取文件名(参数匹配和不匹配)设置保存的地址。最后上传
获取文件名:getOriginalFilename(),通过参数名去调用该方法
String fileName=files.getOriginalFilename();
如果参数名不匹配,可通过注解@RequestParam("***")设置,如果是restful风格,就需要注解@PathVariable(“参数名”),路径也需要改变
@PostMapping("/upload/{id}")
public String upload(@PathVariable("id") Integer userid, String username, @RequestParam("sex") String usersex, MultipartFile photo, HttpSession session)throws Exception{}
@PostMapping(value = "/upload")
public String upload(MultipartFile files, HttpSession session) throws IOException {
String filePath=session.getServletContext().getRealPath("/file");
File file= new File(filePath);
if(!file.exists()){
file.mkdirs();
}
//获取上传文件的名称
String fileName=files.getOriginalFilename();
System.out.println(fileName);
String path=filePath+"/"+fileName;
files.transferTo(new File(path));
//重定向到List 文件列表展示页面
return "redirect:/list";
}
然后,把文件夹下的所有文件放在request作用域中,再跳转至文件列表
@GetMapping(value = "/list")
public String list(HttpSession session, Map<String ,Object> map){
String filePath=session.getServletContext().getRealPath("/file");
File file= new File(filePath);
//查找文件夹下的所有文件
File [] files=file.listFiles();
System.out.println(Arrays.toString(files));
map.put("files",files);
return "list";
}
前端页面展示,通过c标签,循环展示
<P>文件上传成功</P>
<c:forEach items="${files}" var="file">
<p><a href="${pageContext.request.contextPath}/download/${file.name}">${file.name}</a></p>
</c:forEach>
下载
下载文件名必须设置格式ISO-8859-1
返回 responseEntity<byte[]> 中 文件读取到字节流中
获取项目部署到服务器的路径:sesssion.getservletContext().getrealpath()
1、获取文件的字节流 fileinputstream 文件以流的方式读入到byte数组中
byte[] byt = new byte[]
2响应的头部信息 键值对 headers
3、状态 Httpstatus.OK
最后 return entity
@GetMapping("/download/{filename}")
public ResponseEntity<byte[]> down(@PathVariable("filename") String filename, HttpSession session)throws Exception{
String foldpath = session.getServletContext().getRealPath("/file");
//读取文件流 file中间是下载文件的绝对路径
InputStream inputStream = new FileInputStream(new File(foldpath+"/"+filename));
byte[] bs = new byte[inputStream.available()];
inputStream.read(bs);
//头部信息
MultiValueMap<String,String> headers = new HttpHeaders();
//下载的文件名字必须是iso-8859-1的编码格式
headers.add("Content-Disposition","attachment ; filename="+new String(filename.getBytes("UTF-8"),"ISO-8859-1"));
//ResponseEntity可以定义返回的HttpStatus(状态码)和HttpHeaders
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(bs,headers, HttpStatus.OK);
return entity;
}
至于下载保存的地址,是根据浏览器的设置而定。