从 FileOutputStream 创建文件

发布于 2024-12-24 16:23:24 字数 413 浏览 1 评论 0原文

我正在使用 org.apache.commons.net.ftp 下载远程计算机中的文件。 有一种方法可以将文件读取到 FileOutputStream 对象。

ftpClient.retrieveFile("/" + ftpFile.getName(), fos);

问题是,我有另一种接受 File 对象的方法。因此,我需要创建一个 File 对象文件 FileOutputStream。我认为,我需要创建一个 InputStream 才能从 FileOutputStream 创建文件对象。这是正确的吗?我可能会遗漏一些东西,应该有一种简单的方法可以从 FileOutputStream 创建 File 吗?

I'm using org.apache.commons.net.ftp to download files in a remote machine.
There is a method, that reads the files to a FileOutputStream object.

ftpClient.retrieveFile("/" + ftpFile.getName(), fos);

Problem, here is, i've another method that accepts a File object. So, i need to create a File object file the FileOutputStream. I think, i need to create an InputStream to be able to create a file object from the FileOutputStream. is this correct? I might be missing something and there should be an easy way to create a File from a FileOutputStream?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我只土不豪 2024-12-31 16:23:24

FileOutputStream 有一个采用 File 对象的构造函数。

以下应该执行您需要执行的操作:

File f = new File("path/to/my/file");
if(f.createNewFile()) { // may not be necessary
    FileOutputStream fos = new FileOutputStream(f); // create a file output stream around f
    ftpClient.retrieveFile("/" + ftpFile.getName(), fos);

    otherMethod(f); // pass the file to your other method
}

FileOutputStream has a constructor that takes a File object.

The following should do what you need it to do:

File f = new File("path/to/my/file");
if(f.createNewFile()) { // may not be necessary
    FileOutputStream fos = new FileOutputStream(f); // create a file output stream around f
    ftpClient.retrieveFile("/" + ftpFile.getName(), fos);

    otherMethod(f); // pass the file to your other method
}
赠我空喜 2024-12-31 16:23:24

请注意,除了 mcfinnigan 的答案之外,您还必须知道,当您使用代码时:

FileOutputStream fos = new FileOutputStream(f); // create a file output stream around f
ftpClient.retrieveFile("/" + ftpFile.getName(), fos);

然后将在您的文件系统上的第一行创建一个空文件。然后,如果第二行抛出异常,因为路径 "/" + ftpFile.getName() 不存在远程文件,则空文件仍将位于您的文件系统上。

所以我用 Guava 做了一些 LazyInitOutputStream 来处理这个问题:

public class LazyInitOutputStream extends OutputStream {

  private final Supplier<OutputStream> lazyInitOutputStreamSupplier;

  public LazyInitOutputStream(Supplier<OutputStream> outputStreamSupplier) {
    this.lazyInitOutputStreamSupplier = Suppliers.memoize(outputStreamSupplier);
  }

  @Override
  public void write(int b) throws IOException {
    lazyInitOutputStreamSupplier.get().write(b);
  }

  @Override
  public void write(byte b[]) throws IOException {
    lazyInitOutputStreamSupplier.get().write(b);
  }

  @Override
  public void write(byte b[], int off, int len) throws IOException {
    lazyInitOutputStreamSupplier.get().write(b,off,len);
  }


  public static LazyInitOutputStream lazyFileOutputStream(final File file) {
    return lazyFileOutputStream(file,false);
  }

  public static LazyInitOutputStream lazyFileOutputStream(final File file,final boolean append) {
    return new LazyInitOutputStream(new Supplier<OutputStream>() {
      @Override
      public OutputStream get() {
        try {
          return new FileOutputStream(file,append);
        } catch (FileNotFoundException e) {
          throw Throwables.propagate(e);
        }
      }
    });
  }

我在使用 Spring 集成 remote.file 包和 FTP/SFTP 文件下载功能时遇到了这个问题。我这样用它来解决这个空文件问题:

try ( OutputStream downloadedFileStream = LazyInitOutputStream.lazyFileOutputStream(destinationfilePath.toFile()) ) {
  remoteFileSession.read(source, downloadedFileStream);
}

Note that in addition to the answer of mcfinnigan, you must know that when you use the code:

FileOutputStream fos = new FileOutputStream(f); // create a file output stream around f
ftpClient.retrieveFile("/" + ftpFile.getName(), fos);

Then an empty file will be created on your filesystem on the first line. Then if the 2nd line throws an exception, because no remote file exist for path "/" + ftpFile.getName(), the empty file will still be on your filesystem.

So I've done a little LazyInitOutputStream with Guava to handle that:

public class LazyInitOutputStream extends OutputStream {

  private final Supplier<OutputStream> lazyInitOutputStreamSupplier;

  public LazyInitOutputStream(Supplier<OutputStream> outputStreamSupplier) {
    this.lazyInitOutputStreamSupplier = Suppliers.memoize(outputStreamSupplier);
  }

  @Override
  public void write(int b) throws IOException {
    lazyInitOutputStreamSupplier.get().write(b);
  }

  @Override
  public void write(byte b[]) throws IOException {
    lazyInitOutputStreamSupplier.get().write(b);
  }

  @Override
  public void write(byte b[], int off, int len) throws IOException {
    lazyInitOutputStreamSupplier.get().write(b,off,len);
  }


  public static LazyInitOutputStream lazyFileOutputStream(final File file) {
    return lazyFileOutputStream(file,false);
  }

  public static LazyInitOutputStream lazyFileOutputStream(final File file,final boolean append) {
    return new LazyInitOutputStream(new Supplier<OutputStream>() {
      @Override
      public OutputStream get() {
        try {
          return new FileOutputStream(file,append);
        } catch (FileNotFoundException e) {
          throw Throwables.propagate(e);
        }
      }
    });
  }

I've encoutered this problem while using Spring integration remote.file packages, with the FTP/SFTP file download features. I use it like that to solve this empty file problem:

try ( OutputStream downloadedFileStream = LazyInitOutputStream.lazyFileOutputStream(destinationfilePath.toFile()) ) {
  remoteFileSession.read(source, downloadedFileStream);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文