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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - JAVA 根據Url把多文件打包成ZIP下載實例

JAVA 根據Url把多文件打包成ZIP下載實例

2020-12-15 14:25莫哈_ Java教程

這篇文章主要介紹了JAVA 根據Url把多文件打包成ZIP下載的相關資料,需要的朋友可以參考下

壓縮文件代碼工具類:

?
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
public class UrlFilesToZip {
 private static final Logger logger = LoggerFactory.getLogger(UrlFilesToZip.class);
 //根據文件鏈接把文件下載下來并且轉成字節碼
 public byte[] getImageFromURL(String urlPath) {
  byte[] data = null;
  InputStream is = null;
  HttpURLConnection conn = null;
  try {
   URL url = new URL(urlPath);
   conn = (HttpURLConnection) url.openConnection();
   conn.setDoInput(true);
   // conn.setDoOutput(true);
   conn.setRequestMethod("GET");
   conn.setConnectTimeout(6000);
   is = conn.getInputStream();
   if (conn.getResponseCode() == 200) {
    data = readInputStream(is);
   } else {
    data = null;
   }
  } catch (MalformedURLException e) {
   logger.error("MalformedURLException", e);
  } catch (IOException e) {
   logger.error("IOException", e);
  } finally {
   try {
    if (is != null) {
     is.close();
    }
   } catch (IOException e) {
    logger.error("IOException", e);
   }
   conn.disconnect();
  }
  return data;
 }
 public byte[] readInputStream(InputStream is) {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int length = -1;
  try {
   while ((length = is.read(buffer)) != -1) {
    baos.write(buffer, 0, length);
   }
   baos.flush();
  } catch (IOException e) {
   logger.error("IOException", e);
  }
  byte[] data = baos.toByteArray();
  try {
   is.close();
   baos.close();
  } catch (IOException e) {
   logger.error("IOException", e);
  }
  return data;
 }
}

控制層代碼:

?
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
public void filesdown(HttpServletResponse response){
 try {
   String filename = new String("xx.zip".getBytes("UTF-8"), "ISO8859-1");//控制文件名編碼
   ByteArrayOutputStream bos = new ByteArrayOutputStream();
   ZipOutputStream zos = new ZipOutputStream(bos);
   UrlFilesToZip s = new UrlFilesToZip();
   int idx = 1;
   for (String oneFile : urls) {
    zos.putNextEntry(new ZipEntry("profile" + idx);
    byte[] bytes = s.getImageFromURL(oneFile);
    zos.write(bytes, 0, bytes.length);
    zos.closeEntry();
    idx++;
   }
   zos.close();
   response.setContentType("application/force-download");// 設置強制下載不打開
   response.addHeader("Content-Disposition", "attachment;fileName=" + filename);// 設置文件名
   OutputStream os = response.getOutputStream();
   os.write(bos.toByteArray());
   os.close();
  } catch (FileNotFoundException ex) {
   logger.error("FileNotFoundException", ex);
  } catch (Exception ex) {
   logger.error("Exception", ex);
  }
 }
 }

注意:

1. String filename = new String(“xx.zip”.getBytes(“UTF-8”), “ISO8859-1”);包裝zip文件名不發生亂碼。

2.一定要注意,否則會發生下載下來的壓縮包無法解壓。在給OutputStream 傳值之前,一定要先把ZipOutputStream的流給關閉了!

總結

以上所述是小編給大家介紹的JAVA 根據Url把多文件打包成ZIP下載,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://blog.csdn.net/sinat_32849651/article/details/77098161

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 欧美激情在线精品一区二区三区 | 日韩国产 | 91久久久久久久久 | 午夜久久久久 | 国产成人精品一区二 | 免费观看黄色av网站 | 国产成人免费视频网站视频社区 | 欧美日韩久久久久 | 久久久高清 | 日韩高清一区二区 | 伊人久久精品久久亚洲一区 | 九热精品 | 午夜小视频在线观看 | 国产精品国产三级国产aⅴ9色 | 亚洲视频免费观看 | 亚洲精品一区二区三区在线观看 | 巴西性猛交xxxx免费看久久久 | 日韩综合一区 | 最新电影在线高清免费完整观看视频 | 午夜视频在线观看网站 | 亚洲精品91 | 91免费看大片 | 极品久久 | 免费看少妇高潮一级毛片特黄 | 最近2019中文字幕大全视频10 | 欧美成人免费在线 | 久久这里精品 | 97av在线 | 色视频在线播放 | 亚洲国产精品美女 | 国产精品久久久久国产a级 国产免费久久 | 狠狠综合久久av一区二区老牛 | 亚洲欧美激情精品一区二区 | 在线精品国产 | 亚洲欧美在线一区 | 免费一级毛片 | 欧美成年网站 | 亚洲精品一区二区三区在线观看 | 欧美久久精品一级黑人c片 成人在线视频免费观看 | 伊人逼逼 | av色资源|