我有许多Flickr网址(不是图片本身的网址)。我想从每个URL下载Flickr图像并保存在我的本地计算机上。我有以下代码。 imageStr是Flickr URL。当我将它复制并粘贴到浏览器上时,我可以看到图像,当我下载图像时,我可以看到在URL名称后跟一个.jpg后还有一些扩展名。在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");
}
}
答案 0 :(得分:0)
我用它来搜索searchTerms是String的图像。它将图像添加到某个列表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;
}