Upload analysis in spring MVC

Keywords: Mobile Spring Maven

Let's talk about the upload of spring MVC today

First of all, import should have dependency (or jar package)

 

Importing dependencies in maven looks much easier

   <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.16.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>

 

After importing the jar package or dependency, you can start to write an uploaded form

<form action="/upload" method="post" enctype="multipart/form-data"> 
		
		<input type="file" name="file" />

	</form>

Note that the request method here must be a post request

It must also be written with enctype = "multipart / form data", which means that the format of the uploaded binary stream will be the format of the specified binary stream for upload

Only when this upload format is set can the upload succeed

 

Then you need to write a configuration file for spring MVC

    <!-- Open scan -->
 	<context:component-scan base-package="com.liy.controller" />
 	
 	<!-- open mvc annotation -->
 	<mvc:annotation-driven />
 	

 	<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
		id="multipartResolver" >
		<!-- Set upload file information parameters -->
		<!-- Set the maximum size of file upload -->
		<property name="maxUploadSize">
			<value>5242880</value>
		</property>
	</bean>
 
 	<!-- Allow access to static resources for profile -->
 	<mvc:resources location="/**" mapping="/" />
 

 

Here, the id of CommonsMultipartResolver class must be "multipartResolver"“

The id of this bean is fixed. When the dispatcher servlet looks for the multipart parser, it will look for the bean with the id of multipartResolver. If the id of the defined bean is not multipartResolver, dispatcher servlet ignores it.

 

@SuppressWarnings("serial")
public class DispatcherServlet extends FrameworkServlet {

	/** Well-known name for the MultipartResolver object in the bean factory for this namespace. */
	public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver";

 

 

controller is under receiving processing

@RequestMapping("/upload")
	@ResponseBody
	public void upload(String name,String address,MultipartFile file) throws IllegalStateException, IOException{
		
		System.out.println(name+"--"+address+"--"+file.getOriginalFilename());
		
		file.transferTo(new File("d:/img/imgs/"+file.getOriginalFilename()));
		
	}

 

The name of the MultiparFile parameter here must be the same as >

 <input type="file" name="file" />

The name s here are the same

Posted by dgrinberg on Sat, 02 Nov 2019 17:12:06 -0700