Spring Boot文档阅读笔记-FileHandling解析及抓包分析
这篇博文将说明使用WEB服务上传和下载文件。
首先是文件上传:
使用MultipartFile作为请求参数,这个上传API使用Multi-Part表单的值:
代码如下:
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)public String fileUpload(@RequestParam("file")MultipartFile file) throws IOException {File convertFile = new File("./" + file.getOriginalFilename());convertFile.createNewFile();FileOutputStream fout = new FileOutputStream(convertFile);fout.write(file.getBytes());fout.close();return "File is upload successfully";}
下面是文件下载:
使用InputStreamResource获取需要下载的文件,然后将http头设置为Content-Disposition,并且还需指定响应为流媒体。
完整代码如下:
@GetMapping(value = "/download")public ResponseEntity<Object> download(@RequestParam("fileName") String fileName) throws FileNotFoundException {File file = new File(fileName);InputStreamResource inputStreamResource = new InputStreamResource(new FileInputStream(file));HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));httpHeaders.add("Cache-Control", "no-cache, no-store, must-revalidate");httpHeaders.add("Pragma", "no-cache");httpHeaders.add("Expires", "0");ResponseEntity<Object> responseEntity = ResponseEntity.ok().headers(httpHeaders).contentLength(file.length()).contentType(MediaType.parseMediaType("application/txt")).body(inputStreamResource);return responseEntity;}
使用如下:
上传成功后,会在这个目录中存在文件:
项目打包下载地址:
https://github.com/fengfanchen/Java/tree/master/SpringBootFileHandling
下面来分析下这个过程:
使用Fiddler抓包如下:
上传文件
从中可以看到,content-type为multipart/formdata其中边界分隔符为后面那个。他传的其实是二进制。
content-Disposition为内容倾向,为表单数据,name为程序中需要提交的键,filename为文件名。
下面的行,就是文件内容了。
下面来看看下载:
输入URL会激活IDM的下载
Fiddler下载如下:
这里的返回为Content-Disposition:为attachment说明是附件以及filename="cff.pdf",很多浏览器就是根据这条来判断,是文件的。
如果没有这条,就不会激发浏览器的下载。
Spring Boot文档阅读笔记-FileHandling解析及抓包分析相关推荐
- Spring Boot文档阅读笔记-@SpringBootApplication官方解析与实例(1.5.19)
目录 官方解析 博主例子 官方解析 @SpringBootApplication有如下3个特点: 1. @EnableAutoConfiguration: 能够启动Spring Boot的自动配置机制 ...
- Spring Boot文档阅读笔记-Spring Boot @Bean解析
利用SpringBoot的@Bean创建一个简单的Bean. Spring的@Bean注解是放在方法上的,带上这个注解的方法会被Spring容器管理.并且这个方法要返回一个值(对象),这个值和对象会被 ...
- Spring Boot文档阅读笔记-对Securing a Web Application解析
首先创建一个非安全的Web应用 这个应用包含两个页面,一个是home页面,一个是"Hello,World"页面.home页面使用Thymeleaf,相关代码如下: <!DOC ...
- Spring Boot文档阅读笔记-对Messaging with RabbitMQ解析
此篇教程以Rabbitmq作为消息队列服务端,使用Spring Boot产生和发布消息. 使用Spring AMQP的RabbitTemplate发布消息,使用MessageListenerAdapt ...
- Spring Boot文档阅读笔记-how-to-implement-2-way-ssl-using-spring-boot
two-way-ssl需要12次握手(除去TCP的三次握手),如下图: 双向认证过程: 1.客户端发送ClientHello消息,告诉服务端要使用SSL. 2.客户端发送ServerHello的响应, ...
- Spring Boot文档阅读笔记-EhCache的使用
这里要先注意2个概念: buffer和cache,很多人会讲这两个概念混用.但其实这是两个概念! buffer:一般是指存储临时数据的实体.只能读写一次,对于程序员来说buffer是可见的,比如TCB ...
- Spring Boot文档阅读笔记-构建Restful风格的WebService客户端
对应的maven如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- Spring Boot文档阅读笔记-构建Restful风格的WebService
Maven代码如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- Spring Boot文档阅读笔记-exception handling–@ExceptionHandler解析与实例
程序结构如下: 源码如下: CustomExceptionHandler.java package com.example.demo.controller;import com.example.dem ...
最新文章
- android资料整理
- 谁是“艾灵”?是腾讯的真国风 AI 虚拟人!
- 计算机网络_NAT与NAPT
- WebApp NativeApp HybirdApp
- glBegin()和glEnd()之间可调用的函数
- Eclipse之实用快捷键
- [mmu/cache]-cache的一些基本概念介绍
- Dynamic AX ERP 4.0 数据导出(上)
- jenkins角色权限管理
- 你的心事我全知晓——心情日记小程序丨实战
- 最短路径 自己写的一个很简单的模板 dijkstra算法
- 10分钟学会数据库压力测试
- 联想·云计算中心运维服务(1+X中级)教材出版
- cp:略过目录:”文件名“
- Java8增加功能--Effectively final 功能
- [分享]RFID之我的M1离线卡爆破过程
- csu-2018年11月月赛Round2-div1题解
- Git 术语及中英文对照
- sql入门(3)------SQLServer3
- Vuforia Ground Plane 平面识别