小编mso*_*arr的帖子

克服java.net.MalformedURLException:没有协议异常

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

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

WEATHER_DATA_URL="http://weather.noaa.gov/pub/SL.us008001/DF.anf/DC.mos/DS.mex/RD.[DATE_REPLACE]/cy.00.txt"
Run Code Online (Sandbox Code Playgroud)

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

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();
}
Run Code Online (Sandbox Code Playgroud)

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

public static List<String> readLinesFromWebPage(String urlText) …
Run Code Online (Sandbox Code Playgroud)

java malformedurlexception

9
推荐指数
1
解决办法
5万
查看次数

标签 统计

java ×1

malformedurlexception ×1