I created a image upload using submitUpload() does work when I click in my Button but when I add the submitUpload() in a method doesn’t.
This is the class I am using:
// save image Receiver
public class ImageUpload implements Receiver{
private File file;
private String foto;
private final String path = "/home/fernando/curriculum/";
private String cpf;
/** add cpf document */
public void setCpf(String cpf){
this.cpf = cpf;
}
/** save image */
@Override
public OutputStream receiveUpload(String filename, String mimeType) {
FileOutputStream fos = null;
try{
file = new File(filename);
if(file.getName().endsWith("jpg")){
String cpfNumeros = this.cpf.replaceAll("\\.", "").replace("-", ""); //remove mask cpf
String[] imagem = filename.split("\\."); //get jpg
String novaImagem = cpfNumeros + "." + imagem[1]
; // define name new image
// new image
File newFile = new File(path + novaImagem);
if(newFile.exists()){
newFile.delete();
}
fos = new FileOutputStream(newFile); //salva imagem
}else{
new Notification("Erro de arquivo<br/>",
"Somente arquivos jpg são permitidos",
Notification.Type.ERROR_MESSAGE)
.show(Page.getCurrent());
}
}catch(FileNotFoundException ex){
new Notification("File not found<br/>",
ex.getLocalizedMessage(),
Notification.Type.ERROR_MESSAGE)
.show(Page.getCurrent());
return null;
}
return fos;
}
}
//view
public class ImageUploadView extends CustomComponents {
//upload image
ImageUpload imageUpload = new ImageUpload();
final Upload upload = new Upload("", imageUpload);
upload.setCaption("Image");
upload.setButtonCaption(null);
mainLayout.addComponent(upload);
Button btnSave = new Button("Save");
btnSave.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
save(); //call save method
}
});
}
/** save informations on db and save image of user */
private void save(){
if(!cpf.getValue().trim().isEmpty()){
imageUpload.setCpf(cpf.getValue());
upload.submitUpload();
}
}
If I call the method save the submitUpload() doesn’t work, but when I test submitUpload() directly on Button listener does work.
Any idea ?