这篇文章主要介绍,如何在springboot工程作为服务器,去接收通过http 上传的multi-file的文件。

构建工程

为例创建一个springmvc工程你需要spring-boot-starter-thymeleaf和 spring-boot-starter-web的起步依赖。为例能够上传文件在服务器,你需要在web.xml中加入标签做相关的配置,但在sringboot 工程中,它已经为你自动做了,所以不需要你做任何的配置。

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies>

创建文件上传controller

直接贴代码:

@Controller
public class FileUploadController {private final StorageService storageService;@Autowiredpublic FileUploadController(StorageService storageService) {this.storageService = storageService;}@GetMapping("/")public String listUploadedFiles(Model model) throws IOException {model.addAttribute("files", storageService.loadAll().map(path ->MvcUriComponentsBuilder.fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString()).build().toString()).collect(Collectors.toList()));return "uploadForm";}@GetMapping("/files/{filename:.+}")@ResponseBodypublic ResponseEntity<Resource> serveFile(@PathVariable String filename) {Resource file = storageService.loadAsResource(filename);return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"").body(file);}@PostMapping("/")public String handleFileUpload(@RequestParam("file") MultipartFile file,RedirectAttributes redirectAttributes) {storageService.store(file);redirectAttributes.addFlashAttribute("message","You successfully uploaded " + file.getOriginalFilename() + "!");return "redirect:/";}@ExceptionHandler(StorageFileNotFoundException.class)public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) {return ResponseEntity.notFound().build();}}

这个类通过@Controller注解,表明自己上一个Spring mvc的c。每个方法通过
@GetMapping 或者@PostMapping注解表明自己的 http方法。

  • GET / 获取已经上传的文件列表
  • GET /files/{filename}  下载已经存在于服务器的文件
  • POST / 上传文件给服务器

创建一个简单的 html模板

为了展示上传文件的过程,我们做一个界面:
在src/main/resources/templates/uploadForm.html

<html xmlns:th="http://www.thymeleaf.org">
<body><div th:if="${message}"><h2 th:text="${message}"/></div><div><form method="POST" enctype="multipart/form-data" action="/"><table><tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr><tr><td></td><td><input type="submit" value="Upload" /></td></tr></table></form></div><div><ul><li th:each="file : ${files}"><a th:href="${file}" th:text="${file}" /></li></ul></div></body>
</html>

上传文件大小限制

如果需要限制上传文件的大小也很简单,只需要在springboot 工程的src/main/resources/application.properties 加入以下:

spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB

测试

测试情况如图:

参考资料

https://spring.io/guides/gs/uploading-files/

源码下载

https://github.com/forezp/SpringBootLearning

SpringBoot第十七篇:上传文件相关推荐

  1. 企业级 SpringBoot 教程 (十七)上传文件

    这篇文章主要介绍,如何在springboot工程作为服务器,去接收通过http 上传的multi-file的文件. 构建工程 为例创建一个springmvc工程你需要spring-boot-start ...

  2. Java springcloud B2B2C o2o多用户商城 springcloud架构 (十七)上传文件

    2019独角兽企业重金招聘Python工程师标准>>> 这篇文章主要介绍,如何在springboot工程作为服务器,去接收通过http 上传的multi-file的文件. 构建工程 ...

  3. springboot集成阿里OSS上传文件

    开发前的准备: 自己在阿里云上申请免费的Oss对象存储,新建Bucket---读写权限选择公共读,我开始这里忘了导致后来踩了小坑 接下来是代码中的一些配置:我用的是springboot 1>添加 ...

  4. springboot以FTP方式上传文件到远程服务器

    此处远程服务器是ubuntu,关于ftpserver的配置请参考该文https://blog.csdn.net/sunxiaoju/article/details/85224602,在此处就不再赘述. ...

  5. django ajax文件上传,django 之 ajax 篇 上传文件

    前段: Title {% csrf_token %} 输入正确 ajax上传文件(前段): {% load static %} Title {% csrf_token %} {# #} 输入正确 头像 ...

  6. SpringBoot搭建的项目上传文件时提示The temporary upload location ***is not valid

    系统环境CentOS 7 + Java 8 SpringBoot内置的Tomcat启动时创建临时目录保存上传缓冲文件,但CentOS会定期清理 默认的临时文件目录(一般都在/tmp下) 解决方案: 一 ...

  7. springboot整合amazonS3,封装上传文件接口

    之前整理过spring整合amazonS3的博客,也整理过遇到的问题和文档.今天整理下springboot项目下如何使用amazonS3,并且如果是封装接口的话,是怎样的? 1.项目结构 2.在pom ...

  8. Ant Design Vue <a-upload>上传文件

    使用SpringBoot+Vue+Antd 编写上传文件功能 前端:使用Vue组件库 <template><div class="clearfix">< ...

  9. springboot接收文件上传_SpringBoot第十七篇:上传文件

    这篇文章主要介绍,如何在springboot工程作为服务器,去接收通过http 上传的multi-file的文件. 构建工程 为例创建一个springmvc工程你需要spring-boot-start ...

最新文章

  1. 2个422 并联一个总线_株洲新添2个全国“一村一品”示范村,一个在炎陵
  2. 简单的鼠标可拖动div 兼容IE/FF
  3. C语言实现二进制文件读写
  4. 今天做内存操作系统(xp装在内存中)
  5. ASP.NET 2.0 – 如何巢状化GridView控件
  6. 两类非常隐蔽的全表扫描,不能命中索引(一分钟系列)
  7. 游戏开发之友元类和友元函数(C++基础)
  8. Linux 实用工具vi
  9. 12款热门Java小游戏项目的源码和教程
  10. spring揭秘_理解透彻!Spring中BeanFactory与FactoryBean分析
  11. java实例化对象的四种方式
  12. MySQL字符串拼接函数介绍
  13. 汉王数据导入java环境,怎样把u盆内容导入汉王门禁考勤管理软件
  14. 深度学习入门——神经元
  15. [lighttpd]referer字段实现域名过滤及放行指定域名不走重定向
  16. VS2008的DDK环境配置DDK下载
  17. 婚姻对女人很重要,但远远不是我们的全部
  18. 股市筹码分布的实现效果
  19. 怎样查看java配置好了没_怎么配置JAVA环境变量,看完就明白了
  20. OpenCV4学习笔记(76)——基于ArUco模块+QT实现增强现实(AR)

热门文章

  1. 单向链表JAVA代码
  2. junit、hamcrest、eclemma的安装与使用
  3. ios绘图时的坐标处理
  4. 使用Google 官方的控件SwipeRefreshLayout实现下拉刷新功能
  5. jQuery UI vs Kendo UI jQuery Mobile vs Kendo UI Mobile
  6. 【转】Visual Studio 2010 架构图之用例图(UML Use Case Diagram)
  7. [转]Windows与VC命名规则
  8. Windows Phone 7 Tip (4) -- User Agent
  9. LeetCode实战:反转链表
  10. 【怎样写代码】确保对象的唯一性 -- 单例模式(一):问题案例