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;
}
}
任何想法?