Why vaadin upload component cause corrupttion in PDF files ?!

I have tried every other possible way to upload pdf files into my server. OK, that sounds easy but the problem is that all of the uploaded files are corrupted.
The size of the target and source pdf files are exactly the same, however when you open the uploaded pdf files with an Adobe reader there is a message stating missing font etc. Can anyone help me? Do you know what is the source of problem?

Hello,

we also use the upload component,
but don’t have any such problems.

Are you sure it’s corrupted during upload, and not when downloading it ?

It would also help when you show the relevant code you use to persist the uploaded file to disk on the server.

André

Thanks for reply. here is the first reciever code that I’v used for uploading:


        private String fileName;
        private String mtype;
        private String uniqueFileName;

        public OutputStream receiveUpload(String filename, String mimetype) {
            mtype = mimetype;
            uniqueFileName = UID.getUID();
//            upload.setUniqueFileName(uniqueFileName);
            FileOutputStream fos = null; // Output stream to write to
            File dir = new File(
                    Eeyore.getInstance().getPss().getPDFRepository());
            if (!dir.exists()) {
                dir.mkdirs();
            }
            file = new File(
                    Application.getInstance().getPss().getPDFRepository()
                    + File.separator +
                     uniqueFileName);
            try {
                // Open the file for writing.

                try {
                    fos.flush();
                } catch (IOException ex) {
                    Logger.getLogger(UploadPDF.class.getName()).log(Level.SEVERE, null, ex);
                }
                                fos = new FileOutputStream(file);

            } catch (final java.io.FileNotFoundException e) {
                // Error while opening the file. Not reported here.
                e.printStackTrace();
                return null;
            }
            return fos; // Return the output stream to write to
        }

I have also tried this one:


public class UploadTest extends VerticalLayout implements Receiver, Upload.SucceededListener {

    private Upload uploader = new Upload("Upload document", (Receiver) this);
    BufferedOutputStream tmpFile = null;

    public UploadTest() {
        uploader.setImmediate(true);
        uploader.setButtonCaption("Upload File");
        addComponent(uploader);


        uploader.addListener((Upload.SucceededListener) this);
    }

    public OutputStream receiveUpload(String filename, String MIMEType) {
        try {
            tmpFile = new BufferedOutputStream(new
                    FileOutputStream("c:\\testFinalfinal.pdf"));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(UploadTest.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tmpFile;
    }

    public void uploadSucceeded(SucceededEvent event) {
        getWindow().showNotification("Successful upload");
        if (tmpFile != null) {
            try {
                tmpFile.flush();
                tmpFile.close();
            } catch (IOException ex) {
                Logger.getLogger(UploadTest.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
        }
    }

I’m following this thread only because I have some similar, but with a GIF file. A GIF file that renders fine in my browser, when uploaded, can result in a GIF format exception for Java’s Image libs when I create a thumbnail. But I should be storing the uploaded original “as is”, yet when I try to serve that file, I get an error in the browser also saying the image is corrupted (in FF, there’s an error message, while others just show the red X placeholder).

But like you, I noted the length was the same, and I checked the first and last 10 bytes, and they seemed to match, but maybe I’ll need to look deeper for some sort of small bug in the uploader or in my processing of it.

And it’s not on every image, as I can upload many without problem, but there’s a couple I’ve found that have an issue. Very odd…

Yep, seems to be a bug in 6.5.0.


http://dev.vaadin.com/ticket/6361

Also in earlier version 6.2
Is this bug gonna be solved in tonight build?!

Hurray…yes, the bug report looks good. I just wrote the original contents from the upload to a file to compare the uploaded GIF with what I had before, and the file lengths are identical, but as you say in the bug report, when I ran a diff on the two, it was when the original file had 0d0d, which in my case the upload converted to 770d. My check the first 10 bytes and the last 10 bytes wasn’t good enough

Odd error, as if its doing some sort of character processing instead of working on bytes.

Just tried the 6.5.1 version and the bug is removed :smiley: