JSF is used in recent projects. The requirement is to download a page to download files to the client. In addition, unlike downloading a single file, this download is a ZIP file, which is to first compress the files in the specified directory before downloading the page. The Code is as follows:
/**
* Automatic Program Packaging
* @ Throws IOException
*/
Public String getZipData (String fileName) throws IOException {
ZipOutputStream zos = null;
FileOutputStream fos = null;
DataOutputStream dos = null;
String zipFileName = ""; // -- compressed file name
String realInputFilePath = ""; // -- compressed file input stream path
Try {
String realNewFilePath = "d:/xxx"; // -- path of the compressed file
ZipFileName = realNewFilePath + fileName + ". zip ";
RealInputFilePath = "e:/xxx"; // -- path of the file to be compressed
File directory = new File (realInputFilePath );
File [] files = directory. listFiles (); // -- get all File names in the specified directory
If (null! = Files ){
Int fileLen = files. length;
Fos = new FileOutputStream (zipFileName); // -- path + file name after Packaging
Dos = new DataOutputStream (fos );
Zos = new ZipOutputStream (dos); // -- defines the zip stream.
For (int I = 0; I <fileLen; I ++ ){
FileInputStream FCM = null;
DataInputStream dis = null;
String tempfilePath = files [I]. getAbsolutePath (); // -- Obtain the absolute path + file name of each file
FCM = new FileInputStream (tempfilePath );
Dis = new DataInputStream (FCM );
Zos. putNextEntry (new ZipEntry (tempfilePath ));
Zos. setEncoding ("UTF-8"); // -- specifies the encoding, otherwise the file name is garbled
Int c = 0;
While (c = dis. read ())! =-1 ){
Zos. write (c );
}
Zos. closeEntry ();
If (null! = Fiis ){
FCM. 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;
}
Call this method in action
String zipFileName = batchDownloadService. getZipData (); // -- automatically package the file
BatchDownloadService. downloadFile (zipFileName, fileName, pagebatchDownloadModel); // -- download the file to the client
Methods In dao:
/**
* Download an object to the client
*/
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 FCM = new FileInputStream (zipFile );
String downloadZipName = fileName + "_" + "xx years _" + "xx months _" + ". 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 (FCM );
Int readBytes = 0;
Int buffer size = 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 ();
}
FCM. close ();
Outstream. close ();
FacesContext. getCurrentInstance (). responseComplete (); // -- critical; otherwise, the file is corrupted.
} Else {
Response. setCharacterEncoding ("UTF-8 ");
Response. setContentType ("text/html ");
// Response. getWriter (). write ("<script type = 'text/javascript '> alert ('no data is found for the current condition! ') </Script> ");
Response. getWriter (). write ("<script> location. href = '"+ request. getContextPath () + "/faces/sysMag/batchDownload/downloadFail. jsp "+" '</script> ");
FacesContext. getCurrentInstance (). responseComplete (); // -- critical; otherwise, the file is corrupted.
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
/**
* Delete the specified folder directory, that is, the files in the directory.
*/
Public void deleteDir (File dir ){
If (dir = null |! Dir. exists () |! Dir. isDirectory () // -- check parameters
Return;
For (File file: dir. listFiles ()){
If (file. isFile ())
File. delete (); // -- delete all files
Else if (file. isDirectory ())
DeleteDir (file); // you can call this operation to delete a folder.
}
Dir. delete (); // -- delete the directory itself
}