Java code Implementation to package multiple file download functions

Source: Internet
Author: User

Pass in the corresponding file collection object that needs to be packaged

When the file path is published to the production environment, it can be changed to

*/
File File = new file (Request.getsession (). Getservletcontext (). Getrealpath ("/qrcode.ara"));
if (!file.exists ()) {
File.createnewfile ();
}
Response.reset ();
Response.getwriter ()
Creating a file output stream
FileOutputStream fous = new FileOutputStream (file);
/** Packaging Method We will use a zipoutputstream such an output stream,
* So here we convert the output stream */
Org.apache.tools.zip.ZipOutputStream ZipOut
= new Org.apache.tools.zip.ZipOutputStream (fous);
Zipoutputstream ZipOut
= new Zipoutputstream (fous);
/** This method accepts a collection of files to be packaged,
* There's a zipoutputstream*/
ZipFile (Files, zipout);
Zipout.close ();
Fous.close ();
Return Downloadzip (File,response);
}catch (Exception e) {
E.printstacktrace ();
}
/** until the file has been packaged successfully,
* The packaging process of the file was encapsulated in the static method of Fileutil.zipfile,
* Will be presented later, the next is to write data to the client */
OutputStream out = Response.getoutputstream ();


return response;
}

/**
* To compress all accepted documents into a compressed package
* @param list<file>;
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void ZipFile
(List files,zipoutputstream OutputStream) {
int size = Files.size ();
for (int i = 0; i < size; i++) {
File File = (file) files.get (i);
ZipFile (file, outputstream);
}
}

public static HttpServletResponse Downloadzip (File file,httpservletresponse response) {
try {
Download the file in the form of a stream.
InputStream fis = new Bufferedinputstream (New FileInputStream (File.getpath ()));
byte[] buffer = new byte[fis.available ()];
Fis.read (buffer);
Fis.close ();
Empty response
Response.reset ();

OutputStream toclient = new Bufferedoutputstream (Response.getoutputstream ());
Response.setcontenttype ("Application/octet-stream");
Response.setheader ("Content-disposition", "attachment;filename=" + file.getname ());
Toclient.write (buffer);
Toclient.flush ();
Toclient.close ();
} catch (IOException ex) {
Ex.printstacktrace ();
}finally{
try {
File F = new file (File.getpath ());
F.delete ();
} catch (Exception e) {
E.printstacktrace ();
}
}
return response;
}

/**
* Package files according to the input file and output stream
* @param File
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void ZipFile (File inputfile,
Zipoutputstream Ouputstream) {
try {
if (inputfile.exists ()) {
/** if it is a directory, here is not to take action,
* As for the packaging of the catalogue is under study */
if (Inputfile.isfile ()) {
FileInputStream in = new FileInputStream (inputfile);
Bufferedinputstream bins = new Bufferedinputstream (in, 512);
Org.apache.tools.zip.ZipEntry
ZipEntry entry = new ZipEntry (Inputfile.getname ());
Ouputstream.putnextentry (entry);
outputting data to a compressed file
int nnumber;
byte[] buffer = new BYTE[512];
while ((Nnumber = bins.read (buffer))! =-1) {
Ouputstream.write (buffer, 0, nnumber);
}
Close the created Stream object
Bins.close ();
In.close ();
} else {
try {
file[] files = inputfile.listfiles ();
for (int i = 0; i < files.length; i++) {
ZipFile (Files[i], ouputstream);
}
} catch (Exception e) {
E.printstacktrace ();
}
}
}
} catch (Exception e) {
E.printstacktrace ();
}
}

Original Address http://blog.csdn.net/hbhgjiangkun/article/details/7282629

Java code Implementation to package multiple file download functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.