怎么使用httpclient无法读取图片,请高手帮忙解决,谢谢!

shibai 2007-07-14 01:50:57
比如:
URL: http://itv.hexun.com/lbi-html/ly/adserver/shichang/2007/0713/ir_174_60.gif

这个url在浏览器能显示出来图片,为什么用httpclient无法读取保存到本地呢?
请高手帮忙解决,谢谢!
...全文
469 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
empireconquer 2007-07-24
  • 打赏
  • 举报
回复

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

public class TestURLConnet {

/**
* @param args
*/
public static void main(String[] args) {

// TODO Auto-generated method stub
String urlstr = "http://zi.csdn.net/2007.06/now120x60.gif";
try {
URL url = new URL(urlstr);
BufferedImage image= ImageIO.read(url);
boolean flag = ImageIO.write(image, "jpg", new File("now120x60.gif"));
System.out.println(flag);

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

}
empireconquer 2007-07-24
  • 打赏
  • 举报
回复
package at0707;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class TestURLConnet {

/**
* @param args
*/
public static void main(String[] args) {

// TODO Auto-generated method stub
String urlstr = "http://zi.csdn.net/2007.06/now120x60.gif";
BufferedReader br= null;
try {
URL url = new URL(urlstr);

FileOutputStream out = new FileOutputStream("C:/now120x60.gif");
int c =0;
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK)
{
br = new BufferedReader(new InputStreamReader(url.openStream()));
while ((c = br.read()) != -1)
{
out.write(c);
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
shibai 2007-07-14
  • 打赏
  • 举报
回复
我想用程序把它保存到本地阿
windvscloud 2007-07-14
  • 打赏
  • 举报
回复
用url不就可以了...

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧