public void goDownLoad() throws IOException, URISyntaxException{
// String urlPath ="http://b.hiphotos.baidu.com/pic/w%3D230/sign=1bb6c5bd8ad4b31cf03c93b8b7d7276f/21a4462309f79052c97999c00df3d7ca7acbd5ee.jpg";
URL _URL=new URL(urlPath);
HttpURLConnection con=(HttpURLConnection) _URL.openConnection();
InputStream fis= new BufferedInputStream(con.getInputStream());
//获取文件名
String trueurl=con.getURL().toString();
String filename=trueurl.substring(trueurl.lastIndexOf('/')+1);
this.getResponse().reset();
this.getResponse().addHeader("Content-Disposition",
"attachment;filename="
+ new String(filename.getBytes(),
"UTF-8"));
this.getResponse().addHeader("Content-Length", "" + con.getContentLength());
OutputStream toClient = new BufferedOutputStream(this.getResponse()
.getOutputStream());
this.getResponse().setContentType("application/octet-stream");
byte[] buffer = new byte[1024 * 1024 * 8];
int i = -1;
while ((i = fis.read(buffer)) != -1) {
toClient.write(buffer, 0, i);
}
fis.close();
toClient.flush();
toClient.close();
}
可下载网络图片,ppd,pdf等网络文件。