比如:
URL: http://itv.hexun.com/lbi-html/ly/adserver/shichang/2007/0713/ir_174_60.gif这个url在浏览器能显示出来图片,为什么用httpclient无法读取保存到本地呢?
请高手帮忙解决,谢谢!
URL: http://itv.hexun.com/lbi-html/ly/adserver/shichang/2007/0713/ir_174_60.gif这个url在浏览器能显示出来图片,为什么用httpclient无法读取保存到本地呢?
请高手帮忙解决,谢谢!
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();
}
}}
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();
}
}}