Here you can find the source of toURI(String location)
Parameter | Description |
---|---|
location | the location String to convert into a URI instance |
Parameter | Description |
---|---|
URISyntaxException | if the location wasn't a valid URI |
public static URI toURI(String location) throws URISyntaxException
//package com.java2s; /**//from w w w .j a va 2 s . co m * Copyright 2008-2015 Jordi Hern?ndez Sell?s, Ibrahim Chaehoi * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language governing permissions * and limitations under the License. */ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; public class Main { /** * Create a URI instance for the given URL, replacing spaces with "%20" URI * encoding first. * <p> * Furthermore, this method works on JDK 1.4 as well, in contrast to the * {@code URL.toURI()} method. * * @param url * the URL to convert into a URI instance * @return the URI instance * @throws URISyntaxException * if the URL wasn't a valid URI * @see java.net.URL#toURI() */ public static URI toURI(URL url) throws URISyntaxException { return toURI(url.toString()); } /** * Create a URI instance for the given location String, replacing spaces * with "%20" URI encoding first. * * @param location * the location String to convert into a URI instance * @return the URI instance * @throws URISyntaxException * if the location wasn't a valid URI */ public static URI toURI(String location) throws URISyntaxException { return new URI(location.replace(" ", "%20")); } }