java實(shí)現(xiàn)批量下載 多文件打包成zip格式下載
本文實(shí)例為大家分享了java實(shí)現(xiàn)批量下載的具體代碼,供大家參考,具體內(nèi)容如下
現(xiàn)在的需求的:
根據(jù)產(chǎn)品族、產(chǎn)品類型,下載該產(chǎn)品族、產(chǎn)品類型下面的pic包;
pic包是zip壓縮文件;
t_product表:

這些包以blob形式存在另一張表中:
t_imagefile表:

現(xiàn)在要做的是:將接入網(wǎng)、OLT下面的兩個(gè)包downloadPIC:MA5800系列-pic.zip 和 MA5900-pic.rar一起打包成zip壓縮文件下載下來(lái);
代碼:
ProductController.java:
/**
* 根據(jù)產(chǎn)品族、產(chǎn)品類型下載照片包
*/
@RequestMapping("/downloadwBatch")
public void downloadwBatch(HttpServletRequest request, HttpServletResponse response, String productFamily, String productType){
//http://localhost:8080/MySSM/downloadwBatch?productFamily=接入網(wǎng)&productType=OLT
try {
productFamily = new String(productFamily.getBytes("iso-8859-1"), "utf-8");
productType = new String(productType.getBytes("iso-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//獲取要下載的照片包名
Map<String, String> params = new HashMap<String, String>();
params.put("productFamily", productFamily);
params.put("productType", productType);
List<String> packageNames = productService.getPackageNamesByFamilyAndType(params);
//根據(jù)包名獲取待下載的文件 文件名-字節(jié)數(shù)組的形式
Map<String, byte[]> files = new HashMap<String, byte[]>();
for(String packageName : packageNames){
byte[] f = productService.getPackage(packageName);
if(f!=null){
files.put(packageName, f);
}
}
//設(shè)置下載的壓縮包名
String zipName = productFamily + "_"+ productType + ".zip";
//根據(jù)文件,進(jìn)行壓縮,批量下載
if(files.size() > 0){
productService.downloadBatchByFile(response, files, zipName);
}
}
ProductService.java:
/**
* 根據(jù)包名獲取文件
*/
public byte[] getPackage(String packageName){
byte[] bag = null;
try{
ImageFile m = productMapper.getPackage(packageName);
if(m!=null){
bag = m.getPicture();
}
}catch(Exception e){
e.printStackTrace();
}
return bag;
}
/**
* 根據(jù)產(chǎn)品族、產(chǎn)品類型 獲取待下載的包名
* @param params
* @return
*/
public List<String> getPackageNamesByFamilyAndType(Map<String, String> params) {
List<String> packageNames = productMapper.getPackageNamesByFamilyAndType(params);
return packageNames;
}
/**
* 根據(jù)文件,進(jìn)行壓縮,批量下載
* @param response
* @param files
* @throws Exception
*/
public void downloadBatchByFile(HttpServletResponse response, Map<String, byte[]> files, String zipName){
try{
response.setContentType("application/x-msdownload");
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(zipName, "utf-8"));
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
BufferedOutputStream bos = new BufferedOutputStream(zos);
for(Entry<String, byte[]> entry : files.entrySet()){
String fileName = entry.getKey(); //每個(gè)zip文件名
byte[] file = entry.getValue(); //這個(gè)zip文件的字節(jié)
BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(file));
zos.putNextEntry(new ZipEntry(fileName));
int len = 0;
byte[] buf = new byte[10 * 1024];
while( (len=bis.read(buf, 0, buf.length)) != -1){
bos.write(buf, 0, len);
}
bis.close();
bos.flush();
}
bos.close();
}catch(Exception e){
e.printStackTrace();
}
}
ProductMapper.java:
/** * 根據(jù)包名獲取文件 */ public ImageFile getPackage(String packageName) throws Exception; /** * 據(jù)產(chǎn)品族、產(chǎn)品類型 獲取待下載的包名 */ public List<String> getPackageNamesByFamilyAndType(Map<String, String> params);
ProductMapper.xml:
<!-- 根據(jù)包名獲取文件 -->
<select id="getPackage" parameterType="java.lang.String" resultType="com.cy.model.ImageFile">
select * from t_imagefile where packageName = #{packageName}
</select>
<!-- 跟據(jù)產(chǎn)品族、產(chǎn)品類型 獲取待下載的包名 -->
<select id="getPackageNamesByFamilyAndType" parameterType="java.util.Map" resultType="java.lang.String">
select packageName from t_imagefile m join t_product p on m.packageName = p.downloadPic
where p.productFamily = #{productFamily} and p.productType = #{productType}
</select>
測(cè)試:
在瀏覽器中輸入:http://localhost:8080/MySSM/downloadwBatch?productFamily=接入網(wǎng)&productType=OLT
下載結(jié)果如下:

簡(jiǎn)單的demo
package com.msznyl;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Download {
public static void main(String[] args) {
//需要壓縮的文件--包括文件地址和文件名
String [] path ={"E:\\360DocProtect\\01.txt","E:\\360DocProtect\\02.docx"};
// 要生成的壓縮文件地址和文件名稱
String desPath = "D:\\DOWNLOADS\\new.zip";
File zipFile = new File(desPath);
ZipOutputStream zipStream = null;
FileInputStream zipSource = null;
BufferedInputStream bufferStream = null;
try {
//構(gòu)造最終壓縮包的輸出流
zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
for(int i =0;i<path.length;i++){
File file = new File(path[i]);
//將需要壓縮的文件格式化為輸入流
zipSource = new FileInputStream(file);
//壓縮條目不是具體獨(dú)立的文件,而是壓縮包文件列表中的列表項(xiàng),稱為條目,就像索引一樣
ZipEntry zipEntry = new ZipEntry(file.getName());
//定位該壓縮條目位置,開(kāi)始寫入文件到壓縮包中
zipStream.putNextEntry(zipEntry);
//輸入緩沖流
bufferStream = new BufferedInputStream(zipSource, 1024 * 10);
int read = 0;
//創(chuàng)建讀寫緩沖區(qū)
byte[] buf = new byte[1024 * 10];
while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1)
{
zipStream.write(buf, 0, read);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//關(guān)閉流
try {
if(null != bufferStream) bufferStream.close();
if(null != zipStream) zipStream.close();
if(null != zipSource) zipSource.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用@EnableEurekaServer實(shí)現(xiàn)自動(dòng)裝配詳解
這篇文章主要介紹了Java使用@EnableEurekaServer實(shí)現(xiàn)自動(dòng)裝配過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10
java實(shí)現(xiàn)乘地鐵方案的最優(yōu)選擇(票價(jià),距離)
這篇文章主要介紹了java實(shí)現(xiàn)乘地鐵方案的最優(yōu)選擇(票價(jià),距離),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
如何用Java?幾分鐘處理完?30?億個(gè)數(shù)據(jù)(項(xiàng)目難題)
現(xiàn)有一個(gè) 10G 文件的數(shù)據(jù),里面包含了 18-70 之間的整數(shù),分別表示 18-70 歲的人群數(shù)量統(tǒng)計(jì),今天小編通過(guò)本文給大家講解如何用Java?幾分鐘處理完?30?億個(gè)數(shù)據(jù),這個(gè)問(wèn)題一直以來(lái)是項(xiàng)目難題,今天通過(guò)本文給大家詳細(xì)介紹下,感興趣的朋友一起看看吧2022-07-07
SpringBoot集成shiro,MyRealm中無(wú)法@Autowired注入Service的問(wèn)題
今天小編就為大家分享一篇關(guān)于SpringBoot集成shiro,MyRealm中無(wú)法@Autowired注入Service的問(wèn)題,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
java中Redisson的看門狗機(jī)制的實(shí)現(xiàn)
本文主要介紹了java中Redisson的看門狗機(jī)制的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
詳解Java使用JDBC連接MySQL數(shù)據(jù)庫(kù)
本文詳細(xì)講解了Java使用JDBC連接MySQL數(shù)據(jù)庫(kù)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
Java實(shí)現(xiàn)多個(gè)文檔合并輸出到一個(gè)文檔
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)多個(gè)文檔合并輸出到一個(gè)文檔的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

