2016-08-10 85 views
2

我有這個問題,我正在創建一個文件,但這是創建空文件。從OutputStream創建一個文件

我正在使用Dropbox的API,Dropbox的代碼運行良好,但我不知道我不好。我用我的應用程序使用了2º和3º的代碼,這個運行良好。

這可以按層次進行操作。我正在發送outputStream作爲功能。但這是空的。

我正在使用outputStream,因爲我需要這個操作outputstream。

1º代碼(類檢驗||電話):

File tempFile=new File("C:\\PRUEBAS_TFG\\cloud.png"); if (! tempFile.exists()) { tempFile.createNewFile(); } 
File file = new File("C:\\PRUEBAS_TFG\\cloud.png"); 
OutputStream outputStream = new FileOutputStream(file); 
catmanager.downloadFilesToItem(catmanager.getAllListCatalog().get(0), catmanager.getAllListCatalog().get(0).getItems().get(2), listFile, outputStream); 
outputStream.close(); 

2º代碼(類catmanager ||1ºbody):

public void downloadFilesToItem(Catalog catalog, Item item, List<String> files, OutputStream output){ 
    try{ 
     String fsPath; 
     fsPath = pathCatalog(catalog); 
     getFSManager().changeDirectoryConfigNPath(fsPath+"/"+item.getName()+"_item"); 
     for(int i = 0;i<files.size();i++){ 
      getFSManager().downloadFile(files.get(i), output); 
     } 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

3ºCode(類FSManager ||2ºbody)

public void downloadFile(String fileName, OutputStream output) throws IOException{//Aqui deveria Buscar el Fichero Funciona por que da la casualidad que esta el fichero en la primera nube 
    /** Cogemos la lista de FS del usuario */ 
    List<IFileSystem> aux = getFileSystemsUser(); 

    for(int i = 0; i < aux.size();i++){ 
     /** Se realiza la Funcionalidad del metodo*/ 
     System.out.println(aux.get(i).toString()); 
     try{ 
      aux.get(i).downloadFile(_listPath.get(i)+"/"+fileName, output); 
      i = aux.size()+1; 
     }catch(IOException e) { 

     } 
    } 
} 

4ºCode(Class aux Dropbox || API Dropbox):

public void downloadFile(String fileName, OutputStream aux) throws IOException{ 
    try{ 
     getDbxClient().getFile(fileName, null, aux); 
    }catch(IOException e){ 
     throw e; 
    }catch(DbxException u){ 
     throw new IOException(u.getCause()); 
    } 
} 

謝謝提前。

+0

請問您能提供一個最小(和更好格式)的例子嗎? :-)我們會提供幫助。 :-) –

+0

感謝您的建議,我修改了該職位 –

回答

0

我想你必須要flush(參見相應的方法)把文件輸出到你的文件(並關閉它)。 Dropbox API處理OutputStream,但肯定不關心你想寫一個文件。

你可以在example後看看你。

+0

我已經在所有關卡中使用過功能Flush,但File仍然是空的。 –

+0

您是否已經看過https://www.dropbox.com/developers-v1/core/start/java上的tutoriel? –

+0

是的,Dropbox的功能運行良好。我在我的應用中使用這個Dropbox功能來處理其他進程。感謝您的關注 –

1

您正在使用一個FileOutputStream下載文件列表。你期望合併文件(在一個圖像),或類似的東西?

如果不是,我當然不會通過FileOutputStream作爲您的連續調用(2,3,4)的輸入參數,而是代表文件夾的File的實例。在上一個方法(4)中,爲該文件夾中的文件創建(並在下載後)一個新的FileOutputStream。像

public void downloadFile(String fileName, File folder) throws IOException{ 
    OutputStream outputStream = new FileOutputStream(new File(folder, filename)); 
    try{ 
    getDbxClient().getFile(fileName, null, outputStream); 
    }catch(IOException e){ 
    throw e; 
    }catch(DbxException u){ 
    throw new IOException(u.getCause()); 
    }finally { 
    outputstream.close(); 
    } 
} 
+0

問題是,我需要在JEE中爲我的應用程序分層次使用類。我需要OutputStream到達Dropbox類,Dropbox類到達用OutputStream容器測試類。 感謝您的關注 –