English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article shares the specific code for implementing file upload and download functions in spring mvc, for your reference. The specific content is as follows
File upload
Introduce spring mvc and commons in pom.xml-related jar for fileupload
<!-- spring mvc --> dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.13.RELEASE</version> </dependency> <!-- File upload and download --> dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency>
Add file upload related configurations in the springmvc.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- The maximum file upload size, unit in bytes (10MB) --> <property name="maxUploadSize"> <value>10485760</value> </property> <!-- The request encoding format must be consistent with the jSP's pageEncoding attribute to correctly read the form content, the default is ISO-8859-1 --> <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean>
Add a form tag in the jsp file
<form action="upload" enctype="multipart/form-data" method="post"> <table> <tr> <td>File description: </td> <td><input type="text" name="description"></td> </tr> <tr> <td>Select file: </td> <td><input type="file" name="file"></td> </tr> <tr> <td><input type="submit" value="Upload"></td> </tr> </table> </form>
Add a file upload method
//The uploaded file will be automatically bound to MultipartFile @RequestMapping(value = "/upload", method = RequestMethod.POST) public String upload(HttpServletRequest request, @RequestParam("description") String description, @RequestParam("file") MultipartFile file) throws Exception { //If the file is not empty, write the upload path if(!file.isEmpty()) { //Uploaded file path String path = request.getServletContext().getRealPath("/file/"); //Uploaded file name String filename = file.getOriginalFilename(); File filepath = new File(path,filename); //Check if the path exists, and create one if it does not if (!filepath.getParentFile().exists()) { filepath.getParentFile().mkdirs(); } //Save the uploaded file to a target file file.transferTo(new File(path + File.separator + filename)); return "success"; } return "error"; } }
That's all for this article. I hope it will be helpful to everyone's learning and that everyone will support the Yelling Tutorial more.
Declaration: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)