无法从某些网址获取图片内容

时间:2014-07-18 07:32:28

标签: java url bufferedreader bufferedimage javax.imageio

在开发插件以复制各种网页中的图像的过程中,我试图从我的java代码中的URL中获取图像,如下所示:

URL urlString = new URL("http://www.hdwallsource.com/img/2014/7/harley-davidson-wallpaper-16885-17439-hd-wallpapers.jpg");
URLConnection conn = urlString.openConnection();
System.out.println("--------------------getContentType: "+conn.getContentType()+"....conn:"+conn);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println("--------------------"+in);
String inputLine;
while ((inputLine = in.readLine()) != null)
    System.out.println(inputLine);
     in.close();

对于此特定图片路径:“http://www.hdwallsource.com/img/2014/7/harley-davidson-wallpaper-16885-17439-hd-wallpapers.jpg”我正在获取getContentType:text / html;字符集= UTF-8。我的sysout给了我这个图像可用的页面的所有HTML代码。对于所有其他图像路径,如:http://img6a.flixcart.com/image/mobile/n/q/a/motorola-moto-g-400x400-imadsmbwhznhucjj.jpeg我正在获得正确的图像/ jpeg类型和图像内容。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

如果您在浏览器的新隐身窗口中打开第一个链接,您会发现您也获得了html页面。该网站可能有一些不向抓取工具显示图像的政策。看来,如果您没有网站的Cookie,他们会向您显示HTML页面。