【问题标题】:Create a file from an OutputStream从 OutputStream 创建文件
【发布时间】:2016-12-16 18:35:27
【问题描述】:

我有这个问题,我正在创建一个文件,但这是在创建一个空文件。

我正在使用 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());
    }
}

谢谢提前。

【问题讨论】:

  • 您能否提供一个最小(并且格式更好)的示例,好吗? :-) 我们很乐意提供帮助。 :-)
  • 感谢您的建议,我已经修改了帖子

标签: java outputstream fileoutputstream


【解决方案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();
  }
}

【讨论】:

  • 问题是我需要在 JEE 中为我的应用程序分级分类。我需要 OutputStream 到达 Dropbox 类,并且 Dropbox 类到达带有 OutputStream 容器的测试类。感谢您的关注
【解决方案2】:

我想你必须flush(参见相应的方法)将输出组添加到你的文件中(并关闭它)。 Dropbox API 正在处理 OutputStream,但肯定不在乎您要写入文件。

你可以关注example

【讨论】:

  • 我在所有关卡中都使用了flush功能,但是文件还是空的。
  • 你已经看过dropbox.com/developers-v1/core/start/java的教程了吗?
  • 是的,Dropbox的功能运行良好。我在我的应用程序中将这个 Dropbox 功能用于其他进程。感谢您的关注
猜你喜欢
  • 2013-08-23
  • 2011-08-25
  • 2011-04-30
  • 1970-01-01
  • 2010-11-16
  • 2013-09-13
  • 2012-08-03
  • 1970-01-01
相关资源
最近更新 更多