Upload image to ftp ?

Hello

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
//image picture
Image image = new Image();
image.setWidth("128px");
image.setHeight("128px");        
mainLayout.addComponent(image);
        
//upload image
ImageUpload imageUp = new ImageUpload(image);
Upload upload = new Upload();
upload.setCaption("Get your image picture");
upload.setButtonCaption("Send");        
upload.addSucceededListener(imageUp);
mainLayout.addComponent(upload);



// my class upload
/** upload image picture */
public class ImageUpload implements Receiver, SucceededListener{
    private FTPClient ftp;
    private File file;
    private final 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(filename);            
            fos = new FileOutputStream(file);    
            sendToFtp(fos);
        }catch(FileNotFoundException ex){
            new Notification("File not found \n", 
                     ex.getLocalizedMessage(), 
                     Notification.Type.ERROR_MESSAGE)
                     .show(Page.getCurrent());
        }
        return fos;
    }
    
   /** FTPClient apache commons net 3.3 */
    private void sendToFtp(FileOutputStream fos){
        ftp = new FTPClient();
        try{
            ftp.connect("ftp.xxxx.com.br");
            if(FTPReply.isPositiveCompletion(ftp.getReplyCode())) {  
                ftp.login("user", "password");                
            } else {  
                //erro ao se conectar  
                ftp.disconnect();  
                new Notification("Connection error \n", 
                        "Conexão recusada", 
                        Notification.Type.ERROR_MESSAGE)
                        .show(Page.getCurrent());
                System.exit(1);  
            }
        }catch(Exception ex){
            new Notification("File not found \n", 
                     ex.getLocalizedMessage(), 
                     Notification.Type.ERROR_MESSAGE)
                     .show(Page.getCurrent());
        }
    }

}

The problem is below


com.vaadin.server.UploadException: Upload failed
    at com.vaadin.server.communication.FileUploadHandler.streamToReceiver(FileUploadHandler.java:594)
    at com.vaadin.server.communication.FileUploadHandler.handleFileUploadValidationAndData(FileUploadHandler.java:432)
    at com.vaadin.server.communication.FileUploadHandler.doHandleSimpleMultipartFileUpload(FileUploadHandler.java:395)
    at com.vaadin.server.communication.FileUploadHandler.handleRequest(FileUploadHandler.java:280)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1371)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.NullPointerException
    at com.vaadin.ui.Upload$1.getOutputStream(Upload.java:1053)
    at com.vaadin.server.communication.FileUploadHandler.streamToReceiver(FileUploadHandler.java:523)
    ... 18 more

Any idea ?

thanks

You have forgotten to call upload.setReceiver(): your upload component doesn’t know where to send the file.

upload.setReceiver(): did work well!