springmvc+ajax上传图片的问题。传过去的是空值,这是为什么?

html代码



邮箱*



账号*



密码*



昵称*



介绍*



头像*







-----------------------------------------------------------------------------------
js代码:
<br> $(function () {<br> $(&quot;#btn&quot;).click(function () {</p> <pre><code> var f = $(&quot;#file&quot;).val(); if(f==null||f==&quot;&quot;){ $(&quot;#picTip&quot;).html(&quot;&lt;span style=&#39;color:Red&#39;&gt;错误提示:上传文件不能为空,请重新选择文件&lt;/span&gt;&quot;); }else{ var extname = f.substring(f.lastIndexOf(&quot;.&quot;)+1,f.length); extname = extname.toLowerCase();//处理了大小写 if(extname!= &quot;jpeg&quot;&amp;&amp;extname!= &quot;jpg&quot;&amp;&amp;extname!= &quot;gif&quot;&amp;&amp;extname!= &quot;png&quot;){ $(&quot;#picTip&quot;).html(&quot;&lt;span style=&#39;color:Red&#39;&gt;错误提示:格式不正确,支持的图片格式为:JPEG、GIF、PNG!&lt;/span&gt;&quot;); } } adduser(); }); }); function adduser(){ var email=$(&quot;#email&quot;).val(); var account=$(&quot;#account&quot;).val(); var password=$(&quot;#password&quot;).val(); var nickname=$(&quot;#nickname&quot;).val(); var introduce=$(&quot;#introduce&quot;).val(); var formData = new FormData($(&quot;#uploadForm&quot;)[0]); alert(formData) alert(email) $.ajax({ url:&quot;./zhuce&quot;, data:{&quot;email&quot;:email,&quot;account&quot;:account,&quot;password&quot;:password,&quot;nickname&quot;:nickname,&quot;introduce&quot;:introduce,&quot;formData&quot;:formData}, type:&quot;post&quot;, async: false, cache: false, contentType: false, processData: false, success: function (data) { if(data==&quot;error&quot;){ alert(&quot;注册失败!&quot;) } if(data==&quot;success&quot;){ alert(&quot;成功!&quot;) window.location.href=&quot;login.jsp&quot; } } }); } &lt;/script&gt; ----------------------------------------------------------------------- </code></pre> <p>后台controller代码:<br> @RequestMapping(value=&quot;/zhuce&quot;)<br> @ResponseBody<br> public String zhuce(String email,String account,String password,String nickname,String introduce,@RequestParam(value=&quot;file&quot;,required = false) CommonsMultipartFile file,HttpServletRequest request, Model model){<br> System.out.println(&quot;邮箱和账号&quot;+email+&quot;,&quot;+account);<br> System.out.println(&quot;hhhh&quot;);<br> System.out.println(&quot;filename:&quot;+file.getOriginalFilename());</p> <pre><code> String path = request.getSession().getServletContext().getContextPath(); System.out.println(&quot;path:&quot;+path); return &quot;success&quot;; } ---------------------------------------------------------------- </code></pre> <p>运行结果是:<br> 邮箱和账号null,null<br> hhhh<br> java.lang.NullPointerException--获取文件名字这里是空的</p>

3个回答

var _formData = new FormData($("#tztg_saveForm")[0]);
$.ajax({
url: '',
type: 'POST',
cache: false,
data: _formData,
processData: false,
contentType: false,
dataType: "json",
}).done(function (data) {
}).fail(function (e) {
return false;
});
这是我做的一个项目做的上传。你可以参考下

要不就用js的插件 要不就自己写java接受文件流来接受

ajax的data数据类型可以是_formData吗

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!