作业下载图片

下载图片要求:
    分别用HttpURLConnection 和 HttpClient对两个图片进行下载,利用多线程,接口回调的方式。

线程1: 利用HttpURLConnection线程进行下载
  
  
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
/**
* 这个线程利用HttpURLConnection负责下载
* @author Mixm
* @date 2015年10月8日 下午11:38:44
*/
public class HUCDownloadThread extends Thread {
 
private String path = null;
CallBack01 cb = null;
 
public HUCDownloadThread(String path, CallBack01 cb) {
this.path = path;
this.cb = cb;
}
 
@Override
public void run() {
URL url = null;
HttpURLConnection conn = null;
 
BufferedInputStream bis = null;
ByteArrayOutputStream baos = null;
 
try {
url = new URL(path);
conn = (HttpURLConnection) url.openConnection();
 
if (conn.getResponseCode() == 200) {
bis = new BufferedInputStream(conn.getInputStream());
baos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len = 0;
while((len = bis.read(b)) != -1){
baos.write(b, 0, len);
System.out.println(getName() + "haha 在下载");
}
baos.flush();
cb.success(baos.toByteArray());
 
} else {
System.out.println("臣妾做不到啊");
}
 
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bis.close();
baos.close();
conn.disconnect();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
 
interface CallBack01 {
void success(byte[] b);
}
线程2: 利用HttpClient进行下载:
  
  
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
 
/**
* 这个线程利用HttpClient进行下载
* @author Mixm
* @date 2015年10月8日 下午11:39:52
*/
public class HCDownloadThread extends Thread{
private String uriPath = null;
private String savePath = null;
private CallBack2 cb = null;
public HCDownloadThread(String uriPath, String savePath, CallBack2 cb) {
super();
this.uriPath = uriPath;
this.savePath = savePath;
this.cb = cb;
}
@Override
public void run() {
try {
//创建httpclient链接对象
HttpClient client = new DefaultHttpClient();
//创建请求方式
HttpGet get = new HttpGet(uriPath);
//得到返回
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() != -1) {
HttpEntity hEntity = response.getEntity();
BufferedInputStream bis = new BufferedInputStream(hEntity.getContent());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(savePath));
byte[] b = new byte[1024];
int len = 0;
while ((len = bis.read(b)) != -1) {
System.out.println(getName() + "我在下载");
bos.write(b, 0, len);
}
bos.flush();
cb.success();
bos.close();
bis.close();
} else {
 
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
 
interface CallBack2{
//这里就不传递数组了
void success();
}
测试代码:
  
  
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class TestHttpClientDemo01 {
 
public static void main(String[] args) {
String path = "http://img0.bdstatic.com/img/image/6446027056db8afa73b23eaf953dadde1410240902.jpg";
new HUCDownloadThread(path, new CallBack01() {
 
@Override
public void success(byte[] b) {
// 传过来的是数组 哥哥就写如文件中去
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(
"hk1.jpg"));
bos.write(b);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
 
}
}).start();
 
// 第二个线程下载
String uriPath = "http://img0.bdstatic.com/img/image/ca3accc6b9c7736fc3f90ca6c407df341409737570.jpg";
String savePath = "hk2.jpg";
new HCDownloadThread(uriPath, savePath, new CallBack2() {
 
@Override
public void success() {
System.out.println("我就打印一下我传完了 并没有什么卵用");
 
}
}).start();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值