以下是简单的DEMO实例代码:
String url = "http://odum9helk.qnssl.com/resource/gogopher.jpg?imageView2/2/h/200";
HttpClientResult result = HttpClientUtil.httpGet(url, null);
String response = result.getResponse();
// byte[] byte_res = response.getBytes("utf8");
// ByteArrayInputStream in = new ByteArrayInputStream(byte_res);
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("d:\\rabbit.jpg"), "utf-8");
out.write(response);
out.flush();
out.close();
接口 HttpClientUtil.httpGet部分代码:
CloseableHttpClient hc = getHttpClient();
ht = hc.execute(httpGet);
HttpEntity het = ht.getEntity();
InputStream is = het.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf8"));
String readLine;
StringBuffer sb = new StringBuffer();
while ((readLine = br.readLine()) != null) {
sb.append(readLine);
}
is.close();
br.close();
int status = ht.getStatusLine().getStatusCode();
return new HttpClientResult(url, status, sb.toString());
3 个解决方案
#1
图片是二进制文件,只能用字节流,不能用字符流
#2
这个我知道,但是改正字节也不行,图片还是打不开。
String url = "http://odum9helk.qnssl.com/resource/gogopher.jpg?imageView2/2/h/200";
HttpClientResult result = HttpClientUtil.httpGet(url, null);
String response = result.getResponse();
byte[] byte_res = response.getBytes("utf8");
ByteArrayInputStream in = new ByteArrayInputStream(byte_res);
FileOutputStream out = new FileOutputStream(new File("d:\\rabbit.jpg"));
byte[] buffer = new byte[1024];
int s;
while((s = in.read(buffer)) > 0)
{
out.write(buffer, 0, s);
}
out.flush();
out.close();
#3
package cn.bjsxt.oop.Url;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
/**
* function:使用字节流从网上下载图片到本地
*
* @author wordxiao
*
*/
public class DownLoad {
public static void main(String[] args) throws IOException {
URL url = new URL(
"http://odum9helk.qnssl.com/resource/gogopher.jpg?imageView2/2/h/200");
InputStream is = url.openStream();
OutputStream os = new FileOutputStream("f:/wordxiao.jpeg");
byte[] b = new byte[1024];
int temp = 0;
while ((temp = is.read(b)) != -1) {
os.write(b, 0, temp);
}
os.close();
is.close();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
/**
* function:使用字节流从网上下载图片到本地
*
* @author wordxiao
*
*/
public class DownLoad {
public static void main(String[] args) throws IOException {
URL url = new URL(
"http://odum9helk.qnssl.com/resource/gogopher.jpg?imageView2/2/h/200");
InputStream is = url.openStream();
OutputStream os = new FileOutputStream("f:/wordxiao.jpeg");
byte[] b = new byte[1024];
int temp = 0;
while ((temp = is.read(b)) != -1) {
os.write(b, 0, temp);
}
os.close();
is.close();
}
}
#1
图片是二进制文件,只能用字节流,不能用字符流
#2
这个我知道,但是改正字节也不行,图片还是打不开。
String url = "http://odum9helk.qnssl.com/resource/gogopher.jpg?imageView2/2/h/200";
HttpClientResult result = HttpClientUtil.httpGet(url, null);
String response = result.getResponse();
byte[] byte_res = response.getBytes("utf8");
ByteArrayInputStream in = new ByteArrayInputStream(byte_res);
FileOutputStream out = new FileOutputStream(new File("d:\\rabbit.jpg"));
byte[] buffer = new byte[1024];
int s;
while((s = in.read(buffer)) > 0)
{
out.write(buffer, 0, s);
}
out.flush();
out.close();
#3
package cn.bjsxt.oop.Url;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
/**
* function:使用字节流从网上下载图片到本地
*
* @author wordxiao
*
*/
public class DownLoad {
public static void main(String[] args) throws IOException {
URL url = new URL(
"http://odum9helk.qnssl.com/resource/gogopher.jpg?imageView2/2/h/200");
InputStream is = url.openStream();
OutputStream os = new FileOutputStream("f:/wordxiao.jpeg");
byte[] b = new byte[1024];
int temp = 0;
while ((temp = is.read(b)) != -1) {
os.write(b, 0, temp);
}
os.close();
is.close();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
/**
* function:使用字节流从网上下载图片到本地
*
* @author wordxiao
*
*/
public class DownLoad {
public static void main(String[] args) throws IOException {
URL url = new URL(
"http://odum9helk.qnssl.com/resource/gogopher.jpg?imageView2/2/h/200");
InputStream is = url.openStream();
OutputStream os = new FileOutputStream("f:/wordxiao.jpeg");
byte[] b = new byte[1024];
int temp = 0;
while ((temp = is.read(b)) != -1) {
os.write(b, 0, temp);
}
os.close();
is.close();
}
}