如何检索 FileInputStream 的原始文件

发布于 2024-12-29 03:27:31 字数 687 浏览 2 评论 0原文

我需要根据我使用的 API 来实现带有 InputStream 参数(FileInputStream 的实例)的方法,该方法将调用带有 File 的方法> 论证。

implementedMethod(InputStream is){
    FileInputStream fis = (FileInputStream)is; //always works
    File f = (???) ????(???).???;
    calledMethod(f);
}

那么,当我只有 FileInputStream 时,如何提供该 File 呢?我不明白 FileChannelFileDescriptor (均在 FIS 中)到底是什么以及它们如何工作。

我猜测这是无法完成的,并且我必须在文件系统上实际写入一个(临时)文件。如果没有,如果我可以使用 FileChannel 或 FileDescriptor 在内存中创建文件,甚至使用 RandomAccessFile (?)我想知道如何...

是否构建一个可读的 < code>File 对象要求它存在于磁盘上的某个位置?我觉得这很奇怪...

I need based on the APIs I use, to implement a method with an InputStream parameter (instance of FileInputStream) that will call a method with a File argument.

implementedMethod(InputStream is){
    FileInputStream fis = (FileInputStream)is; //always works
    File f = (???) ????(???).???;
    calledMethod(f);
}

So how do I provide that File when all I have is a FileInputStream? I don't understand what a FileChannel or a FileDescriptor (both in FIS) are exactly and how they work.

I'm guessing that this can't be done and that I will have to actually write a (temp) file on the filesystem. If not, if I can create my file in memory instead with FileChannel or FileDescriptor, or even with something as RandomAccessFile (?) I'd like to know how...

Is it that constructing a readable File object requires it to exist somewhere on the disk? I find this odd...

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

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

发布评论

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

评论(2

梦言归人 2025-01-05 03:27:31

据我所知,没有办法从 FileInputStream 获取文件名。

据我们所知,流可以由代表套接字的文件描述符构造。

我的建议是重新考虑你的设计。

As far as I know, there is no way to get the file name from a FileInputStream.

For all we know, the stream could have been constructed from a file descriptor representing, for example, a socket.

My recommendation would be to rethink your design.

季末如歌 2025-01-05 03:27:31

当然,您可以绕过 API,并假设它并不总是有效(取决于 JDK 和 SecurityManager),请使用反射:

/**
 * Retrieves the {@link File} associated to given {@link FileInputStream}, if possible.
 * @param inputStream the input stream.
 * @return the file.
 */
@Nullable
public File retrieveFile(@NotNull final FileInputStream inputStream)
{
    @Nullable final File result;

    @Nullable String path = null;

    try
    {
        @Nullable final Field field =
            inputStream.getClass().getDeclaredField("path");
        field.setAccessible(true);
        path = (String) field.get(inputStream);
    }
    catch (@NotNull final Throwable noFieldOrNoRights)
    {
    }

    if (path == null)
    {
        result = null;
    }
    else
    {
        result = new File(path);
    }

    return result;
}

You can of course bypass the API and, assuming it won't always work (depending on the JDK and the SecurityManager), use reflection:

/**
 * Retrieves the {@link File} associated to given {@link FileInputStream}, if possible.
 * @param inputStream the input stream.
 * @return the file.
 */
@Nullable
public File retrieveFile(@NotNull final FileInputStream inputStream)
{
    @Nullable final File result;

    @Nullable String path = null;

    try
    {
        @Nullable final Field field =
            inputStream.getClass().getDeclaredField("path");
        field.setAccessible(true);
        path = (String) field.get(inputStream);
    }
    catch (@NotNull final Throwable noFieldOrNoRights)
    {
    }

    if (path == null)
    {
        result = null;
    }
    else
    {
        result = new File(path);
    }

    return result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文