一:任务

1.任务

  文件的上传

  文件的下载

二:文件的上传

1.新建一个对象

  FileInfo.java

  1. package com.cao.dto;
  2.  
  3. public class FileInfo {
  4. private String path;
  5. public FileInfo(String path) {
  6. this.path=path;
  7. }
  8. public String getPath() {
  9. return path;
  10. }
  11. public void setPath(String path) {
  12. this.path = path;
  13. }
  14.  
  15. }

2.新建控制器

  1. package com.cao.web.controller;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Date;
  6.  
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import org.springframework.web.multipart.MultipartFile;
  11.  
  12. import com.cao.dto.FileInfo;
  13.  
  14. @RestController
  15. @RequestMapping("/file")
  16. public class FileController {
  17. @PostMapping
  18. public FileInfo uploadFile(MultipartFile fileKey) throws Exception {
  19. System.out.println("fileName: "+fileKey.getName());
  20. System.out.println("OriginalFilename: "+fileKey.getOriginalFilename());
  21. System.out.println("size: "+fileKey.getSize());
  22. //将要存储在controller的目录下
  23. String folder="E:\\workspace-sts-3.9.5.RELEASE\\it-security-demo\\src\\main\\java\\com\\cao\\web\\controller";
  24. File localFile=new File(folder,new Date().getTime()+".txt");
  25. fileKey.transferTo(localFile);
  26. //写入流中,这个主要用于写入其他的地方,例如服务器等,这里不写了
  27. //fileKey.getInputStream()
  28. return new FileInfo(localFile.getAbsolutePath());
  29. }
  30. }

3.测试类

  1. /**
  2. * 测试文件的上传
  3. * @throws Exception
  4. */
  5. @Test
  6. public void whenUploadSuccess() throws Exception {
  7. String result=mockMvc.perform(MockMvcRequestBuilders.fileUpload("/file")
  8. .file(new MockMultipartFile("fileKey","test.txt","multipart/form-data","hello".getBytes("UTF-8"))))
  9. .andExpect(MockMvcResultMatchers.status().isOk())
  10. .andReturn().getResponse().getContentAsString();
  11. System.out.println("result="+result);
  12. }

4.控制台

  

  存储到的现象

  

三:文件的下载

1.添加io操作的包

  1. <dependency>
  2. <groupId>commons-io</groupId>
  3. <artifactId>commons-io</artifactId>
  4. </dependency>

2.文件下载的程序

  1. package com.cao.web.controller;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.util.Date;
  9.  
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12.  
  13. import org.apache.commons.io.IOUtils;
  14. import org.springframework.web.bind.annotation.GetMapping;
  15. import org.springframework.web.bind.annotation.PathVariable;
  16. import org.springframework.web.bind.annotation.PostMapping;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import org.springframework.web.multipart.MultipartFile;
  20.  
  21. import com.cao.dto.FileInfo;
  22.  
  23. @RestController
  24. @RequestMapping("/file")
  25. public class FileController {
  26.  
  27. String folder="E:\\workspace-sts-3.9.5.RELEASE\\it-security-demo\\src\\main\\java\\com\\cao\\web\\controller";
  28.  
  29. /**
  30. * 文件的上传控制器
  31. * @param fileKey
  32. * @return
  33. * @throws Exception
  34. */
  35. @PostMapping
  36. public FileInfo uploadFile(MultipartFile fileKey) throws Exception {
  37. System.out.println("fileName: "+fileKey.getName());
  38. System.out.println("OriginalFilename: "+fileKey.getOriginalFilename());
  39. System.out.println("size: "+fileKey.getSize());
  40. //将要存储在controller的目录下
  41. File localFile=new File(folder,new Date().getTime()+".txt");
  42. fileKey.transferTo(localFile);
  43. //写入流中,这个主要用于写入其他的地方,例如服务器等,这里不写了
  44. //fileKey.getInputStream()
  45. return new FileInfo(localFile.getAbsolutePath());
  46. }
  47.  
  48. /**
  49. * 文件的下载
  50. */
  51. @GetMapping("/{id}")
  52. public void downFile(@PathVariable String id, HttpServletRequest request,HttpServletResponse response) {
  53. try(
  54. InputStream inputStream=new FileInputStream(new File(folder,id+".txt"));
  55. OutputStream outputStream=response.getOutputStream();
  56. )
  57. {
  58. response.setContentType("application/x-download");
  59. response.addHeader("Content-Disposition", "attachment;filename=test.txt");
  60. IOUtils.copy(inputStream, outputStream);
  61. outputStream.flush();
  62. } catch (Exception e) {
  63. // TODO: handle exception
  64. }
  65. }
  66.  
  67. }

3.在浏览器上访问

  

009 spring boot中文件的上传与下载的更多相关文章

  1. iOS开发中文件的上传和下载功能的基本实现-备用

    感谢大神分享 这篇文章主要介绍了iOS开发中文件的上传和下载功能的基本实现,并且下载方面讲到了大文件的多线程断点下载,需要的朋友可以参考下 文件的上传 说明:文件上传使用的时POST请求,通常把要上传 ...

  2. JavaWeb中文件的上传和下载

    JavaWeb中文件的上传和下载 转自: JavaWeb学习总结(五十)——文件上传和下载 - 孤傲苍狼 - 博客园https://www.cnblogs.com/xdp-gacl/p/4200090 ...

  3. Spring MVC 实现文件的上传和下载

    前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”.我回复他说:“使用Spring MVC框架可以做到这一点,因为Spri ...

  4. Java中文件的上传与下载

    文件的上传与下载主要用到两种方法: 1.方法一:commons-fileupload.jar  commons-io.jar apache的commons-fileupload实现文件上传,下载 [u ...

  5. Spring MVC 实现文件的上传和下载 (八)

    完整的项目案例: springmvc.zip 目录 SpringMVC 中,文件的上传,是通过 MultipartResolver 实现的. 所以,如果要实现文件的上传,只要在 spring-mvc. ...

  6. Struts中文件的上传与下载

    前面学到的用组件去上传 前台: 1.post表单提交 2.表单类型 multipart/form-data 3.intput type=file 后台: Apach提供的FileUpload组件 核心 ...

  7. docker容器中文件的上传与下载

    原文地址:传送门 1.上传文件 docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH [OPTIONS]:保持源目标中的链接,例: docker cp ...

  8. JSP中文件的上传于下载演示样例

    一.文件上传的原理     1.文件上传的前提:         a.form表单的method必须是post         b.form表单的enctype必须是multipart/form-da ...

  9. Struts2中文件的上传与下载

    文件上传 1.jsp页面 <s:form action="fileAction" namespace="/file" method="POST& ...

随机推荐

  1. 单点登录SSO的原理及实现方式总结

      核心思想   用户信息的集中存储(全局Cooike.集中式Session.Json Web Token.Redis缓存服务器.自定义SSO服务器)   认证(Filter中执行)   登出(不同站 ...

  2. 彻底搞透OAuth 2.0

    OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...

  3. 饿了么vue-cli3.0+cube-ui笔记

    1.目录结构 模板文件是public里的index.html,运行项目的时候,会引用src/main.js(入口文件) 详细文档在这里:https://cli.vuejs.org/zh/config/ ...

  4. Confluence 6 避免和清理垃圾

    如果你的 Confluence 是允许公众访问的话,你可能会遇到垃圾内容的骚扰. 阻止垃圾发布者 希望阻止垃圾发布者: 启用验证码(Captcha),请参考页面 Configuring Captcha ...

  5. 使用 mod_rewrite 来修改 Confluence 6 的 URLs

    备注:这个页面的文档是 Apache 的配置,而不是 Confluence 自己的配置.Atlassian 将会对 Confluence 的配置提供支持,但是我们不能保证能够对你所有在配置 Apach ...

  6. elementui上传图片到七牛云服务器

    注册七牛云 首先,注册七牛云,并且完成实名认证,完成后会在个人中心->秘钥管理中看到两个秘钥AccessKey/SecretKey 创建存储空间(必须要实名认证) 生成上传凭证 为了实现上传,我 ...

  7. HTML&javaSkcript&CSS&jQuery&ajax-Css

    CSS 1 .eg <head> <style> body{ background-color:#d0e4fe;} h1{ color:orange; text-alin:ce ...

  8. Dinner

    问题 : Dinner 时间限制: 1 Sec  内存限制: 32 MB 题目描述 Little A is one member of ACM team. He had just won the go ...

  9. java使用POI解析2007以上的Excel表格

    来自http://hao0610.iteye.com/blog/1160678 使用poi来解析Excel的xls和xlsx. 解析xls: package xls; import java.io.F ...

  10. 读书笔记——《You Don't Know JS》

    第一部:<You don't know JS: this & Object prototype> 第三章 Object 对象常量 var myObject = {}; Object ...