/**
* 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 ();
}