资讯专栏INFORMATION COLUMN

SpringBoot非官方教程 | 第十七篇:上传文件

Galence / 3329人阅读

摘要:为例能够上传文件在服务器,你需要在中加入标签做相关的配置,但在工程中,它已经为你自动做了,所以不需要你做任何的配置。每个方法通过或者注解表明自己的方法。

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

构建工程

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

</>复制代码

  1. org.springframework.boot
  2. spring-boot-starter-web
  3. org.springframework.boot
  4. spring-boot-starter-test
  5. test
  6. org.springframework.boot
  7. spring-boot-starter-thymeleaf
创建文件上传controller

直接贴代码:

</>复制代码

  1. @Controller
  2. public class FileUploadController {
  3. private final StorageService storageService;
  4. @Autowired
  5. public FileUploadController(StorageService storageService) {
  6. this.storageService = storageService;
  7. }
  8. @GetMapping("/")
  9. public String listUploadedFiles(Model model) throws IOException {
  10. model.addAttribute("files", storageService
  11. .loadAll()
  12. .map(path ->
  13. MvcUriComponentsBuilder
  14. .fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString())
  15. .build().toString())
  16. .collect(Collectors.toList()));
  17. return "uploadForm";
  18. }
  19. @GetMapping("/files/{filename:.+}")
  20. @ResponseBody
  21. public ResponseEntity serveFile(@PathVariable String filename) {
  22. Resource file = storageService.loadAsResource(filename);
  23. return ResponseEntity
  24. .ok()
  25. .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=""+file.getFilename()+""")
  26. .body(file);
  27. }
  28. @PostMapping("/")
  29. public String handleFileUpload(@RequestParam("file") MultipartFile file,
  30. RedirectAttributes redirectAttributes) {
  31. storageService.store(file);
  32. redirectAttributes.addFlashAttribute("message",
  33. "You successfully uploaded " + file.getOriginalFilename() + "!");
  34. return "redirect:/";
  35. }
  36. @ExceptionHandler(StorageFileNotFoundException.class)
  37. public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) {
  38. return ResponseEntity.notFound().build();
  39. }
  40. }

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

GET / 获取已经上传的文件列表

GET /files/{filename} 下载已经存在于服务器的文件

POST / 上传文件给服务器

创建一个简单的 html模板

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

</>复制代码

  1. File to upload:
上传文件大小限制

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

</>复制代码

  1. spring.http.multipart.max-file-size=128KB
  2. spring.http.multipart.max-request-size=128KB
测试

测试情况如图:

参考资料

https://spring.io/guides/gs/u...

源码下载

https://github.com/forezp/Spr...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71447.html

相关文章

  • SpringBoot官方教程 | 第七篇SpringBoot开启声明式事务

    摘要:准备阶段以上一篇文章的代码为例子,即整合,上一篇文章是基于注解来实现的数据访问层,这篇文章基于的来实现,并开启声明式事务。创建实体类数据访问层接口层用户减块用户加块,声明事务,并设计一个转账方法,用户减块,用户加块。 springboot开启事务很简单,只需要一个注解@Transactional 就可以了。因为在springboot中已经默认对jpa、jdbc、mybatis开启了事事...

    tyheist 评论0 收藏0
  • SpringBoot官方教程 | 第十二篇:springboot集成apidoc

    摘要:首先声明下,是基于注释来生成文档的,它不基于任何框架,而且支持大多数编程语言,为了系列的完整性,所以标了个题。二准备工作安装完安装它的项目源码。输命令输入目录输出目录是我的工程名。 首先声明下,apidoc是基于注释来生成文档的,它不基于任何框架,而且支持大多数编程语言,为了springboot系列的完整性,所以标了个题。 一、apidoc简介 apidoc通过在你代码的注释来生成ap...

    xiaoxiaozi 评论0 收藏0
  • SpringBoot官方教程 | 第十四篇:在springboot中用redis实现消息队列

    摘要:环境依赖创建一个新的工程,在其文件加入依赖创建一个消息接收者类,它是一个普通的类,需要注入到中。 这篇文章主要讲述如何在springboot中用reids实现消息队列。 准备阶段 安装redis,可参考我的另一篇文章,5分钟带你入门Redis。 java 1.8 maven 3.0 idea 环境依赖 创建一个新的springboot工程,在其pom文件,加入spring-boot-...

    APICloud 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<