国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - JavaWeb實現(xiàn)多文件上傳及zip打包下載

JavaWeb實現(xiàn)多文件上傳及zip打包下載

2021-05-17 14:12kidQ Java教程

這篇文章主要為大家詳細(xì)介紹了JavaWeb實現(xiàn)多文件上傳及zip打包下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了javaweb多文件上傳及zip打包下載的具體代碼,供大家參考,具體內(nèi)容如下

項目中經(jīng)常會使用到文件上傳及下載的功能。本篇文章總結(jié)場景在javaweb環(huán)境下,多文件上傳及批量打包下載功能,包括前臺及后臺部分。

首先明確一點:

無法通過頁面的無刷新ajax請求,直接發(fā)下載、上傳請求。上傳和下載,均需要在整頁請求的基礎(chǔ)上實現(xiàn)。項目中一般通過構(gòu)建form表單形式實現(xiàn)這一功能。

一、多文件上傳

項目需求為實現(xiàn)多圖片上傳功能。參考測試了網(wǎng)上找到的眾多插件方法后,決定選用jquery原始上傳方案。以下按步驟貼出具體代碼。

1、html部分(可省略使用js構(gòu)建)

?
1
2
3
4
<form id="uploadform" method="post" enctype="multipart/form-data">
 <input type="file" hidden name="fileimage" multiple/>
 <a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" id="filesubmit" onclick="uploadfilemulti()">上傳資料</a>
</form>

有幾點說明:

1. form中 enctype=”multipart/form-data”
2. 例中使用標(biāo)簽,構(gòu)建submit

2、js部分

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var formdata = new formdata($("#uploadform")[0]);
formdata.append("foldname", "datumlist");  //設(shè)置父級文件夾名稱
 
formdata.append("odercode", selfordercode);
formdata.append("datumtype", datumtype);
$.ajax({
 type: "post",
 data: formdata,
 url: "order/datumlist/batchinsertdatumlists",
 contenttype: false,
 processdata: false,
 success: function (result) {
  if (result.success) {
 
   //清空框文件內(nèi)容
   $("#fileimage").val("");
   var obj = document.getelementbyid('fileimage');
   obj.outerhtml = obj.outerhtml;
 
   refreshdatumlist();
   showsuccesstoast(result.message);
  } else {
   showwarningtoast(result.message);
  }
 },
 error: function () {
  showerrortoast('請求失敗!')
 }
});

以上有幾點說明:

1. var formdata = new formdata($(“#uploadform”)[0]);
2. 使用 formdata.append(“odercode”, selfordercode); 添加其他參數(shù)

java后臺

?
1
2
multiparthttpservletrequest mrequest = (multiparthttpservletrequest) request;
list<multipartfile> files = mrequest.getfiles("fileimage");

以上有幾點說明:

1. 獲取multiparthttpservletrequest,對應(yīng)file標(biāo)簽的name

二、文件批量下載

本項目中,需求為批量下載某一批次文件。使用zip在服務(wù)器壓縮文件,之后將文件下載到客戶機(jī)。
網(wǎng)上查詢,使用java自帶的文件輸出類不能解決壓縮文件中文件名亂碼的問題。解決方法:使用ant.jar包,創(chuàng)建壓縮文件時,可以設(shè)置文件的編碼格式,文件名亂碼的問題就解決了。

html部分(可省略使用js構(gòu)建)

?
1
2
3
4
5
6
7
<form id="uploadform" method="post" enctype="multipart/form-data">
 <div class="product-dl">
  <input type="hidden" name="ordercode"/>
  <input type="hidden" name="datumtype"/>
  <a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" class="btn" onclick="batchdatumlistdownload()">批量下載</a>
 </div>
</form>

js部分

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//批量下載
function batchdatumlistdownload() {
 var param = {};
 param.datumtype = $("#datumtypeq").val();
 if (param.datumtype == -1) {
  param.datumtype = null//查詢所有
 }
 param.ordercode = selfordercode;
 
 $("#uploadform input[name=ordercode]").val(param.ordercode);
 $("#uploadform input[name=datumtype]").val(param.datumtype);
 
 var form = $("#uploadform")[0];
 form.action = "order/datumlist/batchdownloaddatumlist";
 form.method = "post";
 form.submit();//表單提交
}

后臺部分

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public void batchdownloaddatumlist(datumlistvo datumlistvo, httpservletresponse response) {
 try {
  //查詢文件列表
  list<datumlistvo> volist = datumlistservice.querydatumlists(datumlistvo);
 
  //壓縮文件
  list<file> files = new arraylist<>();
  for (datumlistvo vo : volist) {
   file file = new file(vo.getdatumurl());
   files.add(file);
  }
 
  string filename = datumlistvo.getordercode() + "_" + datumlistvo.getdatumtype() + ".zip";
  //在服務(wù)器端創(chuàng)建打包下載的臨時文件
  string globaluploadpath = "";
  string osname = system.getproperty("os.name");
  if (osname.tolowercase().indexof("windows") >= 0) {
   globaluploadpath = globalkeys.getstring(globalkeys.windows_upload_path);
  } else if (osname.tolowercase().indexof("linux") >= 0 || osname.tolowercase().indexof("mac") >= 0) {
   globaluploadpath = globalkeys.getstring(globalkeys.linux_upload_path);
  }
  string outfilepath = globaluploadpath + file.separator + filename;
  file file = new file(outfilepath);
  //文件輸出流
  fileoutputstream outstream = new fileoutputstream(file);
  //壓縮流
  zipoutputstream toclient = new zipoutputstream(outstream);
  //設(shè)置壓縮文件內(nèi)的字符編碼,不然會變成亂碼
  toclient.setencoding("gbk");
  ziputil.zipfile(files, toclient);
  toclient.close();
  outstream.close();
  ziputil.downloadzip(file, response);
 
 } catch (exception e) {
  e.printstacktrace();
 }
}

其中ziputil.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
 * 壓縮文件列表中的文件
 *
 * @param files
 * @param outputstream
 * @throws ioexception
 */
public static void zipfile(list files, zipoutputstream outputstream) throws ioexception, servletexception {
 try {
  int size = files.size();
  //壓縮列表中的文件
  for (int i = 0; i < size; i++) {
   file file = (file) files.get(i);
   try {
    zipfile(file, outputstream);
   } catch (exception e) {
    continue;
   }
  }
 } catch (exception e) {
  throw e;
 }
}
 
/**
 * 將文件寫入到zip文件中
 *
 * @param inputfile
 * @param outputstream
 * @throws exception
 */
public static void zipfile(file inputfile, zipoutputstream outputstream) throws ioexception, servletexception {
 try {
  if (inputfile.exists()) {
   if (inputfile.isfile()) {
    fileinputstream instream = new fileinputstream(inputfile);
    bufferedinputstream binstream = new bufferedinputstream(instream);
    zipentry entry = new zipentry(inputfile.getname());
    outputstream.putnextentry(entry);
 
    final int max_byte = 10 * 1024 * 1024; //最大的流為10m
    long streamtotal = 0;      //接受流的容量
    int streamnum = 0;      //流需要分開的數(shù)量
    int leavebyte = 0;      //文件剩下的字符數(shù)
    byte[] inoutbyte;       //byte數(shù)組接受文件的數(shù)據(jù)
 
    streamtotal = binstream.available();      //通過available方法取得流的最大字符數(shù)
    streamnum = (int) math.floor(streamtotal / max_byte); //取得流文件需要分開的數(shù)量
    leavebyte = (int) streamtotal % max_byte;    //分開文件之后,剩余的數(shù)量
 
    if (streamnum > 0) {
     for (int j = 0; j < streamnum; ++j) {
      inoutbyte = new byte[max_byte];
      //讀入流,保存在byte數(shù)組
      binstream.read(inoutbyte, 0, max_byte);
      outputstream.write(inoutbyte, 0, max_byte); //寫出流
     }
    }
    //寫出剩下的流數(shù)據(jù)
    inoutbyte = new byte[leavebyte];
    binstream.read(inoutbyte, 0, leavebyte);
    outputstream.write(inoutbyte);
    outputstream.closeentry();  //closes the current zip entry and positions the stream for writing the next entry
    binstream.close(); //關(guān)閉
    instream.close();
   }
  } else {
   throw new servletexception("文件不存在!");
  }
 } catch (ioexception e) {
  throw e;
 }
}
 
/**
 * 下載打包的文件
 *
 * @param file
 * @param response
 */
public static void downloadzip(file file, httpservletresponse response) {
 try {
  // 以流的形式下載文件。
  bufferedinputstream fis = new bufferedinputstream(new fileinputstream(file.getpath()));
  byte[] buffer = new byte[fis.available()];
  fis.read(buffer);
  fis.close();
  // 清空response
  response.reset();
 
  outputstream toclient = new bufferedoutputstream(response.getoutputstream());
  response.setcontenttype("application/octet-stream");
  response.setheader("content-disposition", "attachment;filename=" + file.getname());
  toclient.write(buffer);
  toclient.flush();
  toclient.close();
  file.delete();  //將生成的服務(wù)器端文件刪除
 } catch (ioexception ex) {
  ex.printstacktrace();
 }
}

以上基本滿足文件上傳下載所需。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/qq_37878879/article/details/77197448

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美一区二区三 | 色婷婷av久久久久久久 | 国产在线免费 | 一区福利 | 久久av一区二区三区 | 四虎视频 | 国产中文字幕在线免费观看 | 亚洲国产精品久久久 | www.欧美| 少妇看av一二三区 | 国产精品免费久久久久久 | 羞羞的视频 | 在线精品国产一区二区三区 | 亚洲国产精品久久久 | 欧美国产精品一区二区三区 | 久久精品久久久 | 欧美一级黄色片 | 欧美日韩在线电影 | 91一区二区 | 国产在线观看91一区二区三区 | 日韩精品一区二区三区精品av | 亚洲 自拍 另类 欧美 丝袜 | 亚洲成av人片在线观看 | 国产男女免费视频 | 国产日产久久高清欧美一区 | 免费在线观看黄色 | 色婷婷精品久久二区二区蜜臂av | 日韩中文字幕无码一区二区三区 | 国产日韩欧美 | 日韩成人在线免费观看 | 日韩视频精品 | 欧美激情一区二区 | jizz欧美大片 | www.国产 | 日日操综合 | 日韩美女毛片 | 亚洲激情在线观看 | 日韩色爱 | 天天摸天天操 | 亚洲www啪成人一区二区 | 黄片毛片免费观看 |