摘要:为例能够上传文件在服务器,你需要在中加入标签做相关的配置,但在工程中,它已经为你自动做了,所以不需要你做任何的配置。每个方法通过或者注解表明自己的方法。
这篇文章主要介绍,如何在springboot工程作为服务器,去接收通过http 上传的multi-file的文件。
构建工程为例创建一个springmvc工程你需要spring-boot-starter-thymeleaf和 spring-boot-starter-web的起步依赖。为例能够上传文件在服务器,你需要在web.xml中加入标签做相关的配置,但在sringboot 工程中,它已经为你自动做了,所以不需要你做任何的配置。
</>复制代码
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-thymeleaf
创建文件上传controller
直接贴代码:
</>复制代码
@Controller
public class FileUploadController {
private final StorageService storageService;
@Autowired
public 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:.+}")
@ResponseBody
public ResponseEntity 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
</>复制代码
上传文件大小限制
如果需要限制上传文件的大小也很简单,只需要在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/u...
源码下载https://github.com/forezp/Spr...
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/71447.html
摘要:准备阶段以上一篇文章的代码为例子,即整合,上一篇文章是基于注解来实现的数据访问层,这篇文章基于的来实现,并开启声明式事务。创建实体类数据访问层接口层用户减块用户加块,声明事务,并设计一个转账方法,用户减块,用户加块。 springboot开启事务很简单,只需要一个注解@Transactional 就可以了。因为在springboot中已经默认对jpa、jdbc、mybatis开启了事事...
摘要:首先声明下,是基于注释来生成文档的,它不基于任何框架,而且支持大多数编程语言,为了系列的完整性,所以标了个题。二准备工作安装完安装它的项目源码。输命令输入目录输出目录是我的工程名。 首先声明下,apidoc是基于注释来生成文档的,它不基于任何框架,而且支持大多数编程语言,为了springboot系列的完整性,所以标了个题。 一、apidoc简介 apidoc通过在你代码的注释来生成ap...
摘要:环境依赖创建一个新的工程,在其文件加入依赖创建一个消息接收者类,它是一个普通的类,需要注入到中。 这篇文章主要讲述如何在springboot中用reids实现消息队列。 准备阶段 安装redis,可参考我的另一篇文章,5分钟带你入门Redis。 java 1.8 maven 3.0 idea 环境依赖 创建一个新的springboot工程,在其pom文件,加入spring-boot-...
阅读 2525·2021-11-16 11:44
阅读 954·2021-09-10 11:16
阅读 2299·2019-08-30 15:54
阅读 1251·2019-08-30 15:53
阅读 1982·2019-08-30 13:00
阅读 686·2019-08-29 17:07
阅读 3583·2019-08-29 16:39
阅读 3220·2019-08-29 13:30