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等网络文件。
// 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等网络文件。
相关文章推荐
- 计算机网络笔试题
- 基于IHttpAsyncHandler的TCP收发器
- Android 判断网络连接状态
- (android 功能代码)Android网络请求库——android-async-http .
- 电脑同时支持两个网络doc命令
- debian文本配置网络备忘:/etc/network/interfaces
- SurfaceView播放网络电视动态设置屏幕大小
- TCP的TIME_WAIT状态
- HTTP协议 (六) 状态码详解
- HTTP协议 (六) 状态码详解
- HTTP错误总节400 403 401 404
- Android判断网络是否可用
- HTTP 协议详解
- HTTP 协议详解
- Apache Tomcat配置gzip压缩(HTTP压缩)功能
- [Android] AsyncTask使用实例---加载网络图片
- Eclipse安装ADT Plugin时发生错误,提示连接不到 https://dl-ssl.google.com/android/eclipse/...的解决办法
- 单机触摸屏翻书系统网络广告机
- 基于TCP和UDP的聊天功能的实现
- 有关TCP和UDP 粘包 消息保护边界