JAVA File Download

Source: Internet
Author: User

/**
* Local download
*
* @ Param response
* @ Throws FileNotFoundException
*/
Public void downloadLocal (HttpServletResponse response)
Throws FileNotFoundException {
// Download the local file
String fileName = "Operator.doc". toString (); // default file storage name
// Reading the stream
InputStream inStream = new FileInputStream ("c:/Operator.doc"); // file storage path
// Set the output format
Response. reset ();
Response. setContentType ("bin ");
Response. addHeader ("Content-Disposition", "attachment; filename = \""
+ FileName + "\"");
// Cyclically retrieve 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 ();
}
}

/**
* Download network files
*
* @ Param response
* @ Throws MalformedURLException
*/
Public void downloadNet (HttpServletResponse response)
Throws MalformedURLException {
// Download the Network File
Int bytesum = 0;
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];
@ SuppressWarnings ("unused ")
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 ();
}
}

/**
* Online open download is supported.
*
* @ Param filePath
* @ Param response
* @ Param isOnLine
* @ Throws Exception
*/
Public void downLoad (String filePath, HttpServletResponse response,
Boolean isOnLine, String fname) throws Exception {
System. out. println ("filePath:" + filePath );
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 [1, 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 ="
+ Fname );
// The file name should be encoded into a UTF-8
} Else {// download method only
Response. setContentType ("application/x-msdownload ");
Response. setHeader ("Content-Disposition", "attachment; filename ="
+ Fname );
}
OutputStream out = response. getOutputStream ();
While (len = br. read (buf)> 0)
Out. write (buf, 0, len );
Br. close ();
Out. close ();
}

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.