我已经在Struts2中成功上传了一个文件,代码如下:
String filePath = servletRequest.getSession().getServletContext().getRealPath("/") + "WEB-INF\\files\\";
fileToCreate = new File(filePath, getUserDocFileName());
FileUtils.copyFile(this.userDoc, fileToCreate);
问题是文件被复制到 uild\web\WEB-INF 文件文件夹中。现在,当我尝试使用结果类型流检索文件时:
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 文件夹中而不是放在 build 文件夹中?
每当我再次构建项目时,这些文件不应被删除。我想稍后将其部署到 OpenShift。
尝试从 servlet 上下文中获取它
InputStream is = ServletActionContext.getServletContext().getResourceAsStream("/WEB-INF/files/" + fileName);