说明
用Spring的 MultipartFile在Springboot 中实现上传单个文件(多个文件)
多文件上传,大量文件时,防止文件名相同,重新修改存储文件名。
html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > </ head > < body > < h1 >【单文件】上传</ h1 > < form method = "post" action = "/upload.do" enctype = "multipart/form-data" > < input type = "file" name = "file" />< br />< br /> < input type = "submit" value = "上传" />< br /> </ form > < h1 >【多文件】上传</ h1 > < form method = "post" action = "/upload2.do" enctype = "multipart/form-data" > < input type = "file" name = "files" multiple = "multiple" />< br />< br /> < input type = "submit" value = "上传" />< br /> </ form > </ body > </ html > |
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | package com.example.springBootdemo.controller; import java.io.File; import java.io.IOException; import java.util.UUID; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.example.springBootdemo.utils.FileUtils; /** * 单文件、多文件上传 * @author luolei * @date 2019年1月31日 */ @RestController public class FileOperationController { /** * 【单文件】上传 * @param file * @return * @throws IllegalStateException * @throws IOException * String */ @PostMapping ( "/upload.do" ) //@RequestMapping(value="/upload.do", method = RequestMethod.POST) //上传的文件会转换成MultipartFile对象,file名字对应html中上传控件的name public String upload(MultipartFile file) throws IllegalStateException, IOException{ //取得当前上传文件的文件名称 String originalFilename = file.getOriginalFilename(); //transferTo是保存文件,参数就是要保存到的目录和名字 String filePath = "C:\\Users\\Administrator\\Desktop\\images\\" ; file.transferTo( new File(filePath + originalFilename)); System.out.println( "文件类型:" +file.getContentType()); System.out.println( "原文件名:" +originalFilename); System.out.println( "是否为空:" +file.isEmpty()); System.out.println( "文件大小:" +file.getSize()); return "文件上传完毕" ; } /** * 【多文件】上传,大量文件时,防止文件名相同,重新修改存储文件名 * @param files * @return * @throws IOException * String */ @PostMapping ( "/upload2.do" ) //@RequestMapping(value="/upload2.do", method = RequestMethod.POST) //上传的文件会转换成MultipartFile对象,file名字对应html中上传控件的name public String upload2(MultipartFile[] files) throws IOException{ if (files.length == 0 ){ return "请选择要上传的文件" ; } for (MultipartFile multipartFile : files) { if (multipartFile.isEmpty()){ return "文件上传失败" ; } byte [] fileBytes = multipartFile.getBytes(); String filePath = "C:\\Users\\Administrator\\Desktop\\images\\" ; //取得当前上传文件的文件名称 String originalFilename = multipartFile.getOriginalFilename(); //生成文件名 String fileName = UUID.randomUUID() + "&" + originalFilename; FileUtils.uploadFile(fileBytes, filePath, fileName); } return "文件上传完毕" ; } } |
FileUtils 文件上传工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.example.springBootdemo.utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * 文件工具类 * @author luolei * @date 2019年1月31日 */ public class FileUtils { /** * 文件上传方法 * @param file 文件byte[] * @param filePath 文件上传路径 * @param fileName 文件保存路径 * @throws IOException * @throws Exception * void */ public static void uploadFile( byte [] file, String filePath, String fileName) throws IOException{ File targetFile = new File(filePath); if (!targetFile.exists()){ targetFile.mkdirs(); } FileOutputStream out = new FileOutputStream(filePath + fileName); out.write(file); out.flush(); out.close(); } } |
application.properties
在Springboot中可以直接在配置文件中覆盖默认的静态资源路径的配置信息
在application.properties中配置 spring.resources.static-locations可以查看访问我们上传的文件。
1 2 3 4 5 6 7 8 9 | #单文件限制大小 spring.http.multipart.maxFileSize=10MB #一次请求限制大小 spring.http.multipart.maxRequestSize=500MB #在Springboot中可以直接在配置文件中覆盖默认的静态资源路径的配置信息 #配置静态资源目录,以逗号分隔多个目录, #加file:是因为后面指定的是一个具体的硬盘路径,其他的使用classpath指的是系统环境变量 spring.resources.static-locations=classpath: /static/ , file :C: /Users/Administrator/Desktop/images/ |