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)異步上傳方法介紹的全部?jī)?nèi)容,希望對(duì)大家有所幫助。歡迎大家瀏覽本站其他專題,有什么疑問或者建議可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。希望大家對(duì)腳本之家網(wǎng)站多多支持!
相關(guān)文章
詳解SpringBoot 使用Spring Initializr 快速構(gòu)建工程(官方推薦)
本篇文章主要介紹了SpringBoot 使用Spring Initializr 快速構(gòu)建工程(官方推薦),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10解決使用mybatis-plus時(shí),生成的SQL大寫變小寫加下劃線問題
這篇文章主要介紹了解決使用mybatis-plus時(shí),生成的SQL大寫變小寫加下劃線問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12詳解Java中while和do-while循環(huán)、break的使用
本文介紹了循環(huán)結(jié)構(gòu)語句while和do-while循環(huán)、break的使用,while循環(huán)語句通過流程圖和語法語句結(jié)合一個(gè)求1~10的整數(shù)和的例子來幫助大家理解while循環(huán)的用法,感興趣的朋友跟隨小編來看看吧2020-11-11springboot開發(fā)flowable定時(shí)任務(wù)問題
這篇文章主要介紹了springboot開發(fā)flowable定時(shí)任務(wù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11windows 32位eclipse遠(yuǎn)程hadoop開發(fā)環(huán)境搭建
這篇文章主要介紹了windows 32位eclipse遠(yuǎn)程hadoop開發(fā)環(huán)境搭建的相關(guān)資料,需要的朋友可以參考下2016-07-07Mybatis分頁查詢的實(shí)現(xiàn)(Rowbounds和PageHelper)
本文主要介紹了Mybatis分頁查詢的實(shí)現(xiàn)(Rowbounds和PageHelper),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01SpringBoot JSON全局日期格式轉(zhuǎn)換器實(shí)現(xiàn)方式
這篇文章主要介紹了SpringBoot JSON全局日期格式轉(zhuǎn)換器,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04