java壓縮多個(gè)文件并且返回流示例
這個(gè)類可以壓縮多個(gè)文件并且返回流,在程序中可以再操作返回的流做其它功能,比如驗(yàn)證MD5,下面看代碼吧
/**
* 方法描述:<b>測(cè)試類</b></br>
*/
public class TestFileStream{
//文件和壓縮包存儲(chǔ)的位置
StringtempFilePath="C:/temp/"
List<String>fileList=newArrayList<String>();
fileList.add(tempFilePath+"file1.txt");
fileList.add(tempFilePath+"file2.png");
fileList.add(tempFilePath+"file3.xls");
//生成的壓縮包名稱
StringzipName="fileData";
//返回流
ByteArrayOutputStreamoutputStream=fileToZip(fileList,fileData,tempFilePath);
//頁(yè)面輸入壓縮包流
byte[]buffer=outputStream.toByteArray();
//清空response
response.reset();
//設(shè)置response的Header
response.addHeader("Content-Disposition",
"attachment;filename="+
newString(("dataFile.zip").getBytes("gb2312"),"ISO8859-1"));
response.addHeader("Content-Length",""+outputStream.size());
toClient=newBufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
}
/**
*方法描述:<b>將多個(gè)文件壓縮成zip包</b></br>
*/
publicByteArrayOutputStreamfileToZip(List<String>fileList,StringzipName,StringtempFilePath){
byte[]buffer=newbyte[1024];
ZipOutputStreamout=null;
try{
out=newZipOutputStream(newFileOutputStream(tempFilePath+zipName+".zip"));
List<File>filedata=newArrayList<File>();
for(inti=0,len=fileList.size();i<len;i++)
{
filedata.add(newFile(fileList.get(i)));
}
for(intj=0,len=filedata.size();j<len;j++)
{
FileInputStreamfis=newFileInputStream(filedata.get(j));
out.putNextEntry(newZipEntry(filedata.get(j).getName()));
intdataLen;
//讀入需要下載的文件的內(nèi)容,打包到zip文件
while((dataLen=fis.read(buffer))>0){
out.write(buffer,0,dataLen);
}
out.closeEntry();
fis.close();
}
out.close();
}
catch(Exceptionex)
{
ex.printStackTrace();
}
//讀取壓縮包
Filefilezip=newFile(tempFilePath+zipName+".zip");
ByteArrayOutputStreambaos=null;
try
{
baos=newByteArrayOutputStream();
FileInputStreaminStream=newFileInputStream(filezip);
BufferedInputStreambis=newBufferedInputStream(inStream);
intc=bis.read();
while(c!=-1){
baos.write(c);
c=bis.read();
}
bis.close();
inStream.close();
}
catch(Exceptionex)
{
ex.printStackTrace();
}
returnbaos;
}
相關(guān)文章
大數(shù)組元素差異removeAll與Map效率對(duì)比
這篇文章主要介紹了大數(shù)組元素差異removeAll與Map效率對(duì)比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Spring Data JPA+kkpager實(shí)現(xiàn)分頁(yè)功能實(shí)例
本篇文章主要介紹了Spring Data JPA+kkpager實(shí)現(xiàn)分頁(yè)功能實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06Java System.getProperty()-獲取系統(tǒng)參數(shù)案例詳解
這篇文章主要介紹了Java System.getProperty()-獲取系統(tǒng)參數(shù)案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08詳解如何獲取PreparedStatement參數(shù)示例詳解
這篇文章主要為大家介紹了詳解如何獲取PreparedStatement參數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Java之BigDecimal實(shí)現(xiàn)詳解
這篇文章主要介紹了Java之BigDecimal實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01SpringMVC詳解如何映射請(qǐng)求數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于SpringMvc映射請(qǐng)求數(shù)據(jù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-06-06