您的位置:首页 > 理论基础 > 计算机网络

java下载网络文件, ppt,pdf,图片

2013-10-28 16:39 756 查看
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等网络文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: