2014-01-16 59 views
0

我創建使用submitUpload()當我在我的按鈕點擊不工作的圖片上傳,但是當我在一個方法中添加submitUpload()沒有。上傳圖片不可用?

這是我使用的類:

// save image 
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; 
} 
} 


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();  
    } 
} 

如果我調用該方法保存submitUpload()不工作,但是當我直接在按鈕收聽測試submitUpload()確實工作。

有什麼想法?

+0

我認爲,用戶必須選擇在文件網頁瀏覽器,我不認爲你可以使用JavaScript從本地文件系統中選擇一個文件出於安全原因。 –

+0

@AndréSchild上傳是Vaadin – FernandoPaiva

+0

是, 的組件和組件vaadin在客戶端使用JavaScript(在web瀏覽器) 因此同樣限制適用 –

回答

1

嘗試這一個,我們正在使用它:

public class Demographic extends CustomComponent implements Upload.SucceededListener,Upload.FailedListener, Upload.Receiver,Upload.ProgressListener 
{ 
    private Upload uploadPic; 
    public Demographic() 
    { 
     mainLayout = new AbsoluteLayout(); 
     mainLayout.setImmediate(true); 
     mainLayout.setWidth("100%"); 
     mainLayout.setHeight("100%"); 
     mainLayout.setMargin(false); 

     uploadPic = new Upload("Upload image", this); 
     uploadPic.setImmediate(true); 
     uploadPic.setWidth("-1px"); 
     uploadPic.setHeight("-1px");     
     mainLayout.addComponent(uploadPic, "top:135.0px;left:32.0px;"); 


     uploadPic.addListener((Upload.SucceededListener) this); 
     uploadPic.addListener((Upload.FailedListener) this);   
     uploadPic.addListener((Upload.ProgressListener)this); 
     } 
     @Override 
     public void uploadFailed(FailedEvent event) { 
    // TODO Auto-generated method stub 

     app.getMainWindow().showNotification("Error! <br />", "Upload Failed due   to: " + event.getReason().getMessage() ,    Window.Notification.TYPE_WARNING_MESSAGE); 

     } 
    @Override 
    public void uploadSucceeded(SucceededEvent event) { 
      // all success logic 
      if(event.getMIMEType().contains("image")){//mimeType can be made a global variable and can set in Receive upload 

     // System.out.println(event.getFilename()); 

      savePicture(event.getFilename());// save pic to db from the path provided in receive upload 


       app.getMainWindow().showNotification("Success! <br />", "Image upload successful!", Window.Notification.TYPE_TRAY_NOTIFICATION); 
      } 
     } 
    } 
    @Override 
    public OutputStream receiveUpload(String filename, String mimeType) { 
     FileOutputStream fos; 

     if(mimeType.contains("image")){ 
      String basePath = getApplication().getContext().getBaseDirectory().getAbsolutePath() + "\\Documents\\"+filename; 

      File file= new File(basePath);  
      boolean checkForDir = file.exists(); 

     if(!checkForDir){ 
     checkDir.mkdir(); 
      } 
     try { 
     // Open the file for writing. 
      fos = new FileOutputStream(file);  
      } catch (final java.io.FileNotFoundException e) { 

      // Error while opening the file. Not reported here. 
        //e.printStackTrace(); 

      return null; 
     } 

     } 
     return fos; 
    } 
} 

可能這裏有語法錯誤,但我的觀點是解釋這裏的主要邏輯

+0

有一些方法可以測試用戶是否在上傳前選擇了一個文件?例如,如果upload.getValue()返回一個文件或不是空的? – FernandoPaiva

+0

抱歉遲到replyOn uploadPic按文件選擇器將打開,在此對話框中您將瀏覽你的形象,讓說,如果你不接任何文件,然後打開不會上傳任何文件,如果你給錯了名字,那麼文件選擇對話會自動提示你,取消時按下文件選擇也不會上傳, – Mubasher

+0

如果解決了你的問題,請確認它是否爲ANS。謝謝 – Mubasher