保存时上传?

时间:2013-12-11 16:18:27

标签: java frameworks vaadin vaadin7

当我使用vaadin7的上传组件点击按钮保存时,我正在尝试一种保存图片的方法。 上传组件有一个按钮来发送图像,但是当我单击按钮保存我的窗口时,我希望保存该文件。

我正在尝试这个。

//upload image   
Upload upload = new Upload("Choose your picture");
upload.setButtonCaption(null);
mainLayout.addComponent(upload);

Button btnSave = new Button("Save");
btnSave.addClickListener(new Button.ClickListener() {
    @Override
    public void buttonClick(ClickEvent event) {
        //click to save all fields and picture choosed in upload

    }
});


/** upload image picture */
public class ImageUpload implements Receiver{
    private File file;
    private String cpf; // image's name example 222.333.444-55

    /** save image picture */
    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {       
        FileOutputStream fos = null;        
        try{
            if(new File(filename).getName().endsWith("jpg")){
                String cpfFormato = this.cpf.replaceAll("\\.", "").replace("-", "");
                String[] imagem = filename.split("\\.");
                String novaImagem = cpfFormato + ".jpg"; //22233344455.jpg
                file = new File(novaImagem);
                fos = new FileOutputStream("/tmp/" + file);             
            }else{
                new Notification("Erro de arquivo \n",
                             "Only jpg", 
                              Notification.Type.ERROR_MESSAGE)
                             .show(Page.getCurrent());
            }           
        }catch(FileNotFoundException ex){
            new Notification("File not found \n", 
                              ex.getLocalizedMessage(), 
                              Notification.Type.ERROR_MESSAGE)
                              .show(Page.getCurrent());
            return null;
        }
        return fos;
    }   

}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

使用upload.submitUpload();方法。

提示:正如您在API说明中所看到的,您可以通过设置upload.setButtonCaption(null);

来隐藏上传内部提交按钮

API的链接:https://vaadin.com/api/com/vaadin/ui/Upload.html#submitUpload()