注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

姑射道人的博客

博客新地址:nixuchen.com

 
 
 

日志

 
 

文件下载  

2012-07-12 13:21:25|  分类: android |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

package UnitTest;


import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.RandomAccessFile;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;


import junit.framework.TestCase;


import org.junit.Test;


public class InternetDownloadTest  {

public void getImage() throws Exception{

String urlpath="http://localhost:8080/website/images/1.jpg";

URL url = new URL(urlpath);

HttpURLConnection conn= (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

conn.setConnectTimeout(6*1000);

if(conn.getResponseCode()==200){

InputStream inputStream=conn.getInputStream();

File file =new File("laozi.jpg");

byte[] data=readInstream(inputStream);

FileOutputStream outputStream=new FileOutputStream(file);

outputStream.write(data);

outputStream.close();

System.out.println("excuted");

}

}

public byte[] readInstream(InputStream inputStream)throws Exception{

ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();

byte[] buffer=new byte[1024];

int length=-1;

while((length=inputStream.read(buffer))!=-1){

byteArrayOutputStream.write(buffer,0,length);

}

;

byteArrayOutputStream.close();

inputStream.close();

return byteArrayOutputStream.toByteArray();

}

public void getHtml() throws Exception{

String urlpath="http://www.sohu.com";

URL url=new URL(urlpath);

HttpURLConnection conn=(HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

conn.setConnectTimeout(6*1000);

if(conn.getResponseCode()==200){

InputStream inputStream=conn.getInputStream();

byte[] data=readInstream(inputStream);

System.out.println(new String(data));

}

}

public void getFile() throws Exception{

String urlpath="http://localhost:8080/website/tomcat.exe";

URL url=new URL(urlpath);

HttpURLConnection conn=(HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

conn.setConnectTimeout(6*1000);

if(conn.getResponseCode()==200){

InputStream inputStream=conn.getInputStream();

byte[] data=readInstream(inputStream);

File file=new File("tomcat.exe");

FileOutputStream outputStream=new FileOutputStream(file);

outputStream.write(data);

outputStream.close();

}

}


public void multiDownload() throws Throwable{

String filename="1.mp4";

String path="http://localhost:8080/website/video/1.mp4";

URL url=new URL(path);

HttpURLConnection conn=(HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5*1000);

conn.setRequestMethod("GET");

int filelength=conn.getContentLength();

System.out.println(filelength);

RandomAccessFile file=new RandomAccessFile(filename,"rw");

file.setLength(filelength);

file.close();

conn.disconnect();

int threadsize=3;

int threadlength=filelength%threadsize==0?filelength/threadsize:filelength/threadsize+1;

for(int i=0;i<threadlength;i++){

int startPosition=i*threadlength;

RandomAccessFile threadfile=new RandomAccessFile(filename,"rw");

threadfile.seek(startPosition);

new DownloadThread(i, path, startPosition, threadfile, threadlength).start();

}

int quit=System.in.read();

while('q'!=quit){

Thread.sleep(2*1000);

}

}

private class DownloadThread extends Thread{

private int threadid;

private int startposition;

private RandomAccessFile threadfile;

private int threadlength;

private String path;

public DownloadThread(int threadid,String path,int startposition,

RandomAccessFile threadfile,int threadlength){

this.threadid=threadid;

this.startposition=startposition;

this.threadfile=threadfile;

this.threadlength=threadlength;

this.path=path;

}

@Override

public void run() {

// TODO Auto-generated method stub

try {

URL url=new URL(path);

HttpURLConnection conn=(HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5*1000);

conn.setRequestMethod("GET");

conn.setRequestProperty("Range", "byte="+startposition+"-");

InputStream inStream=conn.getInputStream();

byte[] buffer=new byte[1024];

int len=-1;

int length=0;

while(length<threadlength&&(len=inStream.read(buffer))!=-1){

threadfile.close();

inStream.close();

System.out.println("线程"+(threadid+1)+"已经下载完成");

}

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

System.out.println("线程"+(threadid+1)+"下载出错:"+e);

e.printStackTrace();

}

}

}


}

  评论这张
 
阅读(500)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017