This example shares the specific code of SpringMVC using MultipartFile to implement file upload for your reference. The specific content is as follows
1. Configuration file
SpringMVC uses MultipartFile to upload files, so we first need to configure MultipartResolver: for processing file in form
<!-- Configure MultipartResolver CommosMultipartResolver for file upload using spring --> <beans:bean id="multipartResolver" p:defaultEncoding="UTF-8" p:maxUploadSize="5400000" p:uploadTempDir="fileUpload/temp" > </beans:bean>
The attributes are explained in detail:
defaultEncoding="UTF-8" is the requested encoding format, default iso-8859-1
maxUploadSize="5400000" is the size of the uploaded file, in bytes
uploadTempDir="fileUpload/temp" is the temporary path to upload the file
2. Create a simple upload form
<body> <h2>File upload instance</h2> <form action="fileUpload.html" method="post" enctype="multipart/form-data"> Select file: <input type="file" name="file"> <input type="submit" value="submit"> </form> </body>
Note that you should add enctype="multipart/form-data" to the form tag to indicate that the form needs to process the file. This is the most basic thing. Many people will forget the error of looking for the program after an upload error, but forget this point.
3. Write upload control class
1. Create a control class: FileUploadController and a page that returns the result list.jsp
2. Write the action to submit the form
//Get the request of spring's default configuration through Spring's autowired annotation @Autowired private HttpServletRequest request; /*** * Upload the file with the @RequestParam annotation to specify that the file on the form is MultipartFile * * @param file * @return */ @RequestMapping("fileUpload") public String fileUpload(@RequestParam("file") MultipartFile file) { // Determine whether the file is empty if (!file.isEmpty()) { try { // File saving path String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + file.getOriginalFilename(); // TransferTo(new File(filePath)); } catch (Exception e) { e.printStackTrace(); } } // Redirect return "redirect:/list.html"; } /*** * Read all files in the uploaded file and return * * @return */ @RequestMapping("list") public ModelAndView list() { String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"; ModelAndView mav = new ModelAndView("list"); File uploadDest = new File(filePath); String[] fileNames = uploadDest.list(); for (int i = 0; i < fileNames.length; i++) { //Print out the file name System.out.println(fileNames[i]); } return mav; } 3. Use SpringMVC annotation RequestParam to specify file parameters in the form;
4. Specify a web project path to save files
5. TransferTo(File dest) method of MultipartFile to the specified path.
At this point, the basic file upload is over.
Some commonly used methods of the MultipartFile class:
String getContentType()//Get file MIME type
InputStream getInputStream()// and then go to file streaming
String getName() //Get the name of the file component in the form
String getOriginalFilename() //Get the original name of the uploaded file
long getSize() //Get the byte size of the file, unit byte
boolean isEmpty() //Is it empty
void transferTo(File dest) //Save to a target file.
4. Multiple files upload
Uploading multiple files is actually very simple. Just like uploading other same parameters, such as checkbox, use the same name in the form, and then define the MultipartFile parameter class as an array in the action.
Next implement:
1. Create a form that uploads multiple files:
<body> <h2>Upload multiple file instances</h2> <form action="filesUpload.html" method="post" enctype="multipart/form-data"> <p> Select file:<input type="file" name="files"> <p> Select file:<input type="file" name="files"> <p> Select file:<input type="file" name="files"> <p> <input type="submit" value="submit"> </form> </body>
2. Write an action to process the form and write a separate method to save the file to facilitate sharing:
/*** * Save file* @param file * @return */ private boolean saveFile(MultipartFile file) { // Determine whether the file is empty if (!file.isEmpty()) { try { // File save path String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + file.getOriginalFilename(); // TransferTo(new File(filePath)); return true; } catch (Exception e) { e.printStackTrace(); } } return false; } 3. Write action: @RequestMapping("filesUpload") public String filesUpload(@RequestParam("files") MultipartFile[] files) { //Judge the file array cannot be empty and its length is greater than 0 if(files!=null&&files.length>0){ //Loop to get the file in the file array for(int i = 0;i<files.length;i++){ MultipartFile file = files[i]; //Save the file saveFile(file); } } //Redirect return "redirect:/list.html"; }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.