009 spring boot中文件的上传与下载
一:任务
1.任务
文件的上传
文件的下载
二:文件的上传
1.新建一个对象
FileInfo.java
- package com.cao.dto;
- public class FileInfo {
- private String path;
- public FileInfo(String path) {
- this.path=path;
- }
- public String getPath() {
- return path;
- }
- public void setPath(String path) {
- this.path = path;
- }
- }
2.新建控制器
- package com.cao.web.controller;
- import java.io.File;
- import java.io.IOException;
- import java.util.Date;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
- import com.cao.dto.FileInfo;
- @RestController
- @RequestMapping("/file")
- public class FileController {
- @PostMapping
- public FileInfo uploadFile(MultipartFile fileKey) throws Exception {
- System.out.println("fileName: "+fileKey.getName());
- System.out.println("OriginalFilename: "+fileKey.getOriginalFilename());
- System.out.println("size: "+fileKey.getSize());
- //将要存储在controller的目录下
- String folder="E:\\workspace-sts-3.9.5.RELEASE\\it-security-demo\\src\\main\\java\\com\\cao\\web\\controller";
- File localFile=new File(folder,new Date().getTime()+".txt");
- fileKey.transferTo(localFile);
- //写入流中,这个主要用于写入其他的地方,例如服务器等,这里不写了
- //fileKey.getInputStream()
- return new FileInfo(localFile.getAbsolutePath());
- }
- }
3.测试类
- /**
- * 测试文件的上传
- * @throws Exception
- */
- @Test
- public void whenUploadSuccess() throws Exception {
- String result=mockMvc.perform(MockMvcRequestBuilders.fileUpload("/file")
- .file(new MockMultipartFile("fileKey","test.txt","multipart/form-data","hello".getBytes("UTF-8"))))
- .andExpect(MockMvcResultMatchers.status().isOk())
- .andReturn().getResponse().getContentAsString();
- System.out.println("result="+result);
- }
4.控制台
存储到的现象
三:文件的下载
1.添加io操作的包
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- </dependency>
2.文件下载的程序
- package com.cao.web.controller;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Date;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.io.IOUtils;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
- import com.cao.dto.FileInfo;
- @RestController
- @RequestMapping("/file")
- public class FileController {
- String folder="E:\\workspace-sts-3.9.5.RELEASE\\it-security-demo\\src\\main\\java\\com\\cao\\web\\controller";
- /**
- * 文件的上传控制器
- * @param fileKey
- * @return
- * @throws Exception
- */
- @PostMapping
- public FileInfo uploadFile(MultipartFile fileKey) throws Exception {
- System.out.println("fileName: "+fileKey.getName());
- System.out.println("OriginalFilename: "+fileKey.getOriginalFilename());
- System.out.println("size: "+fileKey.getSize());
- //将要存储在controller的目录下
- File localFile=new File(folder,new Date().getTime()+".txt");
- fileKey.transferTo(localFile);
- //写入流中,这个主要用于写入其他的地方,例如服务器等,这里不写了
- //fileKey.getInputStream()
- return new FileInfo(localFile.getAbsolutePath());
- }
- /**
- * 文件的下载
- */
- @GetMapping("/{id}")
- public void downFile(@PathVariable String id, HttpServletRequest request,HttpServletResponse response) {
- try(
- InputStream inputStream=new FileInputStream(new File(folder,id+".txt"));
- OutputStream outputStream=response.getOutputStream();
- )
- {
- response.setContentType("application/x-download");
- response.addHeader("Content-Disposition", "attachment;filename=test.txt");
- IOUtils.copy(inputStream, outputStream);
- outputStream.flush();
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- }
3.在浏览器上访问
009 spring boot中文件的上传与下载的更多相关文章
- iOS开发中文件的上传和下载功能的基本实现-备用
感谢大神分享 这篇文章主要介绍了iOS开发中文件的上传和下载功能的基本实现,并且下载方面讲到了大文件的多线程断点下载,需要的朋友可以参考下 文件的上传 说明:文件上传使用的时POST请求,通常把要上传 ...
- JavaWeb中文件的上传和下载
JavaWeb中文件的上传和下载 转自: JavaWeb学习总结(五十)——文件上传和下载 - 孤傲苍狼 - 博客园https://www.cnblogs.com/xdp-gacl/p/4200090 ...
- Spring MVC 实现文件的上传和下载
前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”.我回复他说:“使用Spring MVC框架可以做到这一点,因为Spri ...
- Java中文件的上传与下载
文件的上传与下载主要用到两种方法: 1.方法一:commons-fileupload.jar commons-io.jar apache的commons-fileupload实现文件上传,下载 [u ...
- Spring MVC 实现文件的上传和下载 (八)
完整的项目案例: springmvc.zip 目录 SpringMVC 中,文件的上传,是通过 MultipartResolver 实现的. 所以,如果要实现文件的上传,只要在 spring-mvc. ...
- Struts中文件的上传与下载
前面学到的用组件去上传 前台: 1.post表单提交 2.表单类型 multipart/form-data 3.intput type=file 后台: Apach提供的FileUpload组件 核心 ...
- docker容器中文件的上传与下载
原文地址:传送门 1.上传文件 docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH [OPTIONS]:保持源目标中的链接,例: docker cp ...
- JSP中文件的上传于下载演示样例
一.文件上传的原理 1.文件上传的前提: a.form表单的method必须是post b.form表单的enctype必须是multipart/form-da ...
- Struts2中文件的上传与下载
文件上传 1.jsp页面 <s:form action="fileAction" namespace="/file" method="POST& ...
随机推荐
- 单点登录SSO的原理及实现方式总结
核心思想 用户信息的集中存储(全局Cooike.集中式Session.Json Web Token.Redis缓存服务器.自定义SSO服务器) 认证(Filter中执行) 登出(不同站 ...
- 彻底搞透OAuth 2.0
OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...
- 饿了么vue-cli3.0+cube-ui笔记
1.目录结构 模板文件是public里的index.html,运行项目的时候,会引用src/main.js(入口文件) 详细文档在这里:https://cli.vuejs.org/zh/config/ ...
- Confluence 6 避免和清理垃圾
如果你的 Confluence 是允许公众访问的话,你可能会遇到垃圾内容的骚扰. 阻止垃圾发布者 希望阻止垃圾发布者: 启用验证码(Captcha),请参考页面 Configuring Captcha ...
- 使用 mod_rewrite 来修改 Confluence 6 的 URLs
备注:这个页面的文档是 Apache 的配置,而不是 Confluence 自己的配置.Atlassian 将会对 Confluence 的配置提供支持,但是我们不能保证能够对你所有在配置 Apach ...
- elementui上传图片到七牛云服务器
注册七牛云 首先,注册七牛云,并且完成实名认证,完成后会在个人中心->秘钥管理中看到两个秘钥AccessKey/SecretKey 创建存储空间(必须要实名认证) 生成上传凭证 为了实现上传,我 ...
- HTML&javaSkcript&CSS&jQuery&ajax-Css
CSS 1 .eg <head> <style> body{ background-color:#d0e4fe;} h1{ color:orange; text-alin:ce ...
- Dinner
问题 : Dinner 时间限制: 1 Sec 内存限制: 32 MB 题目描述 Little A is one member of ACM team. He had just won the go ...
- java使用POI解析2007以上的Excel表格
来自http://hao0610.iteye.com/blog/1160678 使用poi来解析Excel的xls和xlsx. 解析xls: package xls; import java.io.F ...
- 读书笔记——《You Don't Know JS》
第一部:<You don't know JS: this & Object prototype> 第三章 Object 对象常量 var myObject = {}; Object ...