[SpringMVC] Receipt of special request parameters-file upload

Conditions that the form for uploading files needs to meet

1) Form item type:type="file"

2) How to submit the form:method="post"

3) The enctype attribute of the form is a multi-part form:enctype="multipart/form-data"

<form action="${pageContext.request.contextPath}/test22" method="post" enctype="multipart/form-data">
	<input type="text" name="myName" />
	<input type="file" name="myFile" />
	<input type="submit" value="提交" />
</form>

 

Code implementation of single file upload

① Introduce fileupload and io coordinates (additional third-party coordinates are required for file upload)

<!-- 版本仅供参考 -->
<dependency>
    <groupId>commons-fileupload</groupId>
	<artifactId>commons-fileupload</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.3</version>
</dependency>

② Configuration file upload parser (of course it is configured in spring-mvc.xml)

<!-- 配置文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- 上传文件总大小 -->
    <property name="maxUploadSize" value="999999" />
    <!-- 上传单个文件的大小 -->
    <property name="maxUploadSizePerFile" value="999" />
    <!-- 上传文件的编码方式 -->
    <property name="defaultEncoding" value="UTF-8" />
</bean>

③ Write the specific logic of the controller (use the MultipartFile type to correspond to the reception)

@RequestMapping("/test1")
@ResponseBody
public void test1(String myName, MultipartFile myFile) throws IOException {
    
    
	// 依旧是根据名称对应接收 >_<
	// 打印对象瞧瞧,不是null说明成功
    System.out.println(myName);
    System.out.println(myFile);
}
@RequestMapping("/test2")
@ResponseBody
public void test2(String myName, MultipartFile myFile) throws IOException {
    
    
    // 获得文件的名称
    String fileName = myFile.getName();
    // 保存文件
    myFile.transferTo(new File("xxx/xxx/" + fileName));
}

 

Code implementation of multi-file upload

It is exactly the same as single file upload. The following two ideas are very simple, take a look at the difference:

Writing one:

<form action="${pageContext.request.contextPath}/test22" method="post" enctype="multipart/form-data">
    <input type="text" name="myName" />
    <input type="file" name="myFile_1" />
    <input type="file" name="myFile_2" />
    <input type="submit" value="提交" />
</form>
@RequestMapping("/test3")
@ResponseBody
public void test3(String myName, MultipartFile myFile1, MultipartFile myFile2) throws IOException {
    
    
    System.out.println(myFile1.getOriginalFilename());
    System.out.println(myFile2.getOriginalFilename());
}

Writing two:

<form action="${pageContext.request.contextPath}/test22" method="post" enctype="multipart/form-data">
    <input type="text" name="myName" />
    <input type="file" name="myFiles" />
    <input type="file" name="myFiles" />
    <input type="submit" value="提交" />
</form>
@RequestMapping("/test4")
@ResponseBody
public void test4(String myName, MultipartFile[] myFiles) throws IOException {
    
    
    for(MultipartFile file : myFiles) {
    
    
        System.out.println(file.getOriginalFilename());
    }
}

 
 
 
 

 
 
 
 

 
 
 
 

More >_<

Guess you like

Origin blog.csdn.net/m0_46202073/article/details/114373368