2014-07-19 164 views
0

我想上傳文件到git而不保存在本地磁盤上。我在我的webapp中使用vaadin + java,並從vaadin上傳組件。上傳文件的改進,vaadin

public OutputStream receiveUpload(String filename, String MIMEType) 
{ 
    this.filename = filename; 

    FileOutputStream fos = null; 
    try { 
     // exist any possibility to no saving file in filepath (only push 
     // to git) 
     fos = new FileOutputStream(new File(
       filepath + File.separator + filename)); 
    } catch (Exception e) { 
     // How to omit it, I don't want to save file in filepath... 
     return null; 
    } 

    return fos; 
} 

public void uploadSucceeded(Upload.SucceededEvent event) 
{    
    try { 
     // this method read file from filepath. Exist any possibilty to 
     // transfer file from upload panel to here without saving this 
     // file in filepath ? 
     commitToGit(filepath + File.separator + filename); 
    } catch(Exception e) { 
     e.printStackTrace(); 
    } finally { 
     // removing file from filepath, it is no comfortable for me 
     File file = new File(filepath + File.separator + filename); 
     if (file != null) { 
      file.delete(); 
     }  
    } 
} 
+0

您的commitToGit方法必須只提供一個InputStream,您可以將上傳組件的OutputStream管道傳遞給git上傳器的InputStream。 –

+0

您能否提供代碼示例...?我不知道它是如何到達的...... – user3328186

回答

0

在這裏看到的receiveUpload方法,你設置的上傳文件和你的git的連接器之間的管道管功能

Best way to Pipe InputStream to OutputStream

。 然後,uploadSucceeded方法不再需要或可用於清理資源。

+0

嗯......可能我需要轉換客棧的另一面。我有(FOS)FileOutpuTStream,方法commitToGit()的第一個方法看起來:私人字節[] readStreamToByteArray(InputStream流) - 所以我需要inputStrea,而不是OutputStream ... – user3328186

+0

http://ostermiller.org/convert_java_outputstream_inputstream.html –