CommonsMultipartFile接收不了图片文件
来源:4-13 前后端联调验证整体模块功能
火水哥
2019-07-23
CommonsMultipartFile shopImg = null;//接收图片的对象
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext()
);//文件上传解析器,解析request里面的文件信息:通过本次会话当中的上下文去获取相关文件上传的内容
if (commonsMultipartResolver.isMultipart(request)) {//判断request里面是否有上传的文件流
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg");//获取图片
} else {
modelMap.put("success", false);
modelMap.put("errMsg", "上传图片不能为空");
return modelMap;
}![图片描述](http://img1.sycdn.imooc.com/szimg/5d3690ad09de840209840562.jpg)
![图片描述](http://img.mukewang.com/szimg/5d36c11709dca14507930248.jpg)
老师前台shopImg有数据,我后台用request.getParameter(shopImg)取值为null,
request.getParameter(verifyCodeActual)取验证码的有值,这个问题弄了一个晚上和一个上午没弄明白,配置文件我是用@Configuration在里面注册bean写的
@Bean(value = "multipartResolver")
public MultipartResolver multipartResolver(){
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
//resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常
resolver.setResolveLazily(true);
resolver.setMaxInMemorySize(40960);
//上传文件大小 50M 50*1024*1024
resolver.setMaxUploadSize(50*1024*1024);
return resolver;
}
3回答
-
翔仔
2019-07-24
2.0里面是这样的
package com.imooc.o2o.config.web; import javax.servlet.ServletException; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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.InterceptorRegistration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.view.InternalResourceViewResolver; import com.google.code.kaptcha.servlet.KaptchaServlet; import com.imooc.o2o.interceptor.shopadmin.ShopLoginInterceptor; import com.imooc.o2o.interceptor.shopadmin.ShopPermissionInterceptor; import com.imooc.o2o.interceptor.superadmin.SuperAdminLoginInterceptor; /** * 开启Mvc,自动注入spring容器。 WebMvcConfigurerAdapter:配置视图解析器 * 当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean * * @author xiangze * */ @Configuration // 等价于<mvc:annotation-driven/> @EnableWebMvc public class MvcConfiguration implements WebMvcConfigurer, ApplicationContextAware { // Spring容器 private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } /** * 静态资源配置 * * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/"); registry.addResourceHandler("/upload/**").addResourceLocations("file:/Users/baidu/work/image/upload/"); } /** * 定义默认的请求处理器 */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } /** * 创建viewResolver * * @return */ @Bean(name = "viewResolver") public ViewResolver createViewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); // 设置Spring 容器 viewResolver.setApplicationContext(this.applicationContext); // 取消缓存 viewResolver.setCache(false); // 设置解析的前缀 viewResolver.setPrefix("/WEB-INF/html/"); // 设置试图解析的后缀 viewResolver.setSuffix(".html"); return viewResolver; } /** * 文件上传解析器 * * @return */ @Bean(name = "multipartResolver") public CommonsMultipartResolver createMultipartResolver() { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); multipartResolver.setDefaultEncoding("utf-8"); // 1024 * 1024 * 20 = 20M multipartResolver.setMaxUploadSize(20971520); multipartResolver.setMaxInMemorySize(20971520); return multipartResolver; } @Value("${kaptcha.border}") private String border; @Value("${kaptcha.textproducer.font.color}") private String fcolor; @Value("${kaptcha.image.width}") private String width; @Value("${kaptcha.textproducer.char.string}") private String cString; @Value("${kaptcha.image.height}") private String height; @Value("${kaptcha.textproducer.font.size}") private String fsize; @Value("${kaptcha.noise.color}") private String nColor; @Value("${kaptcha.textproducer.char.length}") private String clength; @Value("${kaptcha.textproducer.font.names}") private String fnames; /** * 由于web.xml不生效了,需要在这里配置Kaptcha验证码Servlet */ @Bean public ServletRegistrationBean<KaptchaServlet> servletRegistrationBean() throws ServletException { ServletRegistrationBean<KaptchaServlet> servlet = new ServletRegistrationBean<KaptchaServlet>(new KaptchaServlet(), "/Kaptcha"); servlet.addInitParameter("kaptcha.border", border);// 无边框 servlet.addInitParameter("kaptcha.textproducer.font.color", fcolor); // 字体颜色 servlet.addInitParameter("kaptcha.image.width", width);// 图片宽度 servlet.addInitParameter("kaptcha.textproducer.char.string", cString);// 使用哪些字符生成验证码 servlet.addInitParameter("kaptcha.image.height", height);// 图片高度 servlet.addInitParameter("kaptcha.textproducer.font.size", fsize);// 字体大小 servlet.addInitParameter("kaptcha.noise.color", nColor);// 干扰线的颜色 servlet.addInitParameter("kaptcha.textproducer.char.length", clength);// 字符个数 servlet.addInitParameter("kaptcha.textproducer.font.names", fnames);// 字体 return servlet; } }
不知道能否帮到同学
042019-07-24 -
翔仔
2019-07-23
同学好,首先接收shopImg的方式不对,我在Springmvc里,是通过如下方式接收的
CommonsMultipartFile shopImg = null; CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); if (commonsMultipartResolver.isMultipart(request)) { MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request; shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg"); } else { modelMap.put("success", false); modelMap.put("errMsg", "上传图片不能为空"); return modelMap; }
其次,同学在Springboot里面这样的配置方式不知道springboot会不会加载,
可以试试这个
https://blog.csdn.net/linzhiqiang0316/article/details/77016997
springboot我没做过尝试,同学可以尝试一下
012019-07-24 -
黑白_
2019-07-23
同学,你是不是没有配置文件上传解析器啊
<!-- 文件上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<!-- 1024 * 1024 * 20 = 20M -->
<property name="maxUploadSize" value="20971520"></property>
<property name="maxInMemorySize" value="20971520"></property>
</bean>
032019-10-21
相似问题