博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot基础教程2-1-10 文件上传
阅读量:6911 次
发布时间:2019-06-27

本文共 3631 字,大约阅读时间需要 12 分钟。

hot3.png

1. 概述

文件上传,下载功能是web中常见功能,SpringBoot几乎把文件上传功能封装到了极致,只需短短的配置,和几行代码就能实现文件上传功能。

2. 源码分析

2.1. 添加pom.xml依赖

org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf

2.2 配置文件application.yml

spring:  thymeleaf:    # 禁用 thymeleaf 缓存    cache: false  servlet:    multipart:      # 是否支持批量上传   (默认值 true)      enabled: true      # 上传文件的临时目录 (一般情况下不用特意修改)      location:      # 上传文件最大为 10M (默认值 1M 根据自身业务自行控制即可)      max-file-size: 10MB      # 上传请求最大为 10M(默认值10M 根据自身业务自行控制即可)      max-request-size: 10MB      # 文件大小阈值,当大于这个阈值时将写入到磁盘,否则存在内存中,(默认值0 一般情况下不用特意修改)      file-size-threshold: 0      # 判断是否要延迟解析文件(相当于懒加载,一般情况下不用特意修改)      resolve-lazily: false

默认情况上面配置可以直接省略,不过了解配置能让我们更加理解SpringBoot文件上传细节,方便定位问题

2.3 Controller

@Slf4j@Controller@RequestMappingpublic class FileUploadController {    @GetMapping("/index")    public String index() {        return "index";    }    @PostMapping("/uploadOne")    @ResponseBody    public Map
uploadOne(@RequestParam("file") MultipartFile file) throws IOException { // TODO 将文件写入到指定目录(具体开发中有可能是将文件写入到云存储/或者指定目录通过 Nginx 进行 gzip 压缩和反向代理,此处只是为了演示故将地址写成本地电脑指定目录) file.transferTo(new File("E:\\temp\\" + file.getOriginalFilename())); Map
result = new HashMap<>(16); result.put("contentType", file.getContentType()); result.put("fileName", file.getOriginalFilename()); result.put("fileSize", file.getSize() + ""); return result; } @PostMapping("/uploadMulti") @ResponseBody public List
> uploadMulti(@RequestParam("file") MultipartFile[] files) throws IOException { if (files == null || files.length == 0) { return null; } List
> results = new ArrayList<>(); for (MultipartFile file : files) { // TODO file.transferTo(new File("E:\\temp\\" + file.getOriginalFilename())); Map
map = new HashMap<>(16); map.put("contentType", file.getContentType()); map.put("fileName", file.getOriginalFilename()); map.put("fileSize", file.getSize() + ""); results.add(map); } return results; } @PostMapping("/uploadBase") @ResponseBody public Map
uploadBase(String base64) throws IOException { // TODO BASE64 方式的 格式和名字需要自己控制(如 png 图片编码后前缀就会是 data:image/png;base64,) final File tempFile = new File("E:\\temp\\test.jpg"); // TODO 防止有的传了 data:image/png;base64, 有的没传的情况 String[] d = base64.split("base64,"); final byte[] bytes = Base64Utils.decodeFromString(d.length > 1 ? d[1] : d[0]); FileCopyUtils.copy(bytes, tempFile); Map
result = new HashMap<>(16); result.put("contentType", tempFile.getAbsolutePath()); result.put("fileName", tempFile.getName()); result.put("fileSize", String.valueOf(tempFile.getTotalSpace())); return result; }}

@GetMapping("/index")用来跳转到index.html

@PostMapping("/uploadOne"), @PostMapping("/uploadMulti"), @PostMapping("/uploadBase")分别处理单个文件,多个文件,BASE64编码 @RequestParam("file") 此处的ile对应的就是htmlname="file"input标签,而将文件真正写入的还是借助的commons-io中的FileUtils.copyInputStreamToFile(inputStream,file)

2.4 上传交互页面

    
文件上传

单一文件上传示例

文件1:


批量文件上传示例

文件1:

文件2:


Base64文件上传

BASE64编码:

3. 测试结果

其中,BASE64测试,先将一张图片转换为BASE64编码

4. 工程目录

5. 结束语

说点什么呢,有任何建议,欢迎留言探讨,。


欢迎关注博主公众号,第一时间推送最新文章

欢迎关注博主公众号

转载于:https://my.oschina.net/Mkeeper/blog/1929561

你可能感兴趣的文章
Entity Framework Code-First(9.4):DataAnnotations - Required Attribute
查看>>
Linux crm 运行
查看>>
利用View静态画图
查看>>
Spring MVC
查看>>
不错的资源哦
查看>>
多线程概念
查看>>
emqttd 2.2安装和测试使用
查看>>
Objective-C之优雅的命名
查看>>
php output_buffering 缓存使用
查看>>
深度学习和神经网络的区别是什么
查看>>
Decorator模式
查看>>
每日练习
查看>>
LeetCode算法题-First Unique Character in a String(Java实现)
查看>>
【小程序】小程序开发自定义组件的步骤>>>>>>>>>小程序开发过程中报错:jsEnginScriptError...
查看>>
OSI模型
查看>>
用Quick Cocos2dx做一个连连看(三)
查看>>
好久没做.Net开发了,今天配置IIS和.Net Framework 4.0遇到点问题
查看>>
emoji情感分类器
查看>>
简单理解java反射机制
查看>>
Codeforces 399B - Red and Blue Balls
查看>>