java實(shí)現(xiàn)文件打包壓縮輸出到瀏覽器下載
文件打包壓縮輸出到瀏覽器下載
java批量下載文件打包壓縮工具類,輸出到瀏覽器下載,可以自己改名。
一、工具類:
入?yún)?:文件LIst ;打包后的名字 ;響應(yīng)到瀏覽器
/**
* 功能:壓縮多個(gè)文件,輸出壓縮后的zip文件流
*
* @param srcfile:源文件列表
* @param zipFileName:壓縮后的文件名
* @param response: Http響應(yīng)
*/
public void zipFiles(List<File> srcfile, String zipFileName, HttpServletResponse response) throws IOException {
byte[] buf = new byte[1024];
// 獲取輸出流
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
FileInputStream in = null;
ZipOutputStream out = null;
try {
response.reset(); // 重點(diǎn)突出
// 不同類型的文件對(duì)應(yīng)不同的MIME類型
response.setContentType("application/x-msdownload");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + zipFileName + ".zip");
// ZipOutputStream類:完成文件或文件夾的壓縮
out = new ZipOutputStream(bos);
for (int i = 0; i < srcfile.size(); i++) {
in = new FileInputStream(srcfile.get(i));
// 給列表中的文件單獨(dú)命名
out.putNextEntry(new ZipEntry(srcfile.get(i).getName()));
int len = -1;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
out.close();
bos.close();
log.info("壓縮完成.");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) in.close();
if (out != null) out.close();
}
}
二、調(diào)用
zipFiles(files, zipName, response);
生成zip文件并瀏覽器導(dǎo)出
總結(jié)一下,關(guān)于Java下載zip文件并導(dǎo)出的方法,瀏覽器導(dǎo)出。
String downloadName = "下載文件名稱.zip";
downloadName = BrowserCharCodeUtils.browserCharCodeFun(request, downloadName);//下載文件名亂碼問題解決
//將文件進(jìn)行打包下載
try {
OutputStream out = response.getOutputStream();
byte[] data = createZip("/fileStorage/download");//服務(wù)器存儲(chǔ)地址
response.reset();
response.setHeader("Content-Disposition","attachment;fileName="+downloadName);
response.addHeader("Content-Length", ""+data.length);
response.setContentType("application/octet-stream;charset=UTF-8");
IOUtils.write(data, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
獲取下載zip文件流
public byte[] createZip(String srcSource) throws Exception{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
//將目標(biāo)文件打包成zip導(dǎo)出
File file = new File(srcSource);
a(zip,file,"");
IOUtils.closeQuietly(zip);
return outputStream.toByteArray();
}
public void a(ZipOutputStream zip, File file, String dir) throws Exception {
//如果當(dāng)前的是文件夾,則進(jìn)行進(jìn)一步處理
if (file.isDirectory()) {
//得到文件列表信息
File[] files = file.listFiles();
//將文件夾添加到下一級(jí)打包目錄
zip.putNextEntry(new ZipEntry(dir + "/"));
dir = dir.length() == 0 ? "" : dir + "/";
//循環(huán)將文件夾中的文件打包
for (int i = 0; i < files.length; i++) {
a(zip, files[i], dir + files[i].getName()); //遞歸處理
}
} else { //當(dāng)前的是文件,打包處理
//文件輸入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(dir);
zip.putNextEntry(entry);
zip.write(FileUtils.readFileToByteArray(file));
IOUtils.closeQuietly(bis);
zip.flush();
zip.closeEntry();
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)MD5加密的方法小結(jié)
這篇文章主要介紹了java實(shí)現(xiàn)MD5加密的方法,結(jié)合具體實(shí)例形式總結(jié)分析了java實(shí)現(xiàn)md5加密的常用操作技巧與使用方法,需要的朋友可以參考下2017-10-10
Java Code Cache滿導(dǎo)致應(yīng)用性能降低問題解決
這篇文章主要介紹了Java Code Cache滿導(dǎo)致應(yīng)用性能降低問題解決,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
SpringBoot2.7.14整合redis7的詳細(xì)過程
這篇文章主要介紹了SpringBoot2.7.14整合redis7的詳細(xì)過程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-10-10
利用Spring Boot創(chuàng)建docker image的完整步驟
這篇文章主要給大家介紹了關(guān)于如何利用Spring Boot創(chuàng)建docker image的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java通過Fork/Join優(yōu)化并行計(jì)算
這篇文章主要為大家詳細(xì)介紹了Java通過Fork、Join來優(yōu)化并行計(jì)算,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
SpringDataMongoDB多文檔事務(wù)的實(shí)現(xiàn)
mongodb4.0也出來一段時(shí)間了,這個(gè)版本最為大眾期待的特性就是支持了多文檔事務(wù)。這篇文章主要介紹了SpringDataMongoDB多文檔事務(wù)的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2018-11-11
Spring容器的創(chuàng)建過程之如何注冊(cè)BeanPostProcessor詳解
關(guān)于BeanPostProcessor 各位一定不陌生,今天整理的這篇文章總結(jié)了如何注冊(cè)BeanPostProcessor,文中有非常詳細(xì)的圖文示例,需要的朋友可以參考下2021-06-06

