最近的項目上用到的是JSF,需求是做一個頁面在的下載,實現可以下載文件到客戶端。並且和單獨的文件下載不同的是,此下載是ZIP文件,就是先把指定目錄下的文件先壓縮,再進行頁面下載。具體代碼如下:
/**
* 程序自動打包
* @throws IOException
*/
public String getZipData(String fileName) throws IOException{
ZipOutputStream zos = null;
FileOutputStream fos = null;
DataOutputStream dos = null;
String zipFileName = ""; //--壓縮的文件名
String realInputFilePath = ""; //--壓縮文件輸入流路徑
try{
String realNewFilePath = "d:/xxx"; //--壓縮文件的路徑
zipFileName = realNewFilePath + fileName + ".zip";
realInputFilePath ="e:/xxx"; //--要壓縮文件的路徑
File directory = new File(realInputFilePath);
File[] files = directory.listFiles(); //--得到指定目錄下所有文件名
if(null != files){
int fileLen = files.length;
fos = new FileOutputStream(zipFileName); //--打包後路徑+ 文件名
dos = new DataOutputStream(fos);
zos = new ZipOutputStream(dos); //--定義zip輸流
for(int i=0; i<fileLen; i++){
FileInputStream fis = null;
DataInputStream dis = null;
String tempfilePath = files[i].getAbsolutePath(); //--得到每個文件絕對路徑+ 文件名
fis = new FileInputStream(tempfilePath);
dis = new DataInputStream(fis);
zos.putNextEntry(new ZipEntry(tempfilePath));
zos.setEncoding("UTF-8"); //--指定編碼,否則文件名亂碼
int c=0;
while((c=dis.read()) != -1){
zos.write(c);
}
zos.closeEntry();
if(null != fis){
fis.close();
}
if(null != dis){
dis.close();
}
}
}
}catch(Exception e){
e.printStackTrace();
} finally{
if(null != zos){
zos.close();
}
if(null != dos){
dos.close();
}
if(null != fos){
fos.close();
}
}
return zipFileName;
}
在action中調用此方法
String zipFileName = batchDownloadService.getZipData(); //--進行文件的自動打包
batchDownloadService.downloadFile(zipFileName,fileName,pagebatchDownloadModel); //--下載文件到客戶端
dao中的方法:
/**
* 下載文件到客戶端
*/
public void downloadFile(String zipFileName,String fileName,BatchDownloadModel pagebatchDownloadModel){
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
try{
File zipFile = new File(zipFileName);
if(zipFile.exists()){
ServletOutputStream outstream =response.getOutputStream();
FileInputStream fis = new FileInputStream(zipFile);
String downloadZipName = fileName+"_"+"xx年_" + "xx月_" +".zip";
response.setContentType("application/zip");
response.setHeader("Transfer-Encoding", "chunked");
response.addHeader("Content-disposition", "attachment;filename="+new String(downloadZipName.getBytes("GB2312"),"ISO8859-1") );
BufferedInputStream bufInStrm = new BufferedInputStream (fis);
int readBytes = 0;
int bufferSize = 8192;
byte[] buffer = new byte[bufferSize];
while ((readBytes = bufInStrm.read(buffer)) != -1){
if (readBytes == bufferSize) {
outstream.write(buffer);
}else{
outstream.write(buffer, 0, readBytes);
}
outstream.flush();
response.flushBuffer();
}
fis.close();
outstream.close();
FacesContext.getCurrentInstance().responseComplete(); //--非常關鍵,否則提示文件損壞
}else{
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
//response.getWriter().write("<script type='text/javascript'>alert('當前條件未查詢到任何數據!')</script>");
response.getWriter().write("<script>location.href='"+request.getContextPath() + "/faces/sysMag/batchDownload/downloadFail.jsp"+"'</script>");
FacesContext.getCurrentInstance().responseComplete(); //--非常關鍵,否則提示文件損壞
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 刪除指定文件夾目錄即目錄中文件
*/
public void deleteDir(File dir) {
if (dir == null || !dir.exists() || !dir.isDirectory()) //-- 檢查參數
return;
for (File file : dir.listFiles()) {
if (file.isFile())
file.delete(); //--刪除所有文件
else if (file.isDirectory())
deleteDir(file); //--遞規的方式刪除文件夾
}
dir.delete(); //--刪除目錄本身
}