如果其他人最终发布更好的答案,我会接受.与此同时,这是我提出的解决方法(用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;
}