This article shares the specific code for Spring boot file upload for your reference. The specific content is as follows
1. Create a Maven web project, then configure the pom.xml file, and add dependencies:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.0.2.RELEASE</version> </dependency>
2. Enter a form in the index.jsp file in the webapp directory:
<html> <body> <form method="POST" enctype="multipart/form-data" action="/upload"> File to upload: <input type="file" name="file"><br /> Name: <input type="text" name="name"><br /> <br /> <input type="submit" value="Upload"> Press here to upload the file! </form> </body>
This form is our simulated upload page
3. Write a Controller that processes this form:
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; @Controller public class FileUploadController { @RequestMapping(value="/upload", method=RequestMethod.GET) public @ResponseBody String providesUploadInfo() { return "You can upload a file by posting to this same URL."; } @RequestMapping(value="/upload", method=RequestMethod.POST) public @ResponseBody String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file){ if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded"))); stream.write(bytes); stream.close(); return "You successfully uploaded " + name + " into " + name + "-uploaded !"; } catch (Exception e) { return "You failed to upload " + name + " => " + e.getMessage(); } } else { return "You failed to upload " + name + " because the file was empty."; } } }4. Then we have some restrictions on the uploaded files and write the main method to start the web:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.embedded.MultiPartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import javax.servlet.MultipartConfigElement; @Configuration @ComponentScan @EnableAutoConfiguration public class Application { @Bean public MultipartConfigElement multipartConfigElement() { MultiPartConfigFactory factory = new MultiPartConfigFactory(); factory.setMaxFileSize("128KB"); factory.setMaxRequestSize("128KB"); return factory.createMultipartConfig(); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }5. Then visit http://localhost:8080/upload to see the page.
The above example implements the function of uploading a single file. Assuming that we want to implement the function of batch uploading of files now, we only need to simply modify the above code. Considering the length issue, the following is just a different code from the above, and the descriptions are not posted as above. :
1. Add batchUpload.jsp file
<html> <body> <form method="POST" enctype="multipart/form-data" action="/batch/upload"> File to upload: <input type="file" name="file"><br /> File to upload: <input type="file" name="file"><br /> <input type="submit" value="Upload"> Press here to upload the file! </form> </body> </html>
2. Add the BatchFileUploadController.java file:
import org.springframework.steretype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.List; /** * Created by wenchao.ren on 2014/4/26. */ @Controller public class BatchFileUploadController { @RequestMapping(value="/batch/upload", method= RequestMethod.POST) public @ResponseBody String handleFileUpload(HttpServletRequest request){ List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file"); for (int i =0; i< files.size(); ++i) { MultipartFile file = files.get(i); String name = file.getName(); if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name + i))); stream.write(bytes); stream.close(); } catch (Exception e) { return "You failed to upload " + name + " => " + e.getMessage(); } } else { return "You failed to upload " + name + " because the file was empty."; } } return "upload successful"; } }Such a simple batch upload function is OK, isn't it very simple?
Note: The above code is just for demonstration, so the encoding style adopts a casual approach, and it is not recommended that everyone imitate it.
Reference: MultipartResolver implements file upload function
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.
1. MultipartResolver can also implement file upload function. Reference article: