Spring MVC file upload)
SpringMVC-day02
- Spring MVC file upload
- 1. Use maven to introduce two more jars' commons-io-2.6. Jar commons-fileupload-1.3.3. Jar based on the Spring jar package and spring MVC jar package.`
- 2. Spring MVC needs to configure processor mapper, processor adapter, view parser and automatic scanning in spring mvc.xml.
- 3. For spring MVC file upload, you need to upload the configuration file in spring mvc.xml.
- 4. web.xml configuration
- 5. Write the UI interface fileUpload.jsp for file upload
- 6. Write FileUploadController
Spring MVC file upload
1. Use maven to introduce two jar commons-io-2.6.jar commons-fileupload-1.3.3.jar based on Spring jar package and spring MVC jar package.
2. Spring MVC needs to configure processor mapper, processor adapter, view parser and automatic scanning in spring mvc.xml.
<!-- To configure SpringMVC You need to configure the processor mapper, processor adapter, view parser, and scan the corresponding controller package --> <mvc:annotation-driven /><!--Configure processor mapper, processor adapter --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> <context:component-scan base-package=""></context:component-scan>
3. For spring MVC file upload, you need to upload the configuration file in spring mvc.xml.
The specific configuration of SpringMVC.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- To configure SpringMVC You need to configure the processor mapper, processor adapter, view parser, and scan the corresponding controller package --> <mvc:annotation-driven /><!--Configure processor mapper, processor adapter --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> <context:component-scan base-package=""></context:component-scan> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"></property> <property name="maxUploadSize" value="1048576"></property> </bean> <!-- Static resources do not need to be intercepted --> <mvc:resources location="/" mapping="/**/*.*"></mvc:resources> </beans>
4. web.xml configuration
<!-- To configure DispatcherServlet --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:SpringMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Chinese garbled --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
5. Write the UI interface fileUpload.jsp for file upload
-
method="post" enctype="multipart/form-data" type="file"
6. Write FileUploadController
@Controller public class FileUploadController { //If the function parameter name is the same as the UI name, you do not need to configure @ RequestParam. Otherwise, the value value of @ RequestParam should be the same as the UI name. @RequestMapping("/fileUpload") public String upload(@RequestParam("fileUpload") MultipartFile file) throws Exception, IOException { //1. Get the file name String fileName=file.getOriginalFilename(); String savePath="F://fileUpload"; //1. Generally, the file is not saved on the project or server, but on the local disk. //2. The address path of the general file to be saved in the database. Generally, the content of the file is not saved. //3. It is better not to put all the files under one folder. You can create a folder and store the files according to certain rules. File filePath=new File(savePath); if(!filePath.exists()) {//Create file if file does not exist filePath.mkdir();//create folder } //Destination address of the document File dest=new File(savePath+File.separator+fileName); System.out.println(savePath+File.separator+fileName); file.transferTo(dest);//Go to the file as dest return "success"; } }
Multi file upload
@RequestMapping("/fileUpload") public String upload(@RequestParam("fileUpload") MultipartFile files[]) throws Exception, IOException { String savePath = "F://fileUpload"; File filePath = new File(savePath); if (!filePath.exists()) {// Create file if file does not exist filePath.mkdir();// create folder } for (MultipartFile file : files) { File dest = new File(savePath + File.separator + file.getOriginalFilename()); System.out.println(savePath + File.separator + file.getOriginalFilename()); file.transferTo(dest); } return "success"; }