"File Download" Java download files in several ways

Source: Internet
Author: User

1. Download it in a streaming fashion.
  Public httpservletresponse Download (String path, httpservletresponse response) {try {//path = file to download            The path.            File File = new file (path);            Gets the file name.            String filename = File.getname ();            Gets the suffix name of the file.            String ext = filename.substring (Filename.lastindexof (".") + 1). toUpperCase ();            Download the file in the form of a stream.            InputStream fis = new Bufferedinputstream (new FileInputStream (path));            byte[] buffer = new byte[fis.available ()];            Fis.read (buffer);            Fis.close ();            Empty response Response.reset (); Set Response header Response.AddHeader ("Content-disposition", "attachment;filename=" + New String (filename.get            Bytes ()));            Response.AddHeader ("Content-length", "" "+ File.length ());            OutputStream toclient = new Bufferedoutputstream (Response.getoutputstream ());            Response.setcontenttype ("Application/octet-stream"); Toclient.wrITE (buffer);            Toclient.flush ();        Toclient.close ();        } catch (IOException ex) {ex.printstacktrace ();    } return response; }
2. Download Local Files
public void downloadlocal (HttpServletResponse response) throws FileNotFoundException {        //download local file        String FileName = "Operator.doc". toString (); The default save name for the file//        read into the stream        inputstream instream = new FileInputStream ("C:/operator.doc");//file storage path        //Set output format        Response.reset ();        Response.setcontenttype ("bin");        Response.AddHeader ("Content-disposition", "attachment; Filename=\ "" + fileName + "\" ");        Loop out the data in the stream        byte[] b = new byte[100];        int Len;        try {while            (len = Instream.read (b)) > 0)                response.getoutputstream (). Write (b, 0, Len);            Instream.close ();        } catch (IOException e) {            e.printstacktrace ();        }    }
3. Download network files
public void Downloadnet (HttpServletResponse response) throws Malformedurlexception {        //download network file        int bytesum = 0;< C2/>int byteread = 0;        URL url = new URL ("Windine.blogdriver.com/logo.gif");        try {            URLConnection conn = Url.openconnection ();            InputStream instream = Conn.getinputstream ();            FileOutputStream fs = new FileOutputStream ("C:/abc.gif");            byte[] buffer = new byte[1204];            int length;            while ((Byteread = instream.read (buffer))! =-1) {                bytesum + = Byteread;                System.out.println (bytesum);                Fs.write (buffer, 0, byteread);            }        } catch (FileNotFoundException e) {            e.printstacktrace ();        } catch (IOException e) {            e.printstacktrace (); c19/>}    }
4. Support for Online open mode
  public void DownLoad (String filePath, HttpServletResponse response, Boolean isOnLine) throws Exception {File F =        New File (FilePath);            if (!f.exists ()) {Response.senderror (404, "File not found!");        Return        } bufferedinputstream br = new Bufferedinputstream (new FileInputStream (f));        byte[] buf = new byte[1024];        int len = 0; Response.reset ();            Very important if (IsOnLine) {//online open mode url u = new URL ("file:///" + FilePath);            Response.setcontenttype (U.openconnection (). getContentType ()); Response.setheader ("content-disposition", "inline;            Filename= "+ f.getname ());            The file name should be encoded as UTF-8} else {//Pure download mode Response.setcontenttype ("Application/x-msdownload"); Response.setheader ("Content-disposition", "attachment;        Filename= "+ f.getname ());        } OutputStream out = Response.getoutputstream (); while (len = Br.read (buf)) > 0) OUT.WRite (buf, 0, Len);        Br.close ();    Out.close (); }

"File Download" Java download files in several ways

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.