• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    SpringMVC+Ajax實現文件批量上傳和下載功能實例代碼

    來源:懂視網 責編:小采 時間:2020-11-27 22:51:32
    文檔

    SpringMVC+Ajax實現文件批量上傳和下載功能實例代碼

    SpringMVC+Ajax實現文件批量上傳和下載功能實例代碼:今天做了文件的上傳下載,小小總結一下,基本的web項目建立及SpringMVC框架搭建此處不詳細寫出來了。 上傳form: <form id=uploadfiles enctype=multipart/form-data> <input type=file multiple=mul
    推薦度:
    導讀SpringMVC+Ajax實現文件批量上傳和下載功能實例代碼:今天做了文件的上傳下載,小小總結一下,基本的web項目建立及SpringMVC框架搭建此處不詳細寫出來了。 上傳form: <form id=uploadfiles enctype=multipart/form-data> <input type=file multiple=mul

    今天做了文件的上傳下載,小小總結一下,基本的web項目建立及SpringMVC框架搭建此處不詳細寫出來了。

    上傳form:

    <form id="uploadfiles" enctype="multipart/form-data">
     <input type="file" multiple="multiple" id="file_upload" name="file_upload" /> 
     <input type="button" value="上傳" onclick="upload()" />
    </form>

    上傳Ajax:

    <script type="text/javascript">
    /*
     * 上傳文件
     */
    function upload(){
     var formData = new FormData($( "#uploadfiles" )[0]);
     $.ajax({
     type: "post",
     url: "./path/upload",
     dataType: "json",
     data: formData,
     /** 
     *必須false才會自動加上正確的Content-Type 
     */ 
     contentType : false, 
     /** 
     * 必須false才會避開jQuery對 formdata 的默認處理 
     * XMLHttpRequest會對 formdata 進行正確的處理 
     */ 
     processData : false,
     success: function(data){//從后端返回數據進行處理
     if(data){
     alert("上傳成功!");
     }else{
     alert("上傳失敗!");
     }
     },
     error: function(err) {//提交出錯
     $("#msg").html(JSON.stringify(err));//打出響應信息
     alert("服務器無響應");
     }
     });
    }
    </script>

    spring.xml配置加上:

    <!-- 配置文件上傳 -->
     <bean id="multipartResolver"
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
     <!-- 默認編碼 -->
     <property name="defaultEncoding" value="utf-8" />
     <!-- 文件大小最大值 -->
     <property name="maxUploadSize" value="10485760000" />
     <!-- 內存中的最大值 -->
     <property name="maxInMemorySize" value="40960" />
     </bean>
    controller:
    /*
     * 上傳多個文件
     */
     @RequestMapping(value = "/upload", produces = "application/json;charset=UTF-8")
     public @ResponseBody
     boolean uploadFiles(@RequestParam("file_upload") MultipartFile [] files) {
     boolean result = false;
     String realPath;
     for(int i=0;i<files.length;i++){
     if (!files[i].isEmpty()) { 
     String uniqueName=files[i].getOriginalFilename();//得到文件名
     realPath="E:"+File.separator+uniqueName;//文件上傳的路徑這里為E盤
     files[i].transferTo(new File(realPath)); // 轉存文件
     result = true; 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
     }
     }
     return result;
     }

    下載的jsp頁面代碼根據需求不同自己設計,這里給出controller代碼:

    /*
     * 下載多個文件
     */
     @RequestMapping(value = "/download")
     public void downloadFiles(HttpServletResponse response) {
     String str= request.getParameter("rows");//下載文件信息,包括文件名、存儲路徑等
     JSONArray path=(JSONArray) JSONArray.parse(request.getParameter("rows"));
     Path paths[]=new Path[path.size()];
     paths = JSONArray.parseArray(str, Path.class).toArray(paths);
     String uri = "d:"+ File.separator + "mldn.zip";//臨時文件存儲路徑
     File zipFile = new File(uri) ; // 定義壓縮文件名稱 
     ZipOutputStream zipOut = null;// 聲明壓縮流對象 
     InputStream input = null;
     //將要壓縮的文件加入到壓縮
    輸出流中 try { zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); } catch (FileNotFoundException e) { e.printStackTrace(); } for(int i = 0;i<paths.length;i++){ File file = new File(paths[i].getUri()+File.separator+paths[i].getFilename()); try { input = new FileInputStream(file) ;// 定義文件的輸入流 zipOut.putNextEntry(new ZipEntry(file.getName())) ; // 設置ZipEntry對象 } catch (Exception e) { e.printStackTrace(); } } //將文件寫入到壓縮文件中 int temp = 0 ; try { while((temp=input.read())!=-1){ // 讀取內容 zipOut.write(temp) ; // 寫到壓縮文件中 } } catch (IOException e) { e.printStackTrace(); }finally{ try { input.close() ; zipOut.close() ; } catch (IOException e) { e.printStackTrace(); } } try { // 以流的形式下載文件。 BufferedInputStream fis = new BufferedInputStream(new FileInputStream(zipFile)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/x-msdownload;"); response.setHeader("Content-Disposition", "attachment;filename=" + zipFile.getName()); toClient.write(buffer); toClient.flush(); toClient.close(); zipFile.delete(); //將生成的服務器端文件刪除 } catch (IOException ex) { ex.printStackTrace(); } }

    將多個文件打成一個壓縮包下載,然后將生成的臨時壓縮文件刪除。

    下載頁面如果用Ajax提交請求的話要注意:ajax函數的返回類型只有xml、text、json、html等類型,沒有“流”類型,所以我們要實現ajax下載,不能夠使用相應的ajax函數進行文件下載。但可以用js生成一個form,用這個form提交參數,并返回“流”類型的數據。

    例子:

    function download(){
     var form=$("<form>");//定義一個form表單
     form.attr("style","display:none");
     form.attr("target","");
     form.attr("method","post");
     form.attr("action","./path/download");//請求url
     var input1=$("<input>");
     input1.attr("type","hidden");
     input1.attr("name","rows");//設置屬性的名字
     input1.attr("value",“test”);//設置屬性的值
     $("body").append(form);//將表單放置在web中
     form.append(input1);
     form.submit();//表單提交 
     }

    總結

    以上所述是小編給大家介紹的SpringMVC+Ajax實現文件批量上傳和下載功能實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    SpringMVC+Ajax實現文件批量上傳和下載功能實例代碼

    SpringMVC+Ajax實現文件批量上傳和下載功能實例代碼:今天做了文件的上傳下載,小小總結一下,基本的web項目建立及SpringMVC框架搭建此處不詳細寫出來了。 上傳form: <form id=uploadfiles enctype=multipart/form-data> <input type=file multiple=mul
    推薦度:
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 国产92成人精品视频免费| 精品综合久久久久久97超人| 国产91在线精品| 四虎国产精品免费久久5151| 91久久精品视频| 久久精品亚洲男人的天堂| 亚洲精品国产高清不卡在线 | 欧美日韩精品久久久久| 久久亚洲AV永久无码精品| 国产精品视频分类一区| 国产精品丝袜久久久久久不卡| 欧美成人精品欧美一级乱黄码| 日本精品在线视频| 欧美日韩国产成人高清视频,欧美日韩在线精品一 | 国产精品久久午夜夜伦鲁鲁| 欧美精品亚洲精品日韩1818| 亚洲欧美精品AAAAAA片| 99精品久久精品一区二区| 亚洲AV永久青草无码精品| 久久91精品国产91久久小草| 国产精品水嫩水嫩| 久久亚洲私人国产精品vA| 56prom精品视频在放免费| 69久久夜色精品国产69| 国产中文在线亚洲精品官网| 一本精品中文字幕在线| 久久国产精品久久| 99re8这里有精品热视频免费| 精品久久久久久国产潘金莲 | 精品欧美一区二区在线看片| 婷婷精品国产亚洲AV麻豆不片| 欧美精品高清在线xxxx| 精品久久久无码中文字幕天天| 国产精品美女久久久免费| 无码精品国产VA在线观看DVD| 日韩一区二区三区精品| 91老司机深夜福利精品视频在线观看 | 大桥未久在线精品视频在线 | 国内精品九九久久久精品| 国自产偷精品不卡在线| 国产精品视频第一区二区三区|