简体   繁体   English

Linux上的Java File.toURI()问题

[英]Java File.toURI() issue on Linux

I'm trying to get the URI of a File to put into a HTML document, which is then later processed to read the image locations (which are in the system temp directory) from that HTML and do some processing on the images. 我正在尝试获取要放入HTML文档中的文件的URI,然后再处理该文件以从该HTML中读取图像位置(位于系统临时目录中)并对图像进行一些处理。

It works fine on Windows, but on Linux I'm running into issues where the image locations from the HTML can't be found for subsequent processing. 它在Windows上可以正常工作,但是在Linux上,我遇到了无法找到HTML图像位置进行后续处理的问题。 I think the issue is as follows... 我认为问题如下...

On Windows, the File.toURI().toString() command is returning (for C:\\temp\\image.jpeg): 在Windows上,File.toURI()。toString()命令正在返回(对于C:\\ temp \\ image.jpeg):

file:/C:/temp/image.jpeg 文件:/ C:/temp/image.jpeg

On Linux, it is therefore presumably returning (for /tmp/image.jpeg): 因此,在Linux上,它大概会返回(对于/tmp/image.jpeg):

file://tmp/image.jpeg 文件://tmp/image.jpeg

This is being interpreted by the HTML parser as tmp/image.jpeg (ie a relative path), and therefore can't be found. HTML解析器将其解释为tmp / image.jpeg(即相对路径),因此无法找到。

Is there an easy workaround for this, or do I need to manually force the URI to be correct (I guess file:///tmp/image.jpeg and file://C:/temp/image.jpeg)? 是否有一个简单的解决方法,还是我需要手动强制URI正确(我猜file:///tmp/image.jpeg和file:// C:/temp/image.jpeg)?

You can try doing the following: 您可以尝试执行以下操作:

String imageUri = File.toUri().toString().replaceFirst("^file:/", "");

The regular expression should help you get the file resource on Windows as well as Linux 正则表达式应该可以帮助您在Windows和Linux上获取文件资源

As I mentioned in my comment to your question: Don't publish file: URIs to HTML documents. 正如我在对您的问题的评论中提到的: 不要发布file: HTML文档的URI。

Use relative URIs or absolute URIs with respect to the site's root instead . 请相对于网站的根目录使用相对URI或绝对URI

Google on the difference of relative and absolute URIs when it comes to publishing web sites for details. Google详细介绍了发布网站时相对和绝对URI的区别。

Now, if you are so bent over using file: URIs , then you might want to try to get the canonical path. 现在,如果您急切地使用file:URIs ,那么您可能想要尝试获取规范路径。 Hell, the canonical path is the preferred type of path one would want to use to publish file locations (except when publishing on the web as I mentioned before): 地狱,规范的路径是人们想要用来发布文件位置的首选路径类型(如我之前提到的在网络上发布时除外):

// java-like pseudocode:

String myFile = ... // some path, relative or absolute, to a file in an accessible file system

URI = (new File(myFile)).getCanonicalFile().toURI();

Check the Oracle/Sun javadoc information on File and getCanonicalFile/getCanonicalPath for details. 检查File / getCanonicalFile / getCanonicalPath上的Oracle / Sun javadoc信息以了解详细信息。

This might still not solve the issue since you are mentioning the HTML parser treats file: URIs as relative (but you do not mention what HTML parser you are using.) 由于您提到的是HTML解析器将文件视为URI,所以这可能仍不能解决问题:URI是相对的(但您没有提及正在使用的HTML解析器 。)

At the end of the day, you will do better with publishing URLs relative to your documents' root instead of using file: URIs. 最终,您可以发布相对于文档根目录的URL,而不是使用file: URI。 Those URIs were never meant to be used like that. 这些URI绝不会那样使用。

您要提供绝对路径,需要将图像导入到您的项目中,然后将您项目中的相对路径提供给代码。

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

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