java HttpURLConnection下载服务器上的文件
2012-12-07 16:15
561 查看
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;
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();
}
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 多线程下载文件 以及URLConnection和HttpURLConnection的区别
- [置顶] Android开发之下载服务器上的一张图片到本地java代码实现HttpURLConnection
- 运用java.net.HttpURLConnection实现java多线程下载文件
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- 【JAVA】通过HttpURLConnection 上传和下载文件(二)
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- java上传文件到远程服务器(一)---HttpURLConnection方式
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- JAVA通过HttpURLConnection 上传和下载文件的方法
- 用http协议下载文件(HttpURLConnection)java
- java网络编程____httpURLConnection上传下载文件
- Android开发之下载服务器上的一张图片到本地java代码实现HttpURLConnection
- 【JAVA】通过HttpURLConnection 上传和下载文件
- Java使用HttpURLConnection上传文件
- Java Web 学习笔记之三: HttpURLConnection 模拟表单上传文件
- Android程序开发通过HttpURLConnection上传文件到服务器
- Java客户端通过HttpURLConnection连接Web服务器的Session保存问题
- Java: HttpURLConnection 上传文件_20160912_七侠镇莫尛貝
- Java: HttpURLConnection 上传文件_20160912_七侠镇莫尛貝
- JAVA使用HttpUrlConnection实现自动上传文件