0

jspの.propertiesファイルにキーと値のペアのような値でパラメータの値を保存する方法

例えば。
URL: http://www.xyz.com/login.jsp ? fname=harry&lname=potter&occupation=actor .property ファイル

次のようになっている必要があります



出来ますか?

前もって感謝します

4

2 に答える 2

2

これはどうですか:

final String urlString = "http://www.xyz.com/login.jsp?fname=harry&lname=potter&occupation=actor";
final URL url;
try {
    url = new URL(urlString);
} catch (MalformedURLException ex) {
    throw new RuntimeException(ex);
}
final Properties properties = new Properties();
for (final String param : url.getQuery().split("\\&")) {
    final String[] splitParam = param.split("=");
    properties.setProperty(splitParam[0], splitParam[1]);
}
for (final String key : properties.stringPropertyNames()) {
    System.out.println("Key " + key + " has value " + properties.getProperty(key) + ".");
}
final FileOutputStream fileOutputStream;
try {
    fileOutputStream = new FileOutputStream(new File("My File"));
} catch (FileNotFoundException ex) {
    throw new RuntimeException(ex);
}
try {
    properties.store(fileOutputStream, "Properties from URL '" +urlString + "'.");
} catch(IOException ex) {
    throw new RuntimeException(ex);
} finally {
    try {
        fileOutputStream.close();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }

}

これにより、URL が解析され、params がPropertiesオブジェクトに入れられ、ファイルに書き込まれます。

URL 文字列に重複するキーがある場合、それらは上書きされるため、この方法は機能しないことに注意してください。その場合は、Apache HttpComponentsを確認することをお勧めします。

于 2013-02-23T11:06:47.157 に答える
1

チェックjava.util.Properties.store(OutputStream, String)してjava.util.Properties.store(Writer, String)(Java 1.6)

于 2013-02-23T11:03:28.547 に答える