java HttpURLConnection下载服务器上的文件
来源:互联网 发布:编程谜题 编辑:程序博客网 时间:2024/04/27 20:02
String str="http://127.0.0.1:8080/httpFile/test.txt";
System.out.println("urlstr---->"+str);
try {
URL url = new URL(str);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Charset","UTF-8");
connection.connect();
int file_leng = connection.getContentLength();
System.out.println("file length---->"+file_leng);
BufferedInputStream bin = new BufferedInputStream(connection.getInputStream());
String path=Environment.getExternalStorageDirectory()+"/eBook/inw/"+bookName+".inw";
File file = new File(path);
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
OutputStream out=new FileOutputStream(file);
int size=0;
int len=0;
byte[] buf = new byte[1024];
while((size=bin.read(buf))!=-1){
len+=size;
msg.arg1=len*100/file_leng;
System.out.println("下载了: "+len*100/file_leng+"%\n");
}
bin.close();
out.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("urlstr---->"+str);
try {
URL url = new URL(str);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Charset","UTF-8");
connection.connect();
int file_leng = connection.getContentLength();
System.out.println("file length---->"+file_leng);
BufferedInputStream bin = new BufferedInputStream(connection.getInputStream());
String path=Environment.getExternalStorageDirectory()+"/eBook/inw/"+bookName+".inw";
File file = new File(path);
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
OutputStream out=new FileOutputStream(file);
int size=0;
int len=0;
byte[] buf = new byte[1024];
while((size=bin.read(buf))!=-1){
len+=size;
out.write(buf,0,1024);
/* android 中为了更新进度条
Message msg = handler.obtainMessage();msg.arg1=len*100/file_leng;
handler.sendMessage(msg);
*/
Thread.sleep(1000);System.out.println("下载了: "+len*100/file_leng+"%\n");
}
bin.close();
out.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
- java HttpURLConnection下载服务器上的文件
- java下载服务器上的文件
- Android开发之下载服务器上的一张图片到本地java代码实现HttpURLConnection
- java 远程下载服务器上的固定文件
- java方法客户端下载服务器上的文件到本地
- java方法客户端下载服务器上的文件到本地
- java方法客户端下载服务器上的文件到本地
- 下载服务器上的文件的jsp
- SecureCRT下载Linux服务器上的文件
- Flex 下载服务器上的文件
- php下载服务器上的文件
- Flex 下载服务器上的文件
- 读取,下载,linux服务器上的 文件
- 通过url下载服务器上的文件
- js 下载服务器上的文件
- JAVA 单个或多个下载服务器上带超链接的文件 并压缩后下载
- 【JAVA】通过HttpURLConnection 上传和下载文件
- web端下载服务器上的文件,js方法下载
- Eclipse中使用MiddleGenIDE插件
- Windows Phone SDK 8.0中文版已开放下载
- POJ 1273 Drainage Ditches
- 用VB做报表(一)
- NoSQL数据库学习之MongoDB之group by 限制
- java HttpURLConnection下载服务器上的文件
- QT 无边框可拖动改变size窗口实现
- 文章5:http协议中content-length 以及chunked编码分析
- 搭建一个免费的,无限流量的Blog----github Pages和Jekyll入门
- 利用一切资源搭建hadoop
- SQL Server性能监控
- 通过预处理头文件加快VS2008的编译速度
- Linux下scp的用法
- springmvc+HttpInvok远程调用