1

次のパスの netbeans プロジェクトに保存されているファイルがあります。

ReportCSV/ReportDownload.csv

そして、このファイルに書き込むために、Struts アクション クラスに次のコードがあります。

CSVWriter writer = new CSVWriter(new FileWriter("C:\\isis\\src\\main\\webapp\\ReportCSV\\ReportDownload.csv"), '\t');

//feed in your array (or convert your data to an array)
String[] entries = "first#second#third".split("#");
writer.writeNext(entries);
writer.close();

ここでは、正確なファイル パスを指定しても機能しますが、このパスを相対パスにしたいと考えています。試してみまし"\\ReportCSV\\ReportDownload.csv"たが、うまくいきません。私に何ができるか考えていますか?どんな助けでも大歓迎です!ありがとうございました :)

4

1 に答える 1

0

アプリケーション Web への相対パスが必要な場合は、javax.servlet.ServletContext.

classを使用すると、次のorg.apache.struts2.ServletActionContextことを試すことができます。

ServletContext context = ServletActionContext.getServletContext();
String path = context.getRealPath("/ReportCSV/ReportDownload.csv");
CSVWriter writer = new CSVWriter(new FileWriter(path), '\t');
于 2013-11-02T15:31:08.780 に答える