Java file download (with very detailed comments).

Source: Internet
Author: User

Not much to say, direct sticker code:

    File download public static String downloadFile (file file) {HttpServletResponse response = Servletactioncontex            T.getresponse ();             FileInputStream FIS = null;            Bufferedinputstream buff = null;            OutputStream out = null; try {/* If the file exists */if (file.exists ()) {//set to no cache response                   . Reset ();                     Set the encoding method of the response//response.setcontenttype ("Application/x-download"); Response.setcontenttype ("Application/ms-excel"); This sentence is more detailed, tell the browser to download the Excel file//settings download file name Response.setheader ("Content-disposition", "Filena                   Me= "+new String (File.getname (). GetBytes ()," UTF-8 "));                   read out file to I/O stream fis=new fileinputstream (file);                   Buff=new Bufferedinputstream (FIS);                   Get the output stream from the response object, ready to download out = Response.getoutputstream (); //PrintWriter out = Response.getwriter (); I can write anything in bytes.//Writes the contents of int i;                    while ((i = Buff.read ())! =-1) {out.write (i);                  }//Push the contents into the document Out.flush (); }else{return "Download fail";//File Not Present}} catch (Exception e) {//To Do:handle exception}finally{try {if (buff! = null) b                         Uff.close ();                    if (out! = null) out.close ();               } catch (IOException e) {return "download fail";    }} return "Download Success"; }


Java file download (with very detailed comments).

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.