2017-09-14 1 views
-1

Vaadin 업로드 구성 요소에서 파일을 가져 오는 방법을 알고 싶습니다. 다음은 Vaadin Website 의 예입니다. 그러나 OutputStreams에 대한 내용 이외의 저장 방법은 포함되어 있지 않습니다. Help!업로드 된 파일을 저장하는 방법

+1

으로이 문서에서, 당신은'receiveUpload'라는 방법이있다. 이 방법에서는 파일 이름과 MIME 유형을 사용합니다. 그런 다음 해당 파일 이름의'File'을 사용하여'OutputStream' [(java 7 doc)] (https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html)을 만들어야합니다 그것을 반환 ... 물론, '파일'은 업로드를 저장하려는 위치에 있어야합니다. – Shirkam

+0

@Shirkam 코드가 있습니까? 위치는 C : \ –

+1

입니다. 그렇다면 왜 stackoverflow는 무료 프로그래머 커뮤니티입니까? –

답변

2

바이든에서 파일 업로드를 받으려면 방법을 제공하는 Receiver 인터페이스를 구현해야 정보를 수신 할 수 있습니다. 이 작업을 수행하는 가장 간단한 코드 (Vaadin 7 docs에서 예로서 촬영) 될 것이다 : 그와

class FileUploader implements Receiver { 
    private File file; 
    private String BASE_PATH="C:\"; 

    public OutputStream receiveUpload(String filename, 
            String mimeType) { 
     // Create upload stream 
     FileOutputStream fos = null; // Stream to write to 
     try { 
      // Open the file for writing. 
      file = new File(BASE_PATH + filename); 
      fos = new FileOutputStream(file); 
     } catch (final java.io.FileNotFoundException e) { 
      new Notification("Could not open file<br/>", 
         e.getMessage(), 
         Notification.Type.ERROR_MESSAGE) 
      .show(Page.getCurrent()); 
      return null; 
     } 
     return fos; // Return the output stream to write to 
    } 
}; 

에서, UploaderC:\ 당신에게 파일을 작성합니다. 업로드가 성공적으로 완료되거나 완료된 후에 무언가를 원할 경우 SucceeddedListener 또는 FailedListener을 구현할 수 있습니다. 위의 예를 복용 (A SucceededListener 포함) 결과는 같을 수

class FileUploader implements Receiver { 
    //receiveUpload implementation 

    public void uploadSucceeded(SucceededEvent event) { 
     //Do some cool stuff here with the file 
    } 
} 
관련 문제