SpringMVC使用MultipartFile 實(shí)現(xiàn)異步上傳方法介紹
目的是實(shí)現(xiàn)異步上傳
1.添加pom依賴
添加pom依賴,因?yàn)橛玫腶jax,數(shù)據(jù)需要轉(zhuǎn)成json的格式進(jìn)行傳輸,所以還有加入一個(gè)JSON jar包:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.37</version>
</dependency>
2.修改配置文件
applicationContext.xml里面需要加上:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="5400000"></property>
</bean>
3.前端頁面上
前端頁面:
<form id="uploadForm" name="uploadForm"
enctype="multipart/form-data">
<input name="messageContent" value="多個(gè)參數(shù)的情況下">
<label>文件</label> <input type="file" name="file">
<button class="btn" type="button" id="doSave">提交</button>
</form>
</body>
</html>
需要加入的JS:
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script> <script type="text/javascript" src="js/jquery-ui.min.js"></script> <script type="text/javascript" src="js/jquery.form.js"></script>
JS方法:
<script>
$(function() {
$("#doSave")
.click(
function() {
$("#uploadForm")
.ajaxSubmit(
{
type : 'post',
url : "/tmpInfo/method2.do",
//data: //注意只要是寫在表單里面的,都不需要加這個(gè)屬性。在controller中可以根據(jù)@RequestParam String str獲取到屬性值。
contentType : "application/x-www-form-urlencoded; charset=utf-8",
success: function(data) {
//接受到的data還只是一個(gè)字符串,需要轉(zhuǎn)成json對(duì)象
var obj = JSON.parse(data);
if(obj.flag==true){
alert("上傳成功");
}else{
alert("error");
}
},
error: function (data)//服務(wù)器響應(yīng)失敗處理函數(shù)
{
alert("出錯(cuò)");
}
});
});
});
controller代碼:
@RequestMapping("/method2")
@ResponseBody
public String method2(@RequestParam MultipartFile file,
@RequestParam String messageContent ) {
//多個(gè)參數(shù)的話只要多個(gè)@RequestParam即可,注意參數(shù)名要和表單里面的屬性名一致
JSONObject json =new JSONObject();
System.out.println(messageContent);
String orgiginalFileName = "";
int m =new Random().nextInt(100)+10;
System.out.println("m="+m);
String path="D:/"+m+"b.txt";
try {
File newFile =new File(path);
file.transferTo(newFile);
String fileName = file.getName();
InputStream inputStream = file.getInputStream();
String content = file.getContentType();
orgiginalFileName = file.getOriginalFilename();
System.out.println("fileName: "+fileName+", inputStream: "+ inputStream
+"\r\n content: "+content+", orgiginalFileName: ="+ orgiginalFileName
+"\r\n projectName: ");
} catch (IOException e) {
e.printStackTrace();
}
json.put("flag", true);
json.put("message", "success");
System.out.println(json.toJSONString());
return json.toJSONString();
}
以上就是分享給大家的關(guān)于SpringMVC使用MultipartFile 實(shí)現(xiàn)異步上傳方法介紹的全部內(nèi)容,希望對(duì)大家有所幫助。歡迎大家瀏覽本站其他專題,有什么疑問或者建議可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。希望大家對(duì)腳本之家網(wǎng)站多多支持!
相關(guān)文章
全網(wǎng)最深分析SpringBoot MVC自動(dòng)配置失效的原因
這篇文章主要介紹了全網(wǎng)最深分析SpringBoot MVC自動(dòng)配置失效的原因,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Springboot之restTemplate配置及使用方式
這篇文章主要介紹了Springboot之restTemplate配置及使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Spring設(shè)計(jì)模式中代理模式詳細(xì)講解
如何實(shí)現(xiàn)在不修改源碼的基礎(chǔ)上實(shí)現(xiàn)代碼功能的增強(qiáng)呢?spring為我們提供了代理模式。所謂的代理模式通俗來說就是一個(gè)中介,它給某一個(gè)對(duì)象提供一個(gè)代理對(duì)象,并由代理對(duì)象控制原對(duì)象的引用,從而實(shí)現(xiàn)在不修改源碼的基礎(chǔ)上實(shí)現(xiàn)代碼功能的增強(qiáng)2023-01-01
mybatis-plus 實(shí)現(xiàn)分頁查詢的示例代碼
本文介紹了在MyBatis-Plus中實(shí)現(xiàn)分頁查詢,包括引入依賴、配置分頁插件、使用分頁查詢以及在控制器中調(diào)用分頁查詢的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
解決SSLContext.getInstance()中參數(shù)設(shè)置TLS版本無效的問題
這篇文章主要介紹了解決SSLContext.getInstance()中參數(shù)設(shè)置TLS版本無效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

