Handling outstream from receiveupdate

Hi,
I want to capture the outputstream and and after completion of upload wanted to pass that stream to some method.

How can I do that?

in your method you want to add something to the uploaded file?
I think output stream is closed automatically by vaadin upload component when uploading is finished, but can’t imagine a use case for your question - why you need exactly output stream in your other method after upload ?

may be you meant input stream, to access just uploaded data ?

Yes, I want to pass the input stream to some other class method, after the upload is finished to save the file permanently.
Can you pls help me?

You need to provide a stream that Vaadin will write to. The stream must point to where you want the file written.
Once Vaadin is done uploading, the file will be there. You have nothing to do – all you need is take the getStream() methods to get to the result, see the very last lines of the example.

Here is example code

    // Callback method to begin receiving the upload.
    @Override
    public OutputStream receiveUpload(String filename, String MIMEType) {
        FileOutputStream fos = null; // Output stream to write to
        try {
            ServletContext sCtx = app.getServletContext();
            String dirName = sCtx.getRealPath("registration"); //$NON-NLS-1$
            File dir = new File(dirName);
            logger.debug(dir.getAbsolutePath());

            if (!dir.exists()) dir.mkdirs();
            File longPath = new File(filename);
            filename = longPath.getName();
            file = new File(dir, filename);
            logger.debug("writing to {}", file.getAbsolutePath()); //$NON-NLS-1$

            // Open the file for writing.
            fos = new FileOutputStream(file);
        } catch (final java.io.FileNotFoundException e) {
            // Error while opening the file. Not reported here.
            e.printStackTrace();
            this.setComponentError(new SystemError(e));
            throw new SystemError(e);
        }
        return fos; // Return the output stream to write to
    }

    // This is called if the upload is finished.
   @Override
    public void uploadSucceeded(Upload.SucceededEvent event) {
        // Log the upload on screen.
        final String messageFormat = Messages.getString("SpreadsheetUploader.Status", locale); //$NON-NLS-1$
        final String mimeType = event.getMIMEType();
        status.setValue(MessageFormat.format(messageFormat, event.getFilename(), mimeType));
        processUploadedFile();
    }

    /**
     * @throws SystemError
     */
    private void processUploadedFile() throws SystemError {
        // process the file
        logger.debug("reading from: {}", file); //$NON-NLS-1$
        final FileResource fileResource = new FileResource(file, getApplication());
        DownloadStream ds = fileResource.getStream();
        //*** ds.getStream() is the file content; if you read ds.getStream() you are reading the uploaded file.
    }

Hi,

Use the UploadField from
EasyUploads
. It pretty much hides the code that J-F just coded for you.

If you can’t use the 6.5 nightly build, download the 0.1 version.

cheers,
matti

Hi Matti,
Where can I get the documentation r example to use this addon?

Actually, I dont want it to be permanently saved in file system in receiveupload.
I want to get in in an input stream and then pass it on to some other class method from uploadSucceeded event.

Several options.
First one is to create the uploaded file as a temporary file (same code as I provided)
And on successful upload, you open the file (FileInputStream) and pass it to your method, clean-up when done.

Second option is to use pipes.

PipedInputStream in = new PipedInputStream();
final PipedOutputStream out = new PipedOutputStream(in);

You can pass “out” to the uploader. Normally you would not wait for “uploadSucceeded”, you would slurp the bytes as the are written (you would read from “in” as you would on a normal stream). Everything written on “out” by Vaadin you can read on “in”. The typical pattern for this is to create a thread that just reads – it will wait for bytes to become available. And then, uploadSucceeded or cancelled would clean-up or cancel, respectively.