SpringMVC 注解(三) requestHeader,requestPart

@RequestHeader

 

类似requestbody和requestparam 不过请求的参数必须是 在header 信息中,否则会报400,错误,因为默认 required属性为true

 

@RequestPart

 

支持对multipart/form-data 类型的参数进行参数绑定,支持的参数类型有 :MultipartFile,MulitpartResolver , Part 等参数

对于其他任意的参数类型,类似于 requestBody 对参数的解析

注意:requestParam 类型对于 multipart/form-data  也支持参数解析,区别在于处理字符串时不同

示例:

package com.game.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping(value="test",produces="text/html;charset=UTF-8")
public class TestController {
	
	@RequestMapping("one")
	@ResponseBody
	public String test(HttpServletRequest req,HttpServletResponse resp,@RequestPart String tk){
		System.out.println("tk="+tk);
		return "ok";
	}
	
	
	
	//文件上传
	@RequestMapping("uploadFile")
	@ResponseBody
	public void uploadFile(HttpServletRequest req,HttpServletResponse resp,@RequestPart("test") MultipartFile file) throws IllegalStateException, IOException{
		System.out.println("ok");
		System.out.println(file);
		String fileName = file.getOriginalFilename();
		System.out.println("fileName="+fileName);
		String path = req.getSession().getServletContext().getRealPath("upload");  
		System.out.println("path="+path);
		File seFile = new File(path,fileName);
		file.transferTo(seFile);
		return;
	}
	
	
}

注意:

如果@RequestPart 注解的参数类型是MultipartFile 就必须配置 multipartResolver 解析器, 如果@RequestPart 注解的参数类型是Servlet 3.0 规范的 Part 参数,就不必配置multipartResolver 解析器

这里@RequestPart 注解的是MultipartFile必须配置 multipartResolver 解析器,

否则为报500 Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided

一般我们都是在application-mvc.xml 中配置,可是我现在是使用的非xml文件启动的springmvc (参考:https://blog.csdn.net/kzcming/article/details/80741743),可以在WebConfig 中新增 multipartResolver 的配置,代码如下:

package before.springmvc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * 配置 SpringMVC 视图解析类WebConfig ,需要继承自 WebMvcConfigureAdapter
 * 
 * 类似于application-mvc.xml 文件,此文件的配置大部分都可在此类中配置
 * @author Administrator
 *
 */

@Configuration
@EnableWebMvc //启动 springMVC
@ComponentScan(value = {"com.game.controller"})	//配置组件扫描
public class WebConfig extends WebMvcConfigurerAdapter{
	
	//配置jsp 视图解析器
	@Bean
	public ViewResolver viewResolver(){
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/view");//设置预加载路径/目录
		resolver.setSuffix(".jsp");  //设置允许后缀 //返回时也自动匹配到该后缀
		resolver.setExposeContextBeansAsAttributes(true);
		return resolver;
	}
	
	//配置静态资源的处理
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();//对静态资源的请求转发到容器缺省的servlet,而不使用 DispatcherServlet
	}
	
	//配置MultipartResolver 解析器
	@Bean
	public MultipartResolver multipartResolver(){
		CommonsMultipartResolver multipartRe = new CommonsMultipartResolver();
		multipartRe.setMaxUploadSize(1024000000);
		multipartRe.setResolveLazily(true);
		multipartRe.setMaxInMemorySize(4096);
		multipartRe.setDefaultEncoding("UTF-8");//设置默认字符集
		return multipartRe;
		
	}

}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值