0

I'm creating an application and I have a image picture of my customers. How can I get this image and upload to ftp ?

Reading vaadin7 book in chapter Upload I did make the example but did not work, and I'm looking for how to send this image picture to ftp also.

I did try this.

  /** My UI */
    //foto 
    Image picture = new Image();
    picture.setWidth("128px");
    picture.setHeight("128px");     
    mainLayout.addComponent(foto);

    //upload image picture
    ImageUpload imageUp = new ImageUpload(picture);
    Upload upload = new Upload();
    upload.setCaption("Find your picture");
    upload.setButtonCaption("Send");        
    upload.addSucceededListener(imageUp);
    mainLayout.addComponent(upload);


    /** class upload image picture */
    public class ImageUpload implements Receiver, SucceededListener{
        private File file;
        private Image image;    

    public ImageUpload(Image image){
        this.image = image;
        this.image.setVisible(false);
    }

    @Override
    public void uploadSucceeded(SucceededEvent event) {
        this.image.setVisible(true);
        this.image.setSource(new FileResource(file));
    }

    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {
        FileOutputStream fos = null;
        try{
            file = new File("/tmp/" + filename);            
            fos = new FileOutputStream(file);           
        }catch(final java.io.FileNotFoundException ex){
            new Notification("File not found \n", 
                             ex.getLocalizedMessage(), 
                             Notification.Type.ERROR_MESSAGE)
                             .show(Page.getCurrent());
        }
        return fos;

    }
}

Any idea ?

thanks

4J41
  • 5,005
  • 1
  • 29
  • 41
FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118
  • Possibly this would help: http://stackoverflow.com/questions/11739153/file-upload-in-java-through-ftp – drew_w Dec 05 '13 at 16:05
  • 1
    share some more details what didn't work. For clarification: You want to upload the image to a ftp-server after the image has been uploaded to the "webapp server"? – nexus Dec 05 '13 at 20:18
  • @nexus, When does customer choose your picture instead to insert that on my data base I think best send his picture to ftp and in my data base insert only its name and refer your picture to your email. – FernandoPaiva Dec 06 '13 at 10:28
  • I'm just wondering: Do you really need the ftp server? Is your database on the same machine like your webserver is? If yes do you really still need the ftp-server? – nexus Dec 06 '13 at 10:58
  • @nexus I need that work, on ftp or other way. I don't wanna insert the customer image on my db. If I upload the image to my webserver on path /home/myapp/images what are you think ? – FernandoPaiva Dec 06 '13 at 11:32
  • I would upload it to a webapp directory where you can save all kind of files belonging to your app. – nexus Dec 06 '13 at 12:15
  • I'll do try this. Do you knows how to check the extension on upload, example only .jpg ? one more time, thanks ! – FernandoPaiva Dec 06 '13 at 12:39

1 Answers1

0

To provide an answer according to our conversation in the comments:

Upload the image to a folder on the server using the upload component from Vaadin as you did.

Do you knows how to check the extension on upload, example only .jpg ?

You could

  1. quick & dirty: just check if the filename ends with .jpg
  2. Determine the mime-type of the uploaded file
  3. do 1. & 2 (reject the upload if the file doesn't end with .jpg otherwise allow the upload and recheck the uploaded file with its mime-type)
Community
  • 1
  • 1
nexus
  • 2,937
  • 3
  • 17
  • 22