Springboot 之 多文件上传-知识林
2016-11-03 12:00
337 查看
本文章来自【知识林】
文件上传在各种网站平台上应用都非常广泛,这篇文章将讲述在Springboot中是如何完成文件上传的,Springboot是打包运行的,上传后的文件又该何去何从?
这篇文章需要用到前面所讲的知识点《Springboot 之 静态资源路径配置》和Thymeleaf相关的知识。
pom.xml
说明:页面模板还是使用Thymeleaf,文件上传工具使用Apache的
application.properties
此配置可参考《Springboot 之 静态资源路径配置》
Controller控制器
说明:
index.html
注意:
页面截图
通过上面例子上传两张图片后的效果:
示例代码:https://github.com/zsl131/spring-boot-test/tree/master/study17
本文章来自【知识林】
文件上传在各种网站平台上应用都非常广泛,这篇文章将讲述在Springboot中是如何完成文件上传的,Springboot是打包运行的,上传后的文件又该何去何从?
这篇文章需要用到前面所讲的知识点《Springboot 之 静态资源路径配置》和Thymeleaf相关的知识。
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <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> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies>
说明:页面模板还是使用Thymeleaf,文件上传工具使用Apache的
commons-io。
application.properties
此配置可参考《Springboot 之 静态资源路径配置》
server.port=1117 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false web.upload-path=D:/temp/upload/study17/ spring.mvc.static-path-pattern=/** spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,\ classpath:/static/,classpath:/public/,file:${web.upload-path}
Controller控制器
@Controller public class IndexController { //获取上传的文件夹,具体路径参考application.properties中的配置 @Value("${web.upload-path}") private String uploadPath; /** * GET请求 * 上传页面,也将显示已经存在的文件 * @param model * @return */ @GetMapping(value = "/index") public String index(Model model) { //获取已存在的文件 File [] files = new File(uploadPath).listFiles(); model.addAttribute("files", files); return "web/index"; } /** * POST请求 * @param request * @param files * @return */ @PostMapping(value = "index") public String index(HttpServletRequest request, @RequestParam("headimg")MultipartFile[] files) { //可以从页面传参数过来 System.out.println("name====="+request.getParameter("name")); //这里可以支持多文件上传 if(files!=null && files.length>=1) { BufferedOutputStream bw = null; try { String fileName = files[0].getOriginalFilename(); //判断是否有文件且是否为图片文件 if(fileName!=null && !"".equalsIgnoreCase(fileName.trim()) && isImageFile(fileName)) { //创建输出文件对象 File outFile = new File(uploadPath + "/" + UUID.randomUUID().toString()+ getFileType(fileName)); //拷贝文件到输出文件对象 FileUtils.copyInputStreamToFile(files[0].getInputStream(), outFile); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(bw!=null) {bw.close();} } catch (IOException e) { e.printStackTrace(); } } } return "redirect:/index"; } /** * 判断文件是否为图片文件 * @param fileName * @return */ private Boolean isImageFile(String fileName) { String [] img_type = new String[]{".jpg", ".jpeg", ".png", ".gif", ".bmp"}; if(fileName==null) {return false;} fileName = fileName.toLowerCase(); for(String type : img_type) { if(fileName.endsWith(type)) {return true;} } return false; } /** * 获取文件后缀名 * @param fileName * @return */ private String getFileType(String fileName) { if(fileName!=null && fileName.indexOf(".")>=0) { return fileName.substring(fileName.lastIndexOf("."), fileName.length()); } return ""; } }
说明:
@RequestParam("headimg")MultipartFile[] files这里的
headimg是根据页面上File
input的
name属性而定。
index.html
<!DOCTYPE html> <html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>文件上传</title> </head> <body> <h2>已有文件:</h2> <p th:each="file : ${files}"> [站外图片上传中……(2)],文件名称:<span th:text="${file.name}"></span> </p> <hr/> <form method="post" enctype="multipart/form-data"> 昵称:<input name="name" type="text"/> <br/> 头像:<input name="headimg" type="file"/> <br/> <button type="submit">确定上传</button> </form> </body> </html>
注意:
<input name="headimg" type="file"/>这里的
headimg决定了Controller中
@RequestParam("headimg")的值。如果有多个
<input name="headimg" type="file"/>将是多文件上传。
页面截图
通过上面例子上传两张图片后的效果:
示例代码:https://github.com/zsl131/spring-boot-test/tree/master/study17
本文章来自【知识林】
相关文章推荐
- (29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】
- 29. Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】
- spring-boot上传文件MultiPartFile获取不到文件问题解决
- spring boot文件上传、下载
- (29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot
- 解决使用Spring Boot、Multipartfile上传文件路径错误问题
- 010,spring boot 文件上传
- 29. Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】
- Spring Boot 基础知识学习(二)——配置文件多环境配置
- spring boot文件上传、下载
- Spring boot 文件上传(多文件上传)
- Spring Boot基础教程12-web应用开发-文件上传
- SpringBoot文件上传下载和多文件上传(图文详解)
- 基于spring-boot的web应用,ckeditor上传文件图片文件
- 用Spring Boot & Angular2快速开发文件上传服务
- 在spring boot中使用restTemplate上传大文件Java heap space
- spring boot 文件上传
- (29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】
- SpringBoot文件上传