解决SpringMVC接收前台上传文件为null

来源:互联网 发布:淘宝买电脑可靠吗 编辑:程序博客网 时间:2024/06/05 04:35

这个问题的需求来源于,我前端传递图片后,需要在Controller中通过name映射进行获取,在获取的过程中出现了获取他的值为null的状况,发生这种状况的情形会有很多种,这边不一一列举,主要告诉大家如何正确获取。

1.前端界面

需要注意的是,因为这边会引用jquery.form.js,所以在form中一定要加入enctype="multipart/form-data"属性

<form id="jvForm" action="add.do" method="post" enctype="multipart/form-data"><table cellspacing="1" cellpadding="2" width="100%" border="0" class="pn-ftable"><tbody><tr><td width="20%" class="pn-flabel pn-flabel-h"><span class="pn-frequired">*</span>品牌名称:</td><td width="80%" class="pn-fcontent"><input type="text" class="required" name="name" maxlength="100"/></td></tr><tr><td width="20%" class="pn-flabel pn-flabel-h"><span class="pn-frequired">*</span>上传商品图片(90x150尺寸):</td><td width="80%" class="pn-fcontent">注:该尺寸图片必须为90x150。</td></tr><tr><td width="20%" class="pn-flabel pn-flabel-h"></td><td width="80%" class="pn-fcontent"><img width="100" height="100" id="allUrl"/><input type="file" onchange="uploadPic()" name="pic"/></td></tr><tr><td width="20%" class="pn-flabel pn-flabel-h">品牌描述:</td><td width="80%" class="pn-fcontent"><input type="text" class="required" name="description" maxlength="80"  size="60"/></td></tr><tr><td width="20%" class="pn-flabel pn-flabel-h">排序:</td><td width="80%" class="pn-fcontent"><input type="text" class="required" name="sort" maxlength="80"/></td></tr><tr><td width="20%" class="pn-flabel pn-flabel-h">是否可用:</td><td width="80%" class="pn-fcontent"><input type="radio" name="isDisplay" value="1" checked="checked"/>可用<input type="radio" name="isDisplay" value="0"/>不可用</td></tr></tbody><tbody><tr><td class="pn-fbutton" colspan="2"><input type="submit" class="submit" value="提交"/>   <input type="reset" class="reset" value="重置"/></td></tr></tbody></table></form>

2.jquery部分

需要引入jquery,以及jquery.form

<script type="text/javascript">//上传照片function uploadPic(){var options={url:"/upload/uploadPic.do",dataType:"json",type:"post",success:function(data){}};//jquery.form使用方式$("#jvForm").ajaxSubmit(options);}</script>

3.开启SpringMVC文件上传功能

SpringMVC的文件上传功能默认是关闭的,所以需要开启

(1)首先需要引入相关的jar包

 <dependency>      <groupId>commons-fileupload</groupId>      <artifactId>commons-fileupload</artifactId>      <version>1.3.1</version>  </dependency>  

(2)其次需要在spring-mvc.xml中进行配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><context:component-scan base-package="com.gwd.shopping" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>        <!-- 上传图片 -->    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    <!-- 上传尺寸为B ,这里是1M -->     <property name="maxUploadSize" value="1048576"/>    </bean><bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/console/"/><property name="suffix" value=".jsp"/></bean></beans>

4.Controller的编写

这里pic名称需要与前端中file的名称要一致,且要配置@RequestParam(value="pic",required=false,楼主就是因为一开始没有配置value=“pic”,导致获取初始名称一直为null

@Controller@RequestMapping(value="/upload")public class UploadController {@RequestMapping(value="/uploadPic.do")//false表示不是必须传递该值public void uploadPic(@RequestParam(value="pic",required=false)MultipartFile pic){System.out.println(pic.getOriginalFilename());} }


阅读全文
1 0
原创粉丝点击