文件的上传下载
- 文件上传
- 依赖
- 配置
- 文件上传的最大值
- 文件请求的最大值
- 临时文件存储路径
- Controller
- 方法一
- 方法二
- 文件下载
- 方法一
- 方法二
文件上传
今天工作中需求涉及到文件的上传和下载,Spring Boot提供了Multipart文件上传的支持。Multipart是HTTP协议中的一种方式,用于支持文件上传。下面我们将介绍如何在Spring Boot中实现文件上传。
依赖
首先最重要就是导入依赖
org.springframework.boot
spring-boot-starter-web
使用Thymeleaf进行模板渲染,可以添加以下依赖:
org.springframework.boot
spring-boot-starter-thymeleaf
配置
文件上传的最大值
spring.servlet.multipart.max-file-size=10MB
文件请求的最大值
spring.servlet.multipart.max-request-size=10MB
临时文件存储路径
spring.servlet.multipart.location=/tmp
Controller
在Spring Boot中实现文件上传需要编写一个Controller,其中包含两个方法,一个用于返回上传文件的表单页面,另一个用于实际处理文件上传。
方法一
废话不多说,直接看代码
/*** 上传文件** @param month* @param file*/public void uploadFile(String month, MultipartFile file) throws IOException {// 创建文件对象FzMonthReportFileDO fzMonthReportFileDO = new FzMonthReportFileDO();fzMonthReportFileDO.setFileMonth(month);fzMonthReportFileDO.setIsDelete(0);// 判断是否有原文件,有原文件则删除FzMonthReportFileDO reportFileDO = fzMonthReportFileDao.selectOne(fzMonthReportFileDO);if (reportFileDO != null) {reportFileDO.setIsDelete(1);fzMonthReportFileDao.updateByPrimaryKey(reportFileDO);}fzMonthReportFileDO.setId(GUID.newGUID());fzMonthReportFileDO.setRev(System.currentTimeMillis());fzMonthReportFileDO.setContent(file.getBytes());fzMonthReportFileDO.setFileType("上传");fzMonthReportFileDO.setName(file.getOriginalFilename());fzMonthReportFileDO.setFileSize(String.valueOf(file.getSize()));fzMonthReportFileDO.setIsDelete(0);fzMonthReportFileDao.insert(fzMonthReportFileDO);}
上面逻辑很简单,先根据传入的年月去查出数据,然后判断数据是否为空,不为空就进行一个逻辑删除,接着将数据进行一个保存
方法二
传文件和下载文件都比较简单,我们就直接在controller层来编写。也不用在pom.xml 中增加什么依赖。所以直接上代码。在controller 包下创建一个file包,在file 包下创建一个FileController 类。
@RestController
@RequestMapping("file")
@Slf4j
public class FileController {@Value("${file.upload.url}")private String uploadFilePath;@RequestMapping("/upload")public String httpUpload(@RequestParam("files") MultipartFile files[]){JSONObject object=new JSONObject();for(int i=0;i<files.length;i++){String fileName = files[i].getOriginalFilename(); // 文件名File dest = new File(uploadFilePath +'/'+ fileName);if (!dest.getParentFile().exists()) {dest.getParentFile().mkdirs();}try {files[i].transferTo(dest);} catch (Exception e) {log.error("{}",e);object.put("success",2);object.put("result","程序错误,请重新上传");return object.toString();}}object.put("success",1);object.put("result","文件上传成功");return object.toString();}}
上面的代码看起来有点多,其实就是一个上传的方法,首先通过 MultipartFile 接收文件。这里我用的是file[] 数组接收文件,这是为了兼容多文件上传的情况,如果只用file 接收,然后在接口上传多个文件的话,只会接收最后一个文件。这里大家注意一下。看自己的需求,我这里兼容多文件所以用数组接收。
然后遍历files 获取文件,下面这段代码是判断文件在所在目录是否存在,如果不存在就创建对应的目录。
File dest = new File(uploadFilePath +'/'+ fileName);if (!dest.getParentFile().exists()) {dest.getParentFile().mkdirs();}files[i].transferTo(dest);
文件下载
方法一
其实文件下载,不太建议用接口做,因为文件下载一般都是下载一些静态文件,我们可以先将文件处理好,然后通过Nginx 服务下载静态文件,这样速度会快很多。但是这里我们还是写一下。代码也很简单,就一个方法,也写在fileController 类中
@RequestMapping("/download")public String fileDownLoad(HttpServletResponse response, @RequestParam("fileName") String fileName){File file = new File(downloadFilePath +'/'+ fileName);if(!file.exists()){return "下载文件不存在";}response.reset();response.setContentType("application/octet-stream");response.setCharacterEncoding("utf-8");response.setContentLength((int) file.length());response.setHeader("Content-Disposition", "attachment;filename=" + fileName );try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));) {byte[] buff = new byte[1024];OutputStream os = response.getOutputStream();int i = 0;while ((i = bis.read(buff)) != -1) {os.write(buff, 0, i);os.flush();}} catch (IOException e) {log.error("{}",e);return "下载失败";}return "下载成功";}
代码也很简单,就是根据文件名判断是否存在文件,不存在就提示没有文件,存在就将文件下载下来。response设置返回文件的格式,以文件流的方式返回,采用utf-8 字符集,设置下载后的文件名。然后就是以文件流的方式下载文件了。
方法二
/*** 文件下载** @param month* @param response*/public void downloadFile(String month, HttpServletResponse response) throws Exception {// 创建文件对象FzMonthReportFileDO fzMonthReportFileDO = new FzMonthReportFileDO();fzMonthReportFileDO.setFileMonth(month);fzMonthReportFileDO.setIsDelete(0);// 判断原文件是否为空,不为空就进行逻辑删除FzMonthReportFileDO reportFileDO = fzMonthReportFileDao.selectOne(fzMonthReportFileDO);if (reportFileDO != null) {try {// 清空responseresponse.reset();// 设置response的Headerresponse.setCharacterEncoding("UTF-8");//Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存//attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"// filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(reportFileDO.getName(), "UTF-8"));OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");outputStream.write(reportFileDO.getContent());outputStream.flush();} catch (Exception e) {try {response.setStatus(HttpServletResponse.SC_NOT_FOUND);response.getOutputStream().write("下载文件失败".getBytes(StandardCharsets.UTF_8));} catch (IOException e1) {e1.printStackTrace();}}}}
以上就是文件的上传和下载,希望能帮到需要的人