Recently in the project to see some information about uploading and downloading, after the test passed, save a spare
String filename = request.getparameter ("filename");
FileName is the path to the file resource,
Integer num = Filename.lastindexof (".");
Get the format of a file
String fileType = "";
if (num>0) {
FileType = filename.substring (num+1, Filename.length ());
}else{
FileType = "png";
}
Response.setheader ("Content-disposition", "attachment;filename=" + urlencoder.encode ("mz" +filetype, "UTF-8"));
Response.setcontenttype ("Application/octet-stream");
URL url = new URL (filename);
Open connection
URLConnection con = url.openconnection ();
Con.setconnecttimeout (2000);
Con.setdefaultusecaches (FALSE);
SYSTEM.OUT.PRINTLN ("open link");
Input stream
InputStream is = Con.getinputstream ();
1K of data buffering
Byte[] bs = new byte[1024];
Length of data read
int Len;
The output file stream
OutputStream OS = Response.getoutputstream ();
Start reading
while (len = Is.read (BS))! =-1) {
Os.write (BS, 0, Len);
}
Complete, close all links
Os.close ();
Is.close ();
System.out.println ("Download done ..............) ");
Java Web download URLConnection