简体   繁体   English

file.toURI()返回错误值?

[英]file.toURI() returning A Wrong value?

I work in a Java RCP application. 我在Java RCP应用程序中工作。 I am doing the following lines of code: 我正在执行以下代码行:

File file = new File(location);
String filePath = file.toURI().toString();
Desktop desktop = Desktop.getDesktop();
desktop.browse((new URL(filePath)).toURI());

where location is a String. 其中location是一个字符串。

When the value of location is: http://www.google.com , 当location的值为: http : //www.google.com时

file.toURI() 

is appending "file:/C:/eclipse%203.7.2/eclipse/" to the value and hence it becomes file:/C:/eclipse%203.7.2/eclipse/http:/www.google.com 将“ file:/ C:/eclipse%203.7.2/eclipse/”附加到该值,因此该文件变为file:/ C:/eclipse%203.7.2/eclipse/http:/www.google.com

But when the value is: C:\\Program Files, 但是当值是:C:\\ Program Files时,

file.toURI() 

is not appending anything and returning the same value correctly. 没有附加任何内容并正确返回相同的值。

Is there a limitation related to paths starting with http:// or something. 是否存在与以http://开头的路径相关的限制。 Does anyone have any idea on this ? 有人对此有任何想法吗?

java.io.File works with file paths not URLs. java.io.File使用文件路径而不是URL。

So it transforms the supplied initialization parameters into the representation that is your local file system supports. 因此,它将提供的初始化参数转换为本地文件系统支持的表示形式。

"http://" means nothing to your local file system, it's just a file name (well, wrong file name but anyway). “ http://”对您的本地文件系统没有任何意义,它只是一个文件名(嗯,错误的文件名,但无论如何)。

In the first case with " http://www.google.com " it does not see disk drive letter in the supplied value so it is considered as relative path and current working dir absolute path added as a prefix ("user.home" env var if I'm not mistaken). 在第一种情况下,使用“ http://www.google.com ”时,在提供的值中看不到磁盘驱动器号,因此将其视为相对路径和当前工作目录绝对路径作为前缀添加(“ user.home” env var(如果我没记错的话)。

In the second case, you added an absolute path "C:\\Program Files". 在第二种情况下,您添加了绝对路径“ C:\\ Program Files”。 It sees disk drive letter inside and there is no sense to add anything as a prefix. 它看到磁盘驱动器号在里面,没有任何意义添加前缀。

We have 2 types of file locations: relative and absolute. 我们有2种类型的文件位置:相对和绝对。 When the location is something like C:\\User in MS Windows or /home in Linux the location is absolute and there is no need to append something at the beginning of them! 如果该位置是MS Windows中的C:\\User或Linux中的/home之类的位置,则该位置是绝对的,因此无需在它们的开头附加任何内容! But when the location is http://google.com the program append your program location at the beginning of it. 但是,当位置为http://google.com ,程序会在其开头附加程序位置。

I think you need to search about URI and URL . 我认为您需要搜索URIURL You used them incorrectly! 您使用不正确!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM