Java File download

Source: Internet
Author: User

Several ways to download files:

  1. How hyperlinks are ----Add a tag directly to the page to a link
Pros: Easy to develop and complete with hyperlinks.
Cons: Non-zipped packages of files (. Jpg,.png,.gif,.doc,.txt. ) is opened directly in the browser.
Recommendation: You can use this method when all the files are compressed.
  2, the way of using IO stream.

Jsp  
<href= "${pagecontext.request.contextpath}/downloadservlet2?fname=mm.jpg" > Beauty (mm.jpg)</a><br/>

 Public classDownloadservletextendsHttpServlet { Public voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//1. Receiving ParametersString fname = Request.getparameter ("FName"); //2. Create an input stream objectInputStream in =NewFileInputStream (Getservletcontext (). Getrealpath (fname)); //3. Create an output stream objectOutputStream out =Response.getoutputstream (); //4. Notify the browser to open the file in the form of attachment download.Response.setheader ("Content-disposition","Attachment;filename=" +fname); //5 performing IO operations        byte[] BTS =New byte[1024]; intLen =-1;  while(Len=in.read (BTS))!=-1) {out.write (BTS,0, Len); }        //6. Close the streamOut.close ();    In.close (); }     Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}

  3, the way of file forwarding
General files can be used in this way, if you encounter a large file or optimize the download efficiency has implemented the breakpoint continued to pass ... The recommended way to use IO operations.

//time Stamp Tool class Public classTimeutils { Public StaticString Getsimpletime () {Date Date=NewDate (); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyymmddhhmmsssss"); returnSdf.format (date); }     Public Staticstring getipsimpletime (String IP) {string ips[]= Ip.split ("."); String result= "";  for(inti = 0; i < ips.length; i++) {result+=Ips[i]; } Date Date=NewDate (); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyymmddhhmmsssss"); Result+=Sdf.format (date); returnresult; }
//Downloads for servlet Public classDownloadsaleinfosservletextendsHttpServlet { Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//1. Create Business Layer ObjectsOrderService OrderService = Basicfactory.getfactory (). getinstance (OrderService.class); //2, call Query method: The first 100list<saleinfo> list = Orderservice.findsaleinfos (100); //3. StitchingStringBuffer StringBuffer =NewStringBuffer ("Product ID, product name, total sales \ n");  for(Saleinfo saleinfo:list) {stringbuffer.append (saleinfo.getprod_id ()+","). Append (Saleinfo.getprod_name ()+","). Append (Saleinfo.getsale_num ()+ "\ n"); }        //4, tell the browser to download the way the attachmentString fname=Timeutils.getsimpletime (); Response.setheader ("Content-disposition", "attachment;filename=" +fname); //Handling garbled charactersResponse.setcontenttype ("TEXT/HTML;CHARSET=GBK"); //5, the results through the response to the browserresponse.getwriter (). Write (stringbuffer.tostring ()); }     Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}

Summarize:
If only a few compressed packages are downloaded, it is recommended to use a hyperlink.
If there are various types of files downloaded, the use of hyperlinks is not recommended;
You can choose the latter two ways, if the files are not too large, free choice; If the file wants to
Implementing some of the custom features (such as breakpoint continuation) is recommended for IO streaming.

------These kinds of ways are more primitive.

Java File download

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.