springmvc处理图片上传时使用MultipartFile 接收为null。

新手上路,请多包涵

1.使用富文本编辑器上传图片,在controller中使用MultipartFile接收为null。debug发现,request经过dispatcherServlet是有值的。而且可以正常映射到方法中去。
2.springmvc中的配置:


<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- 设定默认编码 -->
    <property name="defaultEncoding" value="UTF-8"></property>
    <!-- 设定文件上传的最大值5MB,5*1024*1024 -->
    <property name="maxUploadSize" value="5242880"></property>
</bean>

3.DEBUG信息:
DEBUG [http-bio-90-exec-10] - DispatcherServlet with name 'web' processing GET request for [/js/kindeditor-4.1.10/lang/zh_CN.js]
DEBUG [http-bio-90-exec-10] - Looking up handler method for path /js/kindeditor-4.1.10/lang/zh_CN.js
DEBUG [http-bio-90-exec-10] - Did not find handler method for [/js/kindeditor-4.1.10/lang/zh_CN.js]
DEBUG [http-bio-90-exec-10] - Matching patterns for request [/js/kindeditor-4.1.10/lang/zh_CN.js] are [/js/**]
DEBUG [http-bio-90-exec-10] - URI Template variables for request [/js/kindeditor-4.1.10/lang/zh_CN.js] are {}
DEBUG [http-bio-90-exec-10] - Mapping [/js/kindeditor-4.1.10/lang/zh_CN.js] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/js/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@105fda6b]]] and 1 interceptor
DEBUG [http-bio-90-exec-10] - Last-Modified value for [/js/kindeditor-4.1.10/lang/zh_CN.js] is: -1
DEBUG [http-bio-90-exec-10] - Null ModelAndView returned to DispatcherServlet with name 'web': assuming HandlerAdapter completed request handling
DEBUG [http-bio-90-exec-10] - Successfully completed request
DEBUG [http-bio-90-exec-10] - DispatcherServlet with name 'web' processing GET request for [/js/kindeditor-4.1.10/plugins/multiimage/images/swfupload.swf]
DEBUG [http-bio-90-exec-10] - Looking up handler method for path /js/kindeditor-4.1.10/plugins/multiimage/images/swfupload.swf
DEBUG [http-bio-90-exec-10] - Did not find handler method for [/js/kindeditor-4.1.10/plugins/multiimage/images/swfupload.swf]
DEBUG [http-bio-90-exec-10] - Matching patterns for request [/js/kindeditor-4.1.10/plugins/multiimage/images/swfupload.swf] are [/js/**]
DEBUG [http-bio-90-exec-10] - URI Template variables for request [/js/kindeditor-4.1.10/plugins/multiimage/images/swfupload.swf] are {}
DEBUG [http-bio-90-exec-10] - Mapping [/js/kindeditor-4.1.10/plugins/multiimage/images/swfupload.swf] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/js/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@105fda6b]]] and 1 interceptor
DEBUG [http-bio-90-exec-10] - Last-Modified value for [/js/kindeditor-4.1.10/plugins/multiimage/images/swfupload.swf] is: -1
DEBUG [http-bio-90-exec-10] - Null ModelAndView returned to DispatcherServlet with name 'web': assuming HandlerAdapter completed request handling
DEBUG [http-bio-90-exec-10] - Successfully completed request
DEBUG [http-bio-90-exec-10] - DispatcherServlet with name 'web' processing POST request for [/pic/upload]
DEBUG [http-bio-90-exec-10] - Found multipart file [uploadFile] of size 345594 bytes with original filename [natalie.png], stored at [D:ideaWorkspace2e3mallwebtargettomcatworkTomcatlocalhost_upload_605390ea_b9db_44ee_9bf4_b426b27db686_00000001.tmp]
DEBUG [http-bio-90-exec-10] - Looking up handler method for path /pic/upload
DEBUG [http-bio-90-exec-10] - Returning handler method [public java.util.Map cn.e3mall.controller.PictureController.fileUpload(org.springframework.web.multipart.MultipartFile)]
DEBUG [http-bio-90-exec-10] - Returning cached instance of singleton bean 'pictureController'
DEBUG [http-bio-90-exec-10] - Skip CORS processing, request is a same-origin one

阅读 11.9k
2 个回答
新手上路,请多包涵

发完一分钟就意识到了这犯的是低级错误,在controller的方法形参类型虽然为MultipartFile,但参数名必须为uploadFile,否则无法映射成功,自然接收不到request域中的对象,果然不要出了什么事情立马去网上找答案。很多做法完全是背道而驰。
谨以此为戒。

新手上路,请多包涵

我们学的时候说可以改变参数名啊 现在都是attach 实现信息新增的时候可以上传成功 但是在修改信息的时候 将原来的旧图片删除 重新新增一个图片的时候就获取不到值了 是怎么回事 求大神指教

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题