0

此代码下载 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();

    }

}
4

3 回答 3

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();
}
于 2013-01-21T14:19:30.043 回答
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();
}

之后,您可以进行各种改进:

  • 验证输入的名称,以查看它是否不为空(在这种情况下使用默认文件名)以及它是否是当前操作系统支持的有效文件名。
  • 只传递一个文件名模板作为参数(例如“image”)并使其在其末尾添加一个增量,以便每次创建一个新文件(例如:如果“image1.png”存在,则新文件名将是“image2.png”)。您需要首先列出当前目录中以“[模板]”开头的所有文件
于 2013-01-21T14:18:22.843 回答
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();

等等等等

当然,不要忘记关闭返回的流。

于 2013-01-21T14:18:46.347 回答