java - XAMPP/MySQL 下载图像错误 - Java

标签 java mysql image xampp imagedownload

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

http://imgur.com/sqzSoI3&V0TSV1m

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

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

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

编辑:this 之后找到解决方案教程。在下面发布了 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();
}

最佳答案

解决方案:

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();
}

关于java - XAMPP/MySQL 下载图像错误 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27008838/

相关文章:

php - 为什么 MySql 将日期时间微秒格式化为 .000000?

SQL 查询从日志表计算访问持续时间

python - unoconv 将 .xlsx 文件导出为 png 等图像

java - java中如何识别选定的帧?

java - 加载共享库 : libX11. so.6 时出错:无法打开共享对象文件:linux docker 容器中没有这样的文件或目录

java - 在 Postgres 表中插入时间戳

java - 无法解决 YouTube 类/符号、YouTube 数据 API Java 示例问题

php - 连接mysql超时

html - 响应式图像和文本行

linux - 如何解压 .img 文件?