keram
keram

Reputation: 2421

Why pdf files downloads as corrupted?

I wrote small program for downloading files via url. Every other files format I can open properly, but for downloaded pdf it's impossible.

public static void saveFile(String fileUrl, String destinationFile) throws IOException {
    URL url = new URL(fileUrl);
    InputStream is = url.openStream();
    OutputStream os = new FileOutputStream(destinationFile);

    byte[] b = new byte[2048];
    int length;

    while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
    }
    os.flush();
    is.close();
    os.close();
}

Do I need special way for handling pdf downloads?

When I try to get selected pdf via URL in browser it's displays properly

EDIT

Added flush() to code, still no success

Trying to open damaged pdf in browser (FF) returns error:

File does not begin with '%PDF-'

Adobe Reader returns:

File could not be open because it is either not a supported file type or because file has been damaged.

Damages pdf has smaller size (about 80%) than original

Upvotes: 0

Views: 3580

Answers (1)

keram
keram

Reputation: 2421

The damaged pdf files has website html code inside.

Upvotes: 1

Related Questions