先分享点小福利给程序员,阿里云的优惠券,购买服务器更便宜
点我获取阿里云优惠券
multipart/form-data
Spring MVC中关于关于Content-Type类型信息的使用
首先我们来看看RequestMapping中的Class定义:
[html] view plain copy
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String[] value() default {};
RequestMethod[] method() default {};
String[] params() default {};
String[] headers() default {};
String[] consumes() default {};
String[] produces() default {};
}
value: 指定请求的实际地址, 比如 /action/info之类。
method: 指定请求的method类型, GET、POST、PUT、DELETE等
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回
params: 指定request中必须包含某些参数值是,才让该方法处理
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求
其中,consumes, produces使用content-typ信息进行过滤信息;headers中可以使用content-type进行过滤和判断。
3. 使用示例
3.1 headers
[html] view plain copy
@RequestMapping(value = "/test", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/")
public void testHeaders(@PathVariable String ownerId, @PathVariable String petId) {
// implementation omitted
}
这里的Headers里面可以匹配所有Header里面可以出现的信息,不局限在Referer信息。
public static void stream2Multi() throws Exception {
// 读入 文件
File file = new File("E:\\模版0720.xls");
FileInputStream in_file = new FileInputStream(file);
// 转 MultipartFile
MultipartFile multi = new MockMultipartFile("模板.xls", in_file);
String name = multi.getOriginalFilename();
// 创建文件夹
String dire = "E:/文件/picture/file.xls";
File file_dire = new File(dire);
if (!file_dire.exists()) {
file_dire.createNewFile();
}
//写入文件
multi.transferTo(file_dire);
}
org.springframework.web.multipart.MultipartException: Current request is not a multipart request
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:190)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:109)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
Caused by: java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
at org.apache.catalina.connector.Request.parseParts(Request.java:2874)
at org.apache.catalina.connector.Request.parseParameters(Request.java:3177)
at org.apache.catalina.connector.Request.getParameter(Request.java:1110)
at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75)
... 30 more
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
我的官网
我的官网http://guan2ye.com
我的CSDN地址http://blog.csdn.net/chenjianandiyi
我的简书地址http://www.jianshu.com/u/9b5d1921ce34
我的githubhttps://github.com/javanan
我的码云地址https://gitee.com/jamen/
阿里云优惠券https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=vf2b5zld&utm_source=vf2b5zld