Java:从FilePath获取URI [英] Java: Get URI from FilePath

查看:97
本文介绍了Java:从FilePath获取URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java知之甚少。我需要在Windows上从 FilePath(String)构造一个URI的字符串表示。有时我得到的 inputFilePath 是: file:/ C:/a.txt ,有时它是: C:/a.txt 。现在,我正在做的是:

I've little knowledge of Java. I need to construct a string representation of an URI from FilePath(String) on windows. Sometimes the inputFilePath I get is: file:/C:/a.txt and sometimes it is: C:/a.txt. Right now, what I'm doing is:

new File(inputFilePath).toURI().toURL().toExternalForm()

上述工作正常,路径没有前缀文件: / ,但对于以文件为前缀的路径:/ ,。 toURI 方法正在转换通过附加当前目录的值,它到无效的URI,因此路径变得无效。

The above works fine for paths, which are not prefixed with file:/, but for paths prefixed with file:/, the .toURI method is converting it to a invalid URI, by appending value of current dir, and hence the path becomes invalid.

请通过建议正确的方法获取正确的URI来帮助我这两种路径。

Please help me out by suggesting a correct way to get the proper URI for both kind of paths.

推荐答案

这些是有效的文件uri:

These are the valid file uri:

file:/C:/a.txt            <- On Windows
file:///C:/a.txt          <- On Windows
file:///home/user/a.txt   <- On Linux

所以你需要删除文件:/ file:/// for Windows和 file:// for Linux。

So you will need to remove file:/ or file:/// for Windows and file:// for Linux.

这篇关于Java:从FilePath获取URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆