Spring 注解面面通 之 @RequestPart
@RequestPart
用于将multipart/form-data
类型数据映射到控制器处理方法的参数中。除了@RequestPart
注解外,@RequestParam
同样可以用于此类操作。
注解解析
① value
:
绑定的参数名称,参数值为String
类型。
② name
:
绑定的参数名称,参数值为String
类型。name
和value
可以同时使用,但两者的值需一致,否则会出现错误。
attribute 'name' and its alias 'value' are present with values of [XXX] and [XXX], but only one is permitted
1
③ required
:
请求头中是否必须包含指定的值,默认值为true
。
required
为true
时,如果请求头中缺少指定的值,则会抛出异常。
required
为false
时,如果请求头中缺少指定的值,则会返回null
。
注解示例
方法参数可以使用@RequestPart
或@RequestParam
注释,参数类型可使用org.springframework.web.multipart.MultipartFile
或javax.servlet.http.Part
。
javax.servlet.http.Part
是Servlet 3.0
提供的类,需在Web中进行配置:
① 引入Servlet-api
的3.0
版本。
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
② web.xml
中,首先需配置web.xml
版本,然后配置multipart-config
标签。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="true">
......
<servlet>
<servlet-name>springMVCServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/spring/applicationContext.mvc.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<!-- 多部分请求配置 -->
<multipart-config>
<maxFileSize>20480000</maxFileSize>
</multipart-config>
</servlet>
......
</web-app>
③ 配置Spring的多部分请求解析器,例如applicationContext.mvc.xml中增加配置。
<bean id="multipartResolver"
class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>
1) 建Controller
,用来演示@RequestPart
和@RequestParam
使用方法。
package com.arhorchin.securitit.webannotations;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Securitit.
* @note 演示@RequestPart使用方法.
*/
@Controller
@RequestMapping("/WebAnnotations")
public class RequestPartController {
/**
* logger.
*/
private Logger logger = LoggerFactory.getLogger(RequestPartController.class);
/**
* 跳转页面.
*/
@RequestMapping(
value = "/RequestPart.html",
method = RequestMethod.GET)
public ModelAndView requestPartHtml(HttpServletRequest req, HttpServletResponse res, ModelMap modelMap)
throws Exception {
return new ModelAndView("webannotations/RequestPart", modelMap);
}
/**
* RequestPart-MultipartFile接收参数.
*/
@ResponseBody
@RequestMapping(
value = "/requestPartMultipartFile.do",
method = RequestMethod.POST)
public String requestPartMultipartFile(@RequestPart("file") MultipartFile multipartFile) throws Exception {
logger.info("Current value of RequestPart [MultipartFile] : name is [" + multipartFile.getOriginalFilename()
+ "], file length is [" + multipartFile.getSize() + "]");
return "Current value of RequestPart [MultipartFile] : name is [" + multipartFile.getOriginalFilename() + "], file length is ["
+ multipartFile.getSize() + "]";
}
/**
* RequestPart-Part接收参数.
*/
@ResponseBody
@RequestMapping(
value = "/requestPartPart.do",
method = RequestMethod.POST)
public String requestPartPart(@RequestPart("file") Part part) throws Exception {
logger.info(
"Current value of RequestPart [Part] : name is [" + part.getName() + "], file length is [" + part.getSize() + "]");
return "Current value of RequestPart [Part] : name is [" + part.getName() + "], file length is [" + part.getSize() + "]";
}
/**
* RequestParam-MultipartFile接收参数.
*/
@ResponseBody
@RequestMapping(
value = "/requestParamMultipartFile.do",
method = RequestMethod.POST)
public String requestParamMultipartFile(@RequestParam("file") MultipartFile multipartFile) throws Exception {
logger.info("Current value of RequestParam [MultipartFile] : name is [" + multipartFile.getOriginalFilename()
+ "], file length is [" + multipartFile.getSize() + "]");
return "Current value of RequestParam [MultipartFile] : name is [" + multipartFile.getOriginalFilename() + "], file length is ["
+ multipartFile.getSize() + "]";
}
/**
* RequestParam-Part接收参数.
*/
@ResponseBody
@RequestMapping(
value = "/requestParamPart.do",
method = RequestMethod.POST)
public String requestParamPart(@RequestParam("file") Part part) throws Exception {
logger.info(
"Current value of RequestParam [Part] : name is [" + part.getName() + "], file length is [" + part.getSize() + "]");
return "Current value of RequestParam [Part] : name is [" + part.getName() + "], file length is [" + part.getSize() + "]";
}
/**
* Part接收参数.
*/
@ResponseBody
@RequestMapping(
value = "/part.do",
method = RequestMethod.POST)
public String part(HttpServletRequest req) throws Exception {
Part part = req.getPart("file");
logger.info(
"Current value of Part : name is [" + part.getName() + "], file length is [" + part.getSize() + "]");
return "Current value of Part : name is [" + part.getName() + "], file length is [" + part.getSize() + "]";
}
}
2) 建html
,用来演示RequestPartController
示例。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Author" content="Securitit">
<meta name="Keywords" content="-">
<meta name="Description" content="Securitit @RequestPart 测试页面">
<title>@RequestPart 测试页面</title>
</head>
<body>
<div style="float: left; width: 520px; border: 1px solid #d8b1b1; padding: 20px 20px;">
<h1 style="margin-top: -10px; text-align: center;">@RequestPart注解</h1>
<div style="border: 1px solid #d8b1b1; padding: 10px 10px; padding-bottom: 30px; width: 500px;">
<h1>RequestPart-MultipartFile 接收参数</h1>
<form action="http://localhost:9199/spring-annotations/WebAnnotations/requestPartMultipartFile.do"
method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" style="width: 100px; background-color: #e6dddd; margin-top: 30px; margin-left: 200px;"/>
</form>
</div>
<div style="border: 1px solid #d8b1b1; padding: 10px 10px; padding-bottom: 30px; width: 500px; margin-top: 50px;">
<h1>RequestPart-Part 接收参数</h1>
<form action="http://localhost:9199/spring-annotations/WebAnnotations/requestPartPart.do"
method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" style="width: 100px; background-color: #e6dddd; margin-top: 30px; margin-left: 200px;"/>
</form>
</div>
</div>
<div style="float: left; width: 520px; border: 1px solid #d8b1b1; padding: 20px 20px; margin-left: 50px;">
<h1 style="margin-top: -10px; text-align: center;">@RequestParam注解</h1>
<div style="border: 1px solid #d8b1b1; padding: 10px 10px; padding-bottom: 30px; width: 500px;">
<h1>RequestParam-MultipartFile 接收参数</h1>
<form action="http://localhost:9199/spring-annotations/WebAnnotations/requestParamMultipartFile.do"
method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" style="width: 100px; background-color: #e6dddd; margin-top: 30px; margin-left: 200px;"/>
</form>
</div>
<div style="border: 1px solid #d8b1b1; padding: 10px 10px; padding-bottom: 30px; width: 500px; margin-top: 50px;">
<h1>RequestParam-Part 接收参数</h1>
<form action="http://localhost:9199/spring-annotations/WebAnnotations/requestParamPart.do"
method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" style="width: 100px; background-color: #e6dddd; margin-top: 30px; margin-left: 200px;"/>
</form>
</div>
</div>
</body>
</html>
3) 启动服务,访问http://localhost:9199/spring-annotations/WebAnnotations/RequestPart.html
页面。
① @RequestPart注解-RequestPart-MultipartFile 接收参数
② @RequestPart注解-RequestPart-Part 接收参数
③ @RequestParam注解-RequestPart-MultipartFile 接收参数
④ @RequestParam注解-RequestPart-Part 接收参数
总结
@RequestPart
和@RequestParam
都可以处理multipart/form-data
类型数据,本质上并没有区别,使用任何一个都能达到期望的效果。文件上传在实际应用中也是必不可少的。
若文中存在错误和不足,欢迎指正!
上一篇: laravel自带邮箱发送
下一篇: 设计模式-状态模式练习