Java InputStream 中的文件路径/名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2501936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File path/name from InputStream
提问by Muhammad Hewedy
How to obtain File Path/Name from an InputStream in Java ?
如何从 Java 中的 InputStream 获取文件路径/名称?
采纳答案by b.roth
It's not possible.(not from the FileInputStream in the Java API). The FileInputStream
constructor does not store this information in any field:
这是不可能的。(不是来自 Java API 中的 FileInputStream)。该FileInputStream
构造函数不存储在任何领域这样的信息:
public FileInputStream(File file) throws FileNotFoundException {
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
}
if (name == null) {
throw new NullPointerException();
}
fd = new FileDescriptor();
open(name);
}
回答by Pyrolistical
You can't because the InputStream
might not have been a file or path. You can implement your own InputStream
that generates data on the fly
你不能,因为它InputStream
可能不是一个文件或路径。您可以实现自己的动态InputStream
生成数据
回答by E-Riz
Two important Object Oriented Design principles prevent you from doing what you're asking: abstraction and encapsulation.
两个重要的面向对象设计原则阻止您做您所要求的事情:抽象和封装。
- Abstractionis the process of defining a general concept that has only the details necessary for its use in a particular context (more details here). In this situation, the abstraction is
InputStream
, which is a general interface that can provide bytes, regardless of the source of those bytes. The abstraction of anInputStream
has no concept of a file path; that's only relevant to particular implementations ofInputStream
. - Encapsulationis the process of hiding implementation details of a class from consumers/users of that class. In this particular situation,
FileInputStream
encapsulates the details of the file it is reading from, because as anInputStream
that information is not relevant to the usage. Thepath
instance field is encapsulatedand as such unavailable to users of the class.
- 抽象是定义一个一般概念的过程,该概念仅具有在特定上下文中使用所需的细节(更多细节在这里)。在这种情况下,抽象是
InputStream
,这是一个可以提供字节的通用接口,而不管这些字节的来源。an 的抽象InputStream
没有文件路径的概念;这仅与InputStream
. - 封装是对类的消费者/用户隐藏类的实现细节的过程。在这种特殊情况下,
FileInputStream
封装它正在读取的文件的详细信息,因为InputStream
该信息与使用无关。该path
实例字段被封装,因此无法使用这些类的用户。
Having said that, it ispossible to access the path
variable if you are willing to accept some important limitations. Basically, the gist is that you can check if the InputStream
is, in fact, an instance of FileInputStream
and, if so, use reflection to read the path
instance field. I'll leave out the details of doing that access since it's easily discoverablein the java.lang.Class
Java docs and online, and not really a generally good thing to do in most contexts. Since the question doesn't provide a context about why, it's hard to offer any more reasonable approaches.
话虽如此,如果您愿意接受一些重要的限制,则可以访问该path
变量。基本上,要点是您可以检查它是否InputStream
实际上是一个实例FileInputStream
,如果是,则使用反射来读取path
实例字段。我将省略执行该访问的详细信息,因为它很容易在java.lang.Class
Java 文档和在线中找到,并且在大多数情况下通常不是一件好事。由于该问题没有提供有关原因的上下文,因此很难提供任何更合理的方法。