Java code Implementation to download individual files, and download packaged files

Source: Internet
Author: User

To download a single file call method

/**
* Response
* Imgpath Download Image address
* FileName Save download file name
* @date April 14, 2015 5:53:24
*/
public static void Download (HttpServletResponse response,string imgpath,string fileName) {
OutputStream Out=null;
Bufferedinputstream BR =null;
try {
Set Response type to download
Response.setcontenttype ("Application/x-msdownload;charset=utf-8");
Page garbled problem
Response.setcharacterencoding ("UTF-8");
Set Download file name
Response.setheader ("Content-disposition", "attachment; Filename= "+filename);
Input stream
br = new Bufferedinputstream (new FileInputStream (Imgpath));
byte[] buf = new byte[1024];
int len = 0;
out = Response.getoutputstream ();
while (len = Br.read (buf)) > 0) {
Out.write (buf, 0, Len);
Out.flush ();
}
if (null!=out) out.close ();
if (NULL!=BR) br.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}

Download package compressed file Call method

/**
*files need to download the file collection for the Ask Price
* @Description: TODO (file package download)
*/
public static HttpServletResponse Downloadfiles (list<file> files,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
try {
ServletContext context=request.getsession (). Getservletcontext ();
/** This collection is all the files you want to pack,
* This assumes that you have prepared the file you want to pack */
list<file> files = new arraylist<file> ();

/** Create a temporary compressed file,
* We're going to inject all the file streams into this file.
* Here's the file you can customize is. rar or. zip*/

File File = new file (Context.getrealpath ("/test.rar"));
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 */
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 ();
}
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[1024];
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 ();
}
}

Java code Implementation to download individual files, and download packaged files

Related Article

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.