本文介绍了克服java.net.MalformedURLException:无协议异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个属性文件,其中包含一个属性,该属性指定包含温度数据集的NOAA网站的URL.该属性包含[DATE_REPLACE]令牌,因为当NOAA生成新的预测时,URL每天都会更改.

I have a properties file that contains a property specifying the URL of a NOAA web site containing a temperature data set. The property contains a [DATE_REPLACE] token because the URL changes daily when NOAA generates a new forecast.

在我的属性文件中,我指定:

In my properties file, I am specifying:

WEATHER_DATA_URL="http://weather.noaa.gov/pub/SL.us008001/DF.anf/DC.mos/DS.mex/RD.[DATE_REPLACE]/cy.00.txt"

我已经声明了一个带有PropertyHelper类(java.util.Properties的包装器)的方法,以使用WEATHER_DATA_URL作为名称" yyyyMMdd "来生成当天的URL字符串.日期格式,即今天的日期.

I have declared a method withing a PropertyHelper class (a wrapper for java.util.Properties) to generate the URL String for the current day using WEATHER_DATA_URL as the name, "yyyyMMdd" as the date format, a today's Date.

public String getPropertyWithDateReplaceToken(String name, String dateFormat, Date dateToFormat)
{
    String value = this.properties.getProperty(name);

    if (StringHelper.isNullOrWhitespace(value) || !value.contains("[DATE_REPLACE]"))
    {
        throw new UnsupportedOperationException("The property value should specify the [DATE_REPLACE] token");
    }

    StringBuilder sb = new StringBuilder(value);
    int index = sb.indexOf("[DATE_REPLACE]");
    while (index != -1)
    {
        String replacement = StringHelper.getTodayAsDateString(dateFormat, dateToFormat);
        sb.replace(index, index + "[DATE_REPLACE]".length(), replacement);
        index += replacement.length();
        index = sb.indexOf(value, index);
    }

    return sb.toString();
}

然后,我使用以下方法调用另一个帮助程序类,以从网页中读取文本:

I then call another helper class with the following method to read the text from the web page:

public static List<String> readLinesFromWebPage(String urlText) throws Exception
{
    List<String> lines = new ArrayList<String>();
    if (StringHelper.isNullOrWhitespace(urlText))
    {
        throw new NullPointerException("URL text cannot be null or empty");
    }

    BufferedReader dataReader = null;
    try
    {
        System.out.println("URL = " + urlText);
        String trimmedUrlText = urlText.replaceAll("\\s", "");

        URL url = new URL(trimmedUrlText);
        dataReader = new BufferedReader(new InputStreamReader(url.openStream()));

        String inputLine;
        while((inputLine = dataReader.readLine()) != null)
        {
            lines.add(inputLine);
        }

        return lines;
    }
    catch(Exception e)
    {
        logger.logThrow(Level.SEVERE, e, "Exception (" + e.getMessage() + ") attempting to " +
                "read data from URL (" + urlText + ")");
        throw e;
    }
}

如您所见,我已尝试从生成的URL字符串中修剪空格,希望这是造成此问题的原因. URL字符串生成正确,但是出现以下异常:

As you can see I have tried to trim spaces from the generated URL String in the hopes that that was causing the issue. The URL string is generated properly but I am getting the following exception:

java.net.MalformedURLException: no protocol: "http://weather.noaa.gov/pub/SL.us008001/DF.anf/DC.mos/DS.mex/RD.20121219/cy.00.txt"

如果我手动设置字符串,一切正常……我想念什么?

If I set the string manually, everything works ... what am I missing?

推荐答案

您的属性文件在URL值两边加上了双引号.删除这些.

Your property file has double quotes around the value of the URL. Remove these.

这篇关于克服java.net.MalformedURLException:无协议异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:48