1

我有一部分程序检查文件夹中是否缺少任何图片,如果缺少则下载它们。它工作得很好,除非每当我尝试查看这些图像时,我的图片查看器(InfranView 和 Windows 照片查看器)都会给我以下错误:

http://imgur.com/sqzSoI3&V0TSV1m

我查看了文件的托管位置(C:\xampp\htdocs 文件夹)并查看了那里的图像,但它们没有损坏。

http://imgur.com/sqzSoI3&V0TSV1m#1

如果您能帮助我找到解决方案,我将不胜感激。

编辑:在教程之后找到解决方案。在下面发布了planetjone代码的摘录。有帮助的帮助!

解决方案:

public void downloadMissingFiles(String urls, String destination)
        throws IOException {
    URL url = new URL(urls);
    InputStream in = new BufferedInputStream(url.openStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int n = 0;
    while (-1!=(n=in.read(buf)))
    {
       out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();
    FileOutputStream fos = new FileOutputStream(destination);
    fos.write(response);
    fos.close();
}
4

1 回答 1

0

解决方案:

public void downloadMissingFiles(String urls, String destination)
        throws IOException {
    URL url = new URL(urls);
    InputStream in = new BufferedInputStream(url.openStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int n = 0;
    while (-1!=(n=in.read(buf)))
    {
       out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();
    FileOutputStream fos = new FileOutputStream(destination);
    fos.write(response);
    fos.close();
}
于 2014-11-20T00:12:38.943 回答