一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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

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

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

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

這篇文章主要介紹了JAVA 根據(jù)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);
 //根據(jù)文件鏈接把文件下載下來并且轉成字節(jié)碼
 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文件名不發(fā)生亂碼。

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

總結

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 视频一本大道香蕉久在线播放 | jizzjizz大学生 | 91麻豆精品激情在线观看最新 | 国色天香高清版 | 欧美整片在线 | 99视频在线国产 | 无码国产成人777爽死 | 高清视频在线播放 | 国产美女亚洲精品久久久久久 | gav男人天堂 | 肉性天堂 | 2021国产麻豆剧传媒新片 | 男人操女人动图 | 成人150p | 毛片影院| 99热成人精品免费久久 | 日本漫画工囗全彩番在线 | 9总探花新品牛仔背带裤 | 红杏劫| 欧美四虎影院 | 久久er99热精品一区二区 | 日本免费一二区 | 亚洲AV无码国产精品色在线看 | 国产va免费精品高清在线 | 国产精品亚洲精品青青青 | 99久久国产综合精品女不卡 | 扒开双腿羞辱调教play视频 | 成人高辣h视频一区二区在线观看 | 洗濯屋动漫在线观看 | 免费福利资源站在线视频 | 国产成人影院 | 暖暖高清日本在线 | 無码一区中文字幕少妇熟女H | 调教全程肉动画片在线观看 | 亚洲人成综合在线播放 | 亚洲一区二区三区免费视频 | 青草精品 | 国产悠悠视频在线播放 | 欧美成黑人性猛交xxoo | 国产一区在线免费观看 | 亚洲免费二区 |