English 中文(简体)
从字符串中创建一个uri, 超过编码
原标题:Creating a uri from a string, over encoding

我在网页上解析一些链接, 然后测试这些链接是否存在。 我正在将解析链接字符串转换为 uri s, 问题在于有些链接已经有以下编码字符 : < a href="http://download.microsoft.com/download/6/3/c/63c1d527- 9d7e-4fd6-9867- fd063203660660/kinect_qsg_bndl_en-fr-es.pdf" rel="no follown" >http://dload.microsoft.com/dload/6/3/c63c1d527-9d7e-4d6-9867-fd06660660/kinect_qsg%20premium_bndl_en-fr-es.pdf < a>

当我通过下面的代码时,我得到:http://dload.micload.com/download/6/c/63c1d527-9d7e-4fd6-9867-fd063206660/kinect_qsg%2520 premium_bndl_en-fr-es.pdf

您可以看到哪些编码是% 20 。 我如何避免? 我应该先解码我的字符串吗? 如果是这样, 这样做的最佳方法是什么?

URL url = null;
        URI uri = null;
        try {
            url = new URL(checkUrl);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
        try {
            uri = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(), url.getRef());
        } catch (URISyntaxException e1) {
            e1.printStackTrace();
        }
最佳回答

尝试使用 URL Decoder 类,

        URL url = null;
        URI uri = null;
        String checkUrl = "http://download.microsoft.com/download/6/3/c/63c1d527-9d7e-4fd6-9867-fd0632066740/kinect_qsg%20premium_bndl_en-fr-es.pdf"; 
        try {
            url = new URL(URLDecoder.decode(checkUrl,"UTF-8"));
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        try {
            uri = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(), url.getRef());
            System.out.println(uri.getHost());
        } catch (URISyntaxException e1) {
            e1.printStackTrace();
        }

班级的类路径为 java.net.URLDecer

问题回答

您可以使用:

String decoded = URLDecoder.decode(yorUrl, "UTF-8");




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签