当我使用 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;
}
}
有什么想法吗?
最佳答案
使用upload.submitUpload();
方法。
提示:正如您在 API 说明中看到的,您可以通过设置 upload.setButtonCaption(null);
来隐藏上传内部提交按钮
API 链接:https://vaadin.com/api/com/vaadin/ui/Upload.html#submitUpload()
关于java - 保存时上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20524374/