How to parse springboot multi-file upload examples and how to write code
In this issue, Xiaobian will bring you about how to analyze springboot multi-file upload examples and how to write code. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.
an illustration
spingMVC supports file upload. We implement spingMVC MultipartResolver through CommonsMultipartResolver of Apach commons-fileupload package.
The example in this article is a simple multi-file upload, modified for different businesses.
2 pom.xlm
commons-fileupload commons-fileupload 1.3.3 org.springframework.boot spring-boot-starter-web
3 application.yml
spring: servlet: multipart: max-file-size: 200MB #Single file upload size max-request-size: 600MB #Continuous upload file size youku1327: file: root: path: "C:\\mydata\\generator\\version06\\" #Storage path
Four controllers
/** * @Author lsc * @Description
* @Date 2019/10/2 20:58 * @Version 1.0 */@RestControllerpublic class FileUploadController { @Value ("${youku1327.file.root.path}") private String fileRootPath; @PostMapping ("/file/upload") public String fileUpload (@RequestParam("files")MultipartFile[] files){ String filePath = ""; //multifile upload for (MultipartFile: files){ //Upload simple filename String originalFilename = file.getOriginalFilename(); //Storage path filePath = new StringBuilder (fileRootPath) .append(System.currentTimeMillis()) .append(originalFilename) .toString(); try { //save file.transferTo(new File(filePath)); } catch (IOException e) { e.printStackTrace(); } } return filePath; }}
Five start classes
/** * @Author lsc * @Description
* @Date 2019/10/2 20:54 * @Version 1.0 */@SpringBootApplicationpublic class FileUploadApplication { public static void main(String[] args) { SpringApplication.run(FileUploadApplication.class,args); }}
sixth test
Send http post request, using form, key files need to be consistent with the parameter name of MultipartFile[], select two files, after sending successfully, you will see the final file path returned;
Open the saved file path to find that the file has been uploaded.
The above is how to analyze the springboot multi-file upload example and how to write the code shared by Xiaobian. If there is a similar doubt, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.