简体   繁体   English

如何在 Java 中自动从 Flickr 下载图像?

[英]How to download images from Flickr automatically in Java?

I have a number of Flickr URL (not the URL of the pic itself).我有许多 Flickr URL(不是图片本身的 URL)。 I would like to download the Flickr image from each URL and save on my local computer.我想从每个 URL 下载 Flickr 图像并保存在我的本地计算机上。 I have the following code.我有以下代码。 imageStr is the Flickr URL. imageStr 是 Fl​​ickr URL。 When I copy and paste it on the browser I can see the image and when I download the image I can see there is some more extension after the URL name followed by a .jpg.当我将它复制并粘贴到浏览器上时,我可以看到图像,当我下载图像时,我可以看到 URL 名称后面还有一些扩展名,后跟一个 .jpg。 Is there any way in JAVA I can get the full image name (full image URL) and get it downloaded automatically?在 JAVA 中有什么方法可以获得完整的图像名称(完整的图像 URL)并自动下载吗?

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class ImageDownloaderDriverBasic {
    public static void main(String[] args) {
        String imageStr="https://flickr.com/photos/32508222@N03/25449794758";
        String trgt="C:\\Backup\\image\\";

        BufferedImage image = null;

        try {
            URL url = new URL(imageStr);
            image = ImageIO.read(url);
            ImageIO.write(image, "jpg",new File(trgt+"out.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Done");
    }
}

I used this to search for images where searchTerms is a String.我用它来搜索 searchTerms 是字符串的图像。 It adds the images to some list v.它将图像添加到某个列表 v。

    String total="";
    try {
        URLConnection uc = new URL("http://api.flickr.com/services/rest/?"+
                                      "method=flickr.photos.search&api_key="+apiKey+
                                      "&per_page="+k+
                                      "&page="+(int)(1+Math.random()*0)+
                                      "&text="+searchTerms).openConnection();
        DataInputStream dis = new DataInputStream(uc.getInputStream());
        String nextline;
        String[] servers = new String[10];
        String[] ids = new String[10];
        String[] secrets = new String[10];
        while ((nextline = dis.readLine()) != null) {
          total+=nextline+"\n";
        }
        dis.close();
    }
    catch(Exception ex0) { ex0.printStackTrace(); }
        int ind=0, i=0;
        while((ind=total.indexOf("<photo", ind+1))>-1) {
          int idi=total.indexOf("id=", ind);
          String id=total.substring(idi+4, total.indexOf('"', idi+4));
          int sci=total.indexOf("secret=", ind);
          String sc=total.substring(sci+8, total.indexOf('"', sci+8));
          int svi=total.indexOf("server=", ind);
          String sv=total.substring(svi+8, total.indexOf('"', svi+8));
          String url="http://static.flickr.com/"+sv+"/"+id+"_"+sc+"_z.jpg";
          System.out.println("trying to read "+url);
          try {
            BufferedImage bi=ImageIO.read(new URL(url));
            v.add(bi);
          }
          catch(Exception ex) { System.out.println("error retrieving"); continue; }
          i++;
          if(i>k) break;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM