1

当我使用 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;
    }   

}

任何想法 ?

4

1 回答 1

0

使用upload.submitUpload();方法。

提示:正如您在 API 描述中看到的,您可以通过设置隐藏上传内部提交按钮upload.setButtonCaption(null);

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

于 2013-12-11T16:33:11.673 回答