1. 程式人生 > 其它 >將zip檔案打包成jar_EasyUI+SpringMVC實現多檔案打包成壓縮包的下載的步驟

將zip檔案打包成jar_EasyUI+SpringMVC實現多檔案打包成壓縮包的下載的步驟

技術標籤:將zip檔案打包成jar迴圈buffer的實現獲取壓縮包裡面的檔名稱輸出圖片的檔案流 可以為空嗎

EasyUI+SpringMVC實現多檔案打包成壓縮包的下載的步驟

新增回款記錄詳情列表的匯款記錄列裡面有個檔案下載的按鈕

0ed9728d269b8d5f4cb3a277addd69fa.png
img

點選下載檔案,檔案就會打包成壓縮包,然後出現在瀏覽器頁面的左下角了

95b53e2a0f9ff738e4b9b22c60544fb3.png
img
點選並拖拽以移動

回款記錄的js程式碼

{
field:'caozuo',
title:'回款記錄',
width:'25%',
align:'center',
formatter:function(value,row,index){
varstr='';
varid=row.id;
return('+index+'">檔案下載+index+'">檔案列表');
}
}

檔案下載的ajax方法

大致意思:點選下載檔案的按鈕,根據var rows=datagrid.datagrid('getSelections');獲取到整個列表的資料,然後再var id = rows[0].id;獲取到匯款記錄id值,通過ajax呼叫根據id下載多個檔案的方法,判斷如果data裡面有返回“下載檔案為空的字串”,就提示下載檔案為空,否則跳轉到window.location.href="${ctx}/file/downLoadAttachment?filename="+origname+"&path="+serverpath;

vargridPanel=datagrid.datagrid("getPanel");
gridPanel.on("click","a.chakan",function(){
varrows=datagrid.datagrid('getSelections');
varid=rows[0].id;
$.ajax({
type:"get",
url:"${ctx}/file/downLoadAttachments?id="+id,
async:false,
success:function(data){
if(data=="下載檔案為空"){
$.messager.alert('溫馨提示!','下載檔案為空!','error');
}else{
vararr=data.split(",");
varorigname="";
varserverpath="";
serverpath=arr[0];
origname=arr[1];
serverpath=encodeURI(serverpath);
origname=encodeURI(origname);
window.location.href="${ctx}/file/downLoadAttachment?filename="+origname+"&path="+serverpath;
}
},
error:function(data){
}
});
})

FileController類的downLoadAttachments方法

/**
*檔案批量下載downAttachments
*新加的
*@throwsIOException
*/
@RequestMapping(value="/downLoadAttachments",produces="application/octet-stream",method=RequestMethod.GET)
@ResponseBody
publicStringdownAttachments(Stringid,HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException{
Listattachments=salesDetailsService.queryAttachmentsById(id);
String[]fileName=newString[attachments.size()];
Stringpath[]=newString[attachments.size()];for(inti=0;ifileName[i]=attachments.get(i).getFileName();
attachments.get(i).setFilePath(uploadDir+attachments.get(i).getFilePath());
path[i]=attachments.get(i).getFilePath();
}
StringretStr="";if(fileName.length>0&&path.length>0){
StringtemFileName="";//生成Zip檔名稱
StringFilePath=downloadDir;//生成Zip檔案路徑
temFileName=newDate().getTime()+".zip";
StringstrZipPath=FilePath+"\\"+temFileName;
retStr=strZipPath+","+temFileName;try{
FilefilePath=newFile(FilePath);if(!filePath.exists()){
filePath.mkdirs();
}
ZipOutputStreamout=newZipOutputStream(newFileOutputStream(strZipPath));byte[]buffer=newbyte[1024];//需要同時下載得檔案for(inti=0;iFile[]file={newFile(path[i])};for(intj=0;jif(file[j].exists()){
FileInputStreamfis=newFileInputStream(file[j]);
out.putNextEntry(newZipEntry(file[j].getName()));//設定壓縮檔案內得字元編碼,防止亂碼intlen;//讀入需要下載得檔案得內容,打包到zip檔案while((len=fis.read(buffer))>0){
out.write(buffer,0,len);
}
out.closeEntry();
fis.close();
}else{
System.out.println("下載檔案為空");
}
}
}
out.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}returnretStr;
}else{return"下載檔案為空";
}
}

FileController類的downLoadAttachment方法

/**
*檔案下載downAttachment
*@throwsIOException
*/
@RequestMapping(value="/downLoadAttachment")
@ResponseBody
publicvoiddownload(@RequestParam("filename")Stringfilename,
@RequestParam("path")Stringpath,HttpServletRequestrequest,HttpServletResponseresponse)throwsException{
//設定響應頭和客戶端儲存檔名
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition","attachment;fileName="+filename);
//用於記錄已完成的下載的資料量,單位是byte
try{
//開啟本地檔案流
InputStreaminputStream=newFileInputStream(path);
//System.out.println("=========================================="+inputStream);
//啟用下載操作
OutputStreamos=response.getOutputStream();

//迴圈寫入輸出流
byte[]b=newbyte[2048];
intlength;
while((length=inputStream.read(b))>0){
os.write(b,0,length);
}

//這裡主要關閉。
os.close();
inputStream.close();

}catch(Exceptione){
throwe;
}
//儲存記錄
}
9de62e9aabe94c8aeee197124bca1c53.png
img