工作中遇到了這個問題,上傳的時候用戶可以一次性上傳多個附件,這樣下載的時候就有問題了,一個文件還好下載,要是多個附件的話那就不好弄了,在網上找了一下沒找到一個下載多個文件的好的解決方法,於是只好先把這些附件打包然後下載下來了.這裏面有文件名爲中文的時候亂碼的問題,網上有兩個解決方法,第一:使用apache的ant.jar包,而不用JDK自帶的,我用的就是這種方法,第二種就不多說了,大家可以去搜搜.
/**
* 將多個模版打包成一個壓縮包
* @param excelUpLoadIndexBoList
*/
public void getZipFile(List<ExcelUpLoadIndexBo> excelUpLoadIndexBoList,String outPutFilePath)
{
try
{
BufferedReader in;
//創建文件輸出流對象
FileOutputStream out = new FileOutputStream(outPutFilePath);
ZipOutputStream zipOut = new ZipOutputStream(out); //創建ZIP數據輸出流對象
ZipEntry entry;
int n;
int len = excelUpLoadIndexBoList.size();
for(int i=0; i<len;i++)//創建文件輸入流對象
{
String filefullName = excelUpLoadIndexBoList.get(i).getFileName();
String filePath = excelUpLoadIndexBoList.get(i).getFilePath();
filePath = filePath + File.separator + filefullName;
in= new BufferedReader(
new InputStreamReader(new FileInputStream(filePath),"ISO8859-1"));
entry = new ZipEntry(filefullName);
zipOut.putNextEntry(entry);
//向壓縮文件中輸出數據
while((n=in.read()) != -1)
{
zipOut.write(n);
}
in.close();
}
zipOut.close();
out.close();
}catch(Exception e)
{
System.out.println(e);
}
}