Search code examples
javamysqlimagexamppimagedownload

XAMPP/MySQL Downloading images error - Java


I have part of my program that checks if there are any pictures missing from a folder and downloads them if they are missing. It works great except whenever I try to view those images, my picture viewer(InfranView, and Windows Photo Viewer) gives me the following error:

http://imgur.com/sqzSoI3&V0TSV1m

I looked at where the files are hosted from (the C:\xampp\htdocs folder) and viewed the images there, but they were not damaged.

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

If you could help me find a solution I will be very thankful.

EDIT: Found solution following this tutorial. Posted an excerpt of planetjone's code below. Help that helps!

SOLUTION:

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

Solution

  • SOLUTION:

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