此代码下载4张带照片的链接。 有没有办法用新名称创建新的.png文件? 例如:image1.png,image2.png,image3.png自动
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class AfbeeldingDown {
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg");
URL url1 = new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg");
URL url2 = new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg");
URL url3 = new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg");
InputStream in = url.openStream();
OutputStream out = new FileOutputStream("image.png");
int r;
while ((r = in.read())!= -1) {
out.write(r);
}
in.close();
out.close();
}
}
答案 0 :(得分:1)
你可以这样做
ArrayList<URL> urlList = new ArrayList();
urlList.add(new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg"));
urlList.add(new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg"));
urlList.add(new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg"));
urlList.add(new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/mazda-2013-tokyo-auto-salon.jpg"));
int i = 1;
for (URL url : urlList) {
InputStream in = url.openStream();
OutputStream out = new FileOutputStream("image" + i + ".png");
i++;
int r;
while ((r = in.read()) != -1) {
out.write(r);
}
in.close();
out.close();
}
更简单
ArrayList<String> imageNames = new ArrayList();
imageNames.add("mazda-2013-tokyo-auto-salon.jpg");
imageNames.add("mazda-2013-Volkswagen-CrossBlue-main.jpg");
for ( String imageName : imageNames) {
URL url = new URL("http://www.autoguide.com/auto-news/wp-content/uploads/2012/12/"+imageName);
InputStream in = url.openStream();
OutputStream out = new FileOutputStream(imageName);
int r;
while ((r = in.read()) != -1) {
out.write(r);
}
in.close();
out.close();
}
答案 1 :(得分:0)
您可以更新代码,以便将您希望它创建的文件名作为参数:
public static void main(String[] args) throws IOException {
//same code as before
OutputStream out = new FileOutputStream(arg[0]);
int r;
while ((r = in.read())!= -1) {
out.write(r);
}
in.close();
out.close();
}
之后,您可以进行各种改进:
答案 2 :(得分:0)
使用简单的类,这是可能的:
// Lacks checking, not thread safe etc -- left as an exercise
public final class CountingFileGenerator
{
private final String prefix, suffix;
private int count = 1;
public CountingFileGenerator(final String prefix, final String suffix)
{
this.prefix = prefix;
this.suffix = suffix;
}
public OutputStream newFile()
throws IOException
{
final String name = String.format("%s%d%s", prefix, count, suffix);
final OutputStream ret = new FileOutputStream(name);
count++;
return ret;
}
}
用法:
final CountingFileGenerator generator = new CountingFileGenerator("image", ".png");
final OutputStream o1 = generator.newFile();
final OutputStream o2 = generator.newFile();
等等。
当然,不要忘记关闭返回的流。