JAVA 根據(jù)Url把多文件打包成ZIP下載實(shí)例
壓縮文件代碼工具類:
public class UrlFilesToZip {
private static final Logger logger = LoggerFactory.getLogger(UrlFilesToZip.class);
//根據(jù)文件鏈接把文件下載下來(lái)并且轉(zhuǎn)成字節(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;
}
}
控制層代碼:
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");// 設(shè)置強(qiáng)制下載不打開(kāi)
response.addHeader("Content-Disposition", "attachment;fileName=" + filename);// 設(shè)置文件名
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.一定要注意,否則會(huì)發(fā)生下載下來(lái)的壓縮包無(wú)法解壓。在給OutputStream 傳值之前,一定要先把ZipOutputStream的流給關(guān)閉了!
總結(jié)
以上所述是小編給大家介紹的JAVA 根據(jù)Url把多文件打包成ZIP下載,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java PriorityQueue數(shù)據(jù)結(jié)構(gòu)接口原理及用法
這篇文章主要介紹了Java PriorityQueue數(shù)據(jù)結(jié)構(gòu)接口原理及用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
SpringSceurity實(shí)現(xiàn)短信驗(yàn)證碼功能的示例代碼
這篇文章主要介紹了SpringSceurity實(shí)現(xiàn)短信驗(yàn)證碼功能的示例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Java使用Ajax實(shí)現(xiàn)跨域上傳圖片功能
這篇文章主要介紹了Java使用Ajax實(shí)現(xiàn)跨域上傳圖片功能,需要的朋友可以參考下2017-09-09
JAVASE精密邏輯控制過(guò)程詳解(分支和循環(huán)語(yǔ)句)
在一個(gè)程序執(zhí)行的過(guò)程中各條語(yǔ)句的執(zhí)行順序?qū)Τ绦虻慕Y(jié)果是有直接影響的,這篇文章主要給大家介紹了關(guān)于JAVASE精密邏輯控制(分支和循環(huán)語(yǔ)句)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04
SpringBoot事務(wù)鉤子函數(shù)的使用方式
本文介紹了SpringBoot中事務(wù)鉤子函數(shù)的使用方式,包括常見(jiàn)場(chǎng)景、使用方式等,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
springBoot整合shiro如何解決讀取不到@value值問(wèn)題
這篇文章主要介紹了springBoot整合shiro如何解決讀取不到@value值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-08-08
Java實(shí)現(xiàn)Huffman編碼的示例代碼
Huffman編碼是一種編碼方式,本文主要介紹了Java實(shí)現(xiàn)Huffman編碼的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08

