java css路径_java – HTML中CSS文件的绝对网络路径?

如果其他人最终发布更好的答案,我会接受.与此同时,这是我提出的解决方法(用Java).关键是要生成一个始终以“file:/”开头的URI.

/**

* This method produces a URI than can be embedded in HTML to reference a file in the local

* network, for example, a CSS file. The only format that seems to work across browsers and

* operating systems is one containing five slashes, i.e., file:/server/path/to/file.css.

* Firefox is the most difficult browser to please, particularly under Windows.

*

* Note that the toString() and getPath() methods of the URI object product different results

* depending on the operating system; hence, we must remove all slashes from the start of the

* path, before adding what we want.

*

* @param fileIn

* A file object referencing the file

* @return A string that can be embedded as a URI in an HTML file

*/

private String fixHtmlUri(File fileIn) {

URI uri = fileIn.toURI();

String path = uri.getPath();

// Remove all slashes from the beginning of the path

int numSlashesAtStart = 0;

while (path.charAt(numSlashesAtStart) == '/') numSlashesAtStart++;

String strippedUriString = path.substring(numSlashesAtStart);

// Add the require five slashes plus the file protocol

return "file:/" + strippedUriString;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值