次のコードを使用して、Struts2 にファイルを正常にアップロードしました。
String filePath = servletRequest.getSession().getServletContext().getRealPath("/") + "WEB-INF\\files\\";
fileToCreate = new File(filePath, getUserDocFileName());
FileUtils.copyFile(this.userDoc, fileToCreate);
問題は、ファイルが \build\web\WEB-INF\files フォルダーにコピーされることです。ここで、結果タイプ ストリームを使用してファイルを取得しようとすると、次のようになります。
String filePath = servletRequest.getSession().getServletContext().getRealPath("/") + "WEB-INF\\files\\";
File file = new File(filePath + getFileName());
try {
inputStream = new DataInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
<action name="loadFile" class="com.app.Controller" method="loadFile">
<result name="success" type="stream">
<param name="contentType">${fileType}</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">inline;filename="${fileName}"</param>
<param name="bufferSize">1024</param>
</result>
<interceptor-ref name="defaultStack"/>
</action>
次の例外が発生します。
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
この問題に関するヘルプはありますか?
アプローチはプロパティFileUtils.copyFile
の設定と同じですか?struts.multipart.saveDir
ファイルをビルドフォルダーではなくWEB-INFフォルダーに配置するにはどうすればよいですか?
プロジェクトを再度ビルドするたびにファイルが削除されることはありません。後で OpenShift にデプロイしたいと考えています。