0

Javaを使用して動的WebプロジェクトのWebContentフォルダーにファイルを作成する方法を知りたいですか?

残っている基本的な質問は、WebContentフォルダーのパスを取得する方法です。

注:サーブレットは使用されません!

編集:

さて、私はjavaメソッドから新しいxmlファイルを作成しようとしています。アプリケーションがデプロイされている場合でもファイルが作成されるように、ファイルをWebContentフォルダーに作成したいと思います。

Jboss、maven、JSFを使用して動的Webプロジェクトを作成しています。ハイチャートにデータを渡すためにxmlファイルが必要です。この方法のみを使用することに注意してください。

概要:

リクエストに応じてxmlファイルを作成します

WebContentフォルダー自体に作成されるXMLファイル

このxmlファイルを使用してデータを渡します

4

1 に答える 1

-1

グラスフィッシュ ソリューション。AbstractSearchPageBean - クラスのいずれか

private static final String WEB_INF = "WEB-INF";

public static String getWebPath() {
    final String webInfPath = getWebInfPath();
    return webInfPath.substring(0, webInfPath.indexOf(WEB_INF) - 1);
}

public static String getWebInfPath() {
    String filePath = "";

    java.net.URL url = AbstractSearchPageBean.class.getResource("AbstractSearchPageBean.class");
    if (url != null) {
        String className = url.getFile();
        filePath = (className.contains(WEB_INF)) ? className.substring(0, className.indexOf(WEB_INF) + WEB_INF.length()) : className;
    }
    return filePath.replace("%20", " ");
}

// Create file in webapp/xml directory
private void createXmlFile(String xml) {
    try {
        String fileName = System.currentTimeMillis() + ".xml";
        File file = new File(Settings.getWebPath() + File.separatorChar + "xml" + File.separatorChar + fileName);
        logger.debug("parseXML(): Creating file: " + file.getAbsolutePath());
        if (file.createNewFile()) {
            FileWriter fw = new FileWriter(file);
            fw.write(this.parseXML(xml));
            fw.flush();
            fw.close();
            logger.debug("parseXML(): file saved to the: " + Settings.getAPPLICATION_DOMAIN() + '/' + fileName);
        } else {
            logger.warn("parseXML(): Can't create file: " + file.getAbsolutePath());
        }
    } catch (IOException ioe) {
        logger.error("parseXML(): Bad save file: ", ioe);
    }
}
于 2012-02-09T12:17:24.593 に答える