java 将字符串转换为 URI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7939441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting string to URI
提问by program-o-steve
How do i convert String www.mywebsite.com/firefox.txt
to URL ? I wanted to use this in the file object and then into the FileReader
constructor.
我如何将字符串转换www.mywebsite.com/firefox.txt
为 URL?我想在文件对象中使用它,然后在FileReader
构造函数中使用它。
采纳答案by Bozho
You can use the new URI(string)
constructor (for an URI) and the new URL(string)
constructor for a URL
.
您可以使用new URI(string)
构造函数(用于 URI)和new URL(string)
构造函数用于URL
.
But that won't work with a FileReader
- it requires the URI scheme to be file:
但这不适用于FileReader
- 它需要 URI 方案file:
If you want to read a remote file, you need something like:
如果要读取远程文件,则需要类似以下内容:
Reader reader = new InputStreamReader(new URL(urlString).openStream(), encoding);
The encoding can be taken from HttpURLConnection
obtained by url.openConnection()
, but you can also set it to something specific if you know it in advance. (Btw, In the above example I've omitted all I/O resource management)
编码可以从由HttpURLConnection
获取url.openConnection()
,但如果您事先知道,也可以将其设置为特定的内容。(顺便说一句,在上面的示例中,我省略了所有 I/O 资源管理)
Note (thanks to @StephenC): the url string must be a valid URL, which means it must start with http://
注意(感谢@StephenC):url 字符串必须是有效的 URL,这意味着它必须以 http://
回答by Stephen C
If your goal is to turn that string is to turn that string (and similar ones) into syntactically valid URLs ... like a typical browser's URL bar does ... then the answer is "use heuristics".
如果您的目标是将该字符串(以及类似字符串)转换为语法上有效的 URL ......就像典型浏览器的 URL 栏所做的那样......那么答案是“使用启发式”。
Specifically, you will need to figure out what heuristics are most likely to turn the user's input into the URL that the user meant. Then write some code to implement them. I'd start by doing some experiments with a browser whose behavior you like and try to figure out what it is doing.
具体来说,您需要找出哪些启发式方法最有可能将用户的输入转换为用户所指的 URL。然后编写一些代码来实现它们。我会先用你喜欢的浏览器做一些实验,然后试着弄清楚它在做什么。
Your comment is unclear, but I think you are saying that the string is a local file name and you want to turn it into "file:" URL.
您的评论不清楚,但我认为您是说该字符串是本地文件名,并且您想将其转换为“文件:”URL。
File file = new File("www.mywebsite.com/firefox.txt");
URL url = file.toURI().toURL();
If you want to write to a remote file, you can't do that simply using URL object and openStream()
. Writing to remote resources identified by URLs typically requires an http or ftp URL, and use of libraries that implement the respective protocol stack.
如果要写入远程文件,则不能仅使用 URL 对象和openStream()
. 写入由 URL 标识的远程资源通常需要一个 http 或 ftp URL,并使用实现相应协议栈的库。
Similarly, you can't do it withe the FileWriter API ... unless the file lives in a file system that has been mounted; e.g. as an Windows network share. The FileWriter API only works for files in the unified "file system" that the local operating system makes available to your application.
类似地,你不能用 FileWriter API 做到这一点……除非文件存在于已挂载的文件系统中;例如作为 Windows 网络共享。FileWriter API 仅适用于本地操作系统为您的应用程序提供的统一“文件系统”中的文件。
Finally, we get back to the fact that you need to use a valid URL ... not just the syntactically invalid nonsense that most users type into a browser's URL bar.
最后,我们回到这样一个事实,即您需要使用一个有效的 URL……而不仅仅是大多数用户在浏览器的 URL 栏中键入的语法上无效的废话。
回答by Alon Amir
If you mean that you need to convert That string to a URL string, you can use the URLEncoder class to decode/encode Strings in Url format.
如果您的意思是需要将该字符串转换为 URL 字符串,则可以使用 URLEncoder 类对 URL 格式的字符串进行解码/编码。
for example:
例如:
String encodedurlStr = URLEncoder.encode(url.toString(),"UTF-8");
if you need to get a URL Type, the answer below is correct (using the URL class constructor)
如果需要获取 URL 类型,下面的答案是正确的(使用 URL 类构造函数)
回答by Richard Johnson
new URL("--your url here--") should turn it into a URL, however the File constructor takes a uri not a url so...
new URL("--your url here--") 应该把它变成一个 URL,但是 File 构造函数采用 uri 而不是 url 所以......
try {
URL url = new URL("--your url here--");
File f = new File( url.toURI() );
FileReader reader = new FileReader(f);
} catch (MalformedURL...IOException e.t.c.) { }
However to get the contents of the url you might want to try
但是要获取您可能想要尝试的网址的内容
new URL("--your url here--").openStream();
and maybe wrap this in an InputStreamReader ala;
并且可能将其包装在 InputStreamReader ala 中;
new InputStreamReader( new URL("--your url here--").openStream() );
Not tried any of this but see no reason it won't work :)
没有尝试过任何这些,但没有理由它不起作用:)
It may surprise the unintelligent among you that --your url here-- means you to fill in your own url.
可能会让你们中间不聪明的人感到惊讶——你在这里的网址——意味着你要填写你自己的网址。
回答by Philipp Reichart
If you want to read the contents of a URL like http://www.mywebsite.com/firefox.txt
, FileReader
won't help you even if you mangle a URL (or rather URI) into a java.io.File
somehow.
如果您想读取像 那样的 URL 的内容,即使您以某种方式将 URL(或者更确切地说 URI)弄乱成一个http://www.mywebsite.com/firefox.txt
,FileReader
也无济于事java.io.File
。
Using URL.openStream()
and an InputStreamReader
will get you the string contents of an URL, but might mess up the content because you might ignore the content encoding sent by the server.
使用URL.openStream()
and anInputStreamReader
将为您提供 URL 的字符串内容,但可能会弄乱内容,因为您可能会忽略服务器发送的内容编码。
I suggest Apache HttpClientand something liek this:
我建议使用Apache HttpClient和类似的东西:
String url = "http://www.mywebsite.com/firefox.txt";
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(new HttpGet(url));
String contents = EntityUtils.toString(response.getEntity());
For a plain text file, this should give you the contents without any encoding issues. (HTML files can specifiy their encoding using meta tags, that can't be handled 100% using an HTTP client)
对于纯文本文件,这应该为您提供没有任何编码问题的内容。(HTML 文件可以使用元标记指定它们的编码,使用 HTTP 客户端无法 100% 处理)