码迷,mamicode.com
首页 > Web开发 > 详细

通过Apache的httpClient的get方式连接服务器下载图片到本地

时间:2015-05-21 10:59:40      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:apache   httpclient   服务端   客户端   get   



客户端程序:

package lgx.java.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

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;

public class HttpClientGet {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 使用get方法连接服务器
		HttpGet httpGet = new HttpGet("http://192.168.1.48:8080/Test/test.jpg");
		HttpClient client = new DefaultHttpClient();
		FileOutputStream fos;
		try {
			// 客户端开始向指定的网址发送请求
			HttpResponse response = client.execute(httpGet);
			InputStream inputStream = response.getEntity().getContent();
			File file = new File("D:\\jj");
			if (!file.exists()) {
				file.mkdirs();
			}

			fos = new FileOutputStream("D:\\jj\\test.jpg");
			byte[] data = new byte[1024];
			int len = 0;
			while ((len = inputStream.read(data)) != -1) {
				fos.write(data, 0, len);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}

服务端可以参考这篇文章http://blog.csdn.net/harryweasley/article/details/45840523

两个文章大同小异,不过一个是用的java接口实现,一个是Apache的接口实现


通过Apache的httpClient的get方式连接服务器下载图片到本地

标签:apache   httpclient   服务端   客户端   get   

原文地址:http://blog.csdn.net/harryweasley/article/details/45887023

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!