文件未找到异常但文件存在。

3

在我的FileInputStream中,我遇到了FileNotFoundException的问题,但我知道这个文件是存在的,我可以使用Web浏览器下载它。

如果我复制异常中的链接到Web浏览器中,它也能正常工作。所有人都有测试时的RWX权限,但这并没有帮助解决问题。

文件名中没有特殊字符... 我不知道为什么会出现这种情况...

谢谢你的帮助!

编辑:

KeyStore ts = KeyStore.getInstance("PKCS12");
ts.load(new FileInputStream("http://192.168.1.1/ordner/myFile.p12"), passwKey);
KeyManagerFactory tmf = KeyManagerFactory.getInstance("SunX509");
tmf.init(ts, passwKey);

出于安全考虑,我更改了链接。


所以你正在尝试使用FileInputStream来访问服务器上的文件?另外,发布你的代码可能会有所帮助。 - mort
请问您能否发布您的代码? - Kiran
请发布实际的错误信息 -- 复制/粘贴。 - Hot Licks
4个回答

6

您应该使用

InputStream is = new URL("http://stackoverflow.com/").openStream();

使用FileInputStream的替代方案。


1

查看异常信息... 可能是因为您没有权限 "java.io.FileNotFoundException (Permission Denied)"。您需要给当前运行应用程序的用户授予权限。


1

您是否将URL作为字符串传递给FileInputStream?那么您正在使用this,它的说明如下:

通过打开到实际文件的连接来创建FileInputStream,该文件由文件系统中的路径名称指定。

也许您更愿意使用URL.openStream(),它可以处理包括远程在内的任何类型的URL。


1

来自 Java 规范:

Signals that an attempt to open the file denoted by a specified pathname has failed. 

This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.

你确定,那里提到的任何情况都没有发生吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接