upload file to temp folder

guys, good day…

Im currently working on a file upload that will store the files to my temp_folder w/c is located in VAADIN/themes. I already run out of ideas how to set the path that my upload will be stored inside my temp_folder. here’s the latest code I created.


public OutputStream receiveUpload(String filename, String mimeType) {
            fileName = filename;
            mtype = mimeType;
            String path = getApplication().getContext().getBaseDirectory() + "/VAADIN/themes/temp_folder/" ;
            File file  = new File(path+filename);            
            FileOutputStream fos = null;
            try{
                fos = new FileOutputStream(file);
            }catch(Exception e){
                e.getMessage();
            }
            return fos;
        }

please do help me guys… Im running out of time. :frowning:


Java’s own temp file mechanism?
If it really is a temp folder and you only read the file again from the code, then why not use ?

I don’t know how good a thing it is to write stuff into your project structure. The java temp or some servlet container folder might be more appropriate.

actually, the main purpose of this is to transfer the file via FileInputStream to store it to S3 bucket of Amazon since the Upload class does not provide FileInputStream, Im not sure if this is the best solution but I just want to try if it works so I could report it to my boss. :frowning: then maybe I’ll just change the code later to a more suitable structure.

Yeah if that is the case then I definitely think that you should pass the Upload component’s stream into File.createTempFile and push that forward to S3. The other option is to take the outputstream out of Upload, convert it to a inputstream and pass it directly to S3. I’m not familiar with what the S3 API works with so can’t be more specific than that.

I could write the file in my local folder using outputstream, the moment I push it to S3 only the filename(0 kb) is being stored. Im using this
toolkit
to upload files to S3 and it requires inputstream to pass to the object. I have no idea how to convert the upload into an inputstream, i’ve tried everything I could find but still failed.

Save the upload to a file in a temp dir, then wrap a
file input stream
around it. Does that work for you?

Cheers,
Bobby

And notice that the upload is only starting when receiveUpload() is called, so don’t start pushing it forward from there - the file will be empty just as you described. Rather use uploadSucceeded() for that, which is called after the upload has actually happened.
Book of Vaadin: Upload
.

Thanks for the replies guys… I’ll try to work out your suggestions. Hopefully vaadin will get
nominated
. :smiley:

I dont need FileInputStream nor temp_dir, I followed what you suggested and it work like a charm… Thanks a lot man.

just want to share my code just incase others might be doing the same as well.

upload.addListener(new Upload.SucceededListener() {
            public void uploadSucceeded(SucceededEvent event) {
                // This method gets called when the upload finished successfully
                status.setValue("Uploading file \"" + event.getFilename() + "\" succeeded");
                try {
                    File file = new File(event.getFilename());
                    S3Object object = new S3Object(file.getAbsoluteFile());
                    s3Service.putObject(myBucket, object);
                } catch (Exception e) {
                    Logger.getLogger(BucketLists.class.getName()).log(Level.SEVERE, null, e);
                }                    
            }
        });

Good thing! Now nominate Vaadin to that nomination award. :wink:

I think it’s already been nominated. “They are still collating nominations…” from @joonaslehtinen :grin:

I try access the file when it upload however it fail in this line:

File file = new File(event.getFilename());

Because event.getFilename() is “informes del 1 de junio al 16 de julio.csv” and the real path of file is "“C:\Users\marlow25\AppData\Local\Temp\JUpload$tmp\informes_del_1_de_junio_al_16_de_julio.csv”

any idea???

@Override public void uploadSucceeded(SucceededEvent event) { // TODO Auto-generated method stub pi.setVisible(false); upload.setVisible(true); try { File fileJMiner = new File(event.getFilename()); br = new BufferedReader(new FileReader(fileJMiner)); thank you

Hi,
but i dont want to upload a file to temperary location and from there to s3. It is taking more time because it looks like uploading two time. First to server temp location and from there to s3.
According to my concern it is not correct and not good.
Any idea, to upload file from client system to s3 direclty.

Thanks.

Hi,

now i have Vaadin 7 Hence getApplication().getContext().getBaseDirectory() is not working,

Can u please tell how to set the path with new code.

vaadin 6: String path = getApplication().getContext().getBaseDirectory() + “/VAADIN/themes/temp_folder/”

vaadin 6: String path = ???

Thanks

You can use PipedInputStream. In my (following) example I upload a file from user computer to another server (webdav), but the idea with S3 should be the same; just use methods you need.

    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {
        try {
            String dirName = dirName();
            URL targetDir = getRoot().resolve(
                    new URI(null, dirName  + "/", null)).toURL();
            Sardine sardine = SardineFactory.begin();
            sardine.createDirectory(targetDir.toExternalForm());
            URL target = targetDir.toURI().resolve(new URI(null, filename, null)).toURL();
            PipedOutputStream pos = new PipedOutputStream();
            InputStream is = new PipedInputStream(pos);
            new Thread(){
                public void run() {
                    try {
                        sardine.put(target.toExternalForm(), is);
                    } catch (IOException e) {
                        log.error("Error uploading file " + filename, e);
                        Notification.show("Error uploading file " + filename, e.getLocalizedMessage(), Type.ERROR_MESSAGE);
                    }
                };
            }.start();
            return pos;
        } catch (Exception e) {
            log.error("Can't upload file " + filename, e);
            throw new RuntimeException(e);
        }
    }

where does it say where you are posting it

Agata Vackova:

Srikanth Shanigaram: Hi,
but i dont want to upload a file to temperary location and from there to s3. It is taking more time because it looks like uploading two time. First to server temp location and from there to s3.
According to my concern it is not correct and not good.
Any idea, to upload file from client system to s3 direclty.

Thanks.

You can use PipedInputStream. In my (following) example I upload a file from user computer to another server (webdav), but the idea with S3 should be the same; just use methods you need.

@Override
    public OutputStream receiveUpload(String filename, String mimeType) {
        try {
            String dirName = dirName();
            URL targetDir = getRoot().resolve(
                    new URI(null, dirName  + "/", null)).toURL();
            Sardine sardine = SardineFactory.begin();
            sardine.createDirectory(targetDir.toExternalForm());
            URL target = targetDir.toURI().resolve(new URI(null, filename, null)).toURL();
            PipedOutputStream pos = new PipedOutputStream();
            InputStream is = new PipedInputStream(pos);
            new Thread(){
                public void run() {
                    try {
                        sardine.put(target.toExternalForm(), is);
                    } catch (IOException e) {
                        log.error("Error uploading file " + filename, e);
                        Notification.show("Error uploading file " + filename, e.getLocalizedMessage(), Type.ERROR_MESSAGE);
                    }
                };
            }.start();
            return pos;
        } catch (Exception e) {
            log.error("Can't upload file " + filename, e);
            throw new RuntimeException(e);
        }
    }

I have days looking how to do this and I do not understand: s to upload it directly to amazon s3