mvc java 实现文件下载_Spring MVC 实现文件的上传和下载

SpringMVC 中,文件的上传,是通过 MultipartResolver 实现的。 所以,如果要实现文件的上传,只要在 spring-mvc.xml 中注册相应的 MultipartResolver 即可。

MultipartResolver 的实现类有两个:

CommonsMultipartResolver

StandardServletMultipartResolver

两个的区别:

第一个需要使用 Apache 的 commons-fileupload 等 jar 包支持,但它能在比较旧的 servlet 版本中使用。

第二个不需要第三方 jar 包支持,它使用 servlet 内置的上传功能,但是只能在 Servlet 3 以上的版本使用。

第一个使用步骤:

/*CommonsMultipartResolver  上传用到的两个包*/

"commons-fileupload:commons-fileupload:1.3.1",

"commons-io:commons-io:2.4"

如果是maven项目的话直接导入:

commons-fileupload

commons-fileupload

1.3.1

dispatcher-servlet.xml配置:<?xml version="1.0" encoding="UTF-8"?>

web.xml:<?xml version="1.0" encoding="UTF-8"?>dispatcherorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:dispatcher-servlet.xmldispatcher/

后台java(上传、下载)处理代码:package edu.nf.ch08.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.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.servlet.ModelAndView;import java.io.File;import java.io.IOException;/** * @author wangl * @date 2018/11/2 */@Controllerpublic class UploadController { /** * 文件上传只需要Spring传入一个MultipartFile对象即可, * 这个对象可以获取文件相关上传的信息。 * 一个MultipartFile表示单个文件上传,当需要上传多个文件时 * 只需要声明为MultipartFile[]数组即可。 * @return */ @PostMapping("/upload") public ModelAndView upload(MultipartFile file){ //获取当前系统用户目录 String home = System.getProperty("user.home"); //指定上传的文件夹目录 File uploadDir = new File(home + "/files"); //如果目录不存在,则创建 if(!uploadDir.exists()){ uploadDir.mkdir(); } //获取上传的文件名 String fileName = file.getOriginalFilename(); //构建一个完整的文件上传对象 File uploadFile = new File(uploadDir.getAbsolutePath() + "/" + fileName); try { //通过transferTo方法进行上传 file.transferTo(uploadFile); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } //将文件名存入Model,转发到index页面 ModelAndView mv = new ModelAndView("index"); mv.addObject("fileName", fileName); return mv; } /** * 文件下载 * 读取服务器本地文件并封装为ResponseEntity对象 * 响应客户端,写回的是一个字节数组 * @param fileName 文件名 * @return */ @GetMapping("/download") public ResponseEntity download(String fileName){ //依据文件名构建本地文件路径 String filePath = System.getProperty("user.home") + "/files/" + fileName; //依据文件路径构建File对象 File file = new File(filePath); //创建响应头对象,设置响应信息 HttpHeaders headers = new HttpHeaders(); try { //对文件名进行重新编码,防止在响应头中出现中文乱码 String headerFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1"); //设置响应内容处理方式为附件,并指定文件名 headers.setContentDispositionFormData("attachment", headerFileName); //设置响应头类型为application/octet-stream,表示是一个流类型 headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); //将文件转换成字节数组 byte[] bytes = FileUtils.readFileToByteArray(file); //创建ResponseEntity对象(封装文件字节数组、响应头、响应状态码) ResponseEntity entity = new ResponseEntity<>(bytes, headers, HttpStatus.CREATED); //最后将整个ResponseEntity对象返回给DispatcherServlet return entity; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("文件下载失败"); } }}

上传文件的网页html:

Title

文件上传

File:

e042553d2f1d

上传成功后转发的jsp(下载文件)页面:

Created by IntelliJ IDEA.

User: wangl

Date: 2018/11/2

Time: 09:56

To change this template use File | Settings | File Templates.

--%>

Title

${fileName}

e042553d2f1d

项目结构:

e042553d2f1d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值