标签:
下载
从服务器拿取数据:(通用)
HttpURLConnection conn=null;
try{
URL url=new URL("http://172.....");
conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTime("5000");
conn.setReadTime("20000");
conn.connect(); ———提交;
InputStream is=conn.getInputStream(); ——接收数据
}
catch(Exception e){
e.printStackTrace();
}finally{
if(conn=null)
}
1、一般下载(和读取xml过程一样)
{
HttpURLConnection conn=null;
try{
URL url=new URL("http://172...../want.zip"); 也可以xxx.xml等。
conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTime("5000");
conn.setReadTime("20000");
conn.connect(); ———提交;
InputStream is=conn.getInputStream(); ——接收数据
String savepath="d:\\test.zip";
File file=new File(savepath);
RandomAccessFile raf=new RandomAccessFile(file,"rw");
do{
byte[] buffer=new byte[256];
int length=is.read(buffer,0,buffer.length);
if(length<=0)break;
raf.write(buffer,0,length);
}while(true);
is.close();
raf.close();
}
catch(Exception e){
e.printStackTrace();
}finally{
if(conn!=null){conn.disconnect();}
}
}
2、多线程下载
public void main(String[] args){
HttpURLConnection conn=null;
try{
URL url=new URL("http://172...../want.zip"); 也可以xxx.xml等。
conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTime("5000");
conn.setReadTime("20000");
conn.connect(); ———提交;
long size=conn.getcontentLengthLong(); ——获取文件大小。
String savepath="d:\\test.zip";
File file=new File(savepath);
RandomAccessFile raf=new RandomAccessFile(file,"rw");
raf.setLength(size); ——先设定空白文件的大小,以确保指针可以移动。
//计算单个线程读取的开始和结束
int threadcount=3
long threadsize=size/threadcount;
for(int i=1;i<=3;i++)
{
long start=(i-1)*threadsize;
long end=i*threadsize-1;
if(i=threadcount){end=size-1};
new DownloadThread(start,end).start();
}
}catch(Exception e){
e.printStackTrace();
}
}
class DownloadThread extends Thread(){
private long atart;
private long end;
public DownloadThread(long start,long end){
this.start=start;
this.end=end;
}
public void run(){
HttpURLConnection conn=null;
try{
URL url=new URL("http://172...../want.zip"); 也可以xxx.xml等。
conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTime("5000");
conn.setReadTime("20000");
conn.setRequestProperty("Range","byte="+start+"-"+end); ——设定读取文件的开始和结束位置。
conn.connect(); ———提交;
InputStream is=conn.getInputStream(); ——接收数据
String savepath="d:\\test.zip";
File file=new File(savepath);
RandomAccessFile raf=new RandomAccessFile(file,"rwd"); “--d”代表多线程读写
raf.seek(start); ————移动指针到要保存的位置。
do{
byte[] buffer=new byte[256];
int length=is.read(buffer,0,buffer.length);
if(length<=0)break;
raf.write(buffer,0,length);
}while(true);
is.close();
raf.close();
}
catch(Exception e){
e.printStackTrace();
}finally{
if(conn!=null){conn.disconnect();}
}
}
}
3、通过开源框架下载 xutils
public void download(View view ){
HttpUtils http=new HttpUtils();
String path=this.getFilesDir().getAbsolutePath()+"/test.zip"; --this.getFilesDir()在主包名下建一个files的文件夹。并返回详细信息
http.download("http://...../test.zip",path,
new RequestCallBack<File>(){
public void onStart(){
super.onStart();
tv.setText("开始下载了");
}
//正在下载时一直调用的
public void onLoading(long total,long current,boolean isUploading){
super.onLoading(total, current ,isUploading);
double precent=current*1.0/total*100; --当前下载量 除以 总下载量 转成double类型
tv.setText("下载了:"+precent+"%");
--- 进度条.setProgress((int)precent);
}
public void onSuccess(ResponseInfo<File> responseInfo){
tv.setText("下载成功");
}
public void onFailure(HttpException error,String msg){
tv.setText("下载失败");
}
});
}
标签:
原文地址:http://www.cnblogs.com/zjw0914/p/5128445.html