前端使用FormData向后台發送數據,在后台使用request.getParameter()進行接收時為null


前端使用FormData對象拼接需要傳遞到后台的數據,並且使用formData.get()可以得到數據,說明數據已被放到formData對象中,

但是后台使用request.getParameter()來獲取時得到的值為null.

 1 var formData = new FormData();
 2         formData.append('shopImg', shopImg);
 3         formData.append('shopStr', JSON.stringify(shop));
 4         var kaptchaActual = $('#inputKaptcha').val();
 5         if(!kaptchaActual){
 6             $.toast("請輸入驗證碼");
 7             return;
 8         }
 9         formData.append('inputKaptcha',kaptchaActual);
10         console.log(formData.get("inputKaptcha"));  //可以得到值,說明已被安排上了
11         // 使用Ajax進行表單的提交
12         $.ajax({
13             url : registerShopUrl,
14             type : 'POST',
15             data : formData,
16             contentType : false,
17             processData : false,
18             cache : false,
19             success : function(data) {
20                 if (data.success) {
21                     $.toast("提交成功");
22                 } else {
23                     $.toast("提交失敗!" + data.errMsg);
24                 }
25                 $('#kaptcha_img').click();
26             }
27         })
28     });

這種情況下,要再引入兩個包

 1 <dependency>
 2         <groupId>commons-fileupload</groupId>
 3         <artifactId>commons-fileupload</artifactId>
 4         <version>1.2.1</version>
 5     </dependency>
 6     
 7     <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
 8     <dependency>
 9         <groupId>commons-io</groupId>
10         <artifactId>commons-io</artifactId>
11         <version>2.4</version>
12     </dependency>

這兩個包是在springmvc上傳時要用到的,還要再進行注冊

 1 <bean id="multipartResolver"  
 2         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
 3         <!-- 上傳文件大小上限,單位為字節(10MB) -->
 4         <property name="maxUploadSize">  
 5             <value>10485760</value>  
 6         </property>  
 7         <!-- 請求的編碼格式,必須和jSP的pageEncoding屬性一致,以便正確讀取表單的內容,默認為ISO-8859-1 -->
 8         <property name="defaultEncoding">
 9             <value>UTF-8</value>
10         </property>
11     </bean>

關於SpringMVC實現文件上傳下載具體可參見:

https://www.cnblogs.com/WJ-163/p/6269409.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM