简体   繁体   English

Struts2文件上传并保存

[英]Struts2 file upload and save

I have successfully uploaded a file in Struts2 with the following code: 我已经使用以下代码在Struts2中成功上传了文件:

String filePath = servletRequest.getSession().getServletContext().getRealPath("/") + "WEB-INF\\files\\";
            fileToCreate = new File(filePath, getUserDocFileName());

            FileUtils.copyFile(this.userDoc, fileToCreate);

The problem is that the file gets copied to the \\build\\web\\WEB-INF\\files folder. 问题是该文件被复制到\\ build \\ web \\ WEB-INF \\ files文件夹中。 Now, when I try to retrieve the file using result type stream: 现在,当我尝试使用结果类型流检索文件时:

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>

I get the following exception: 我得到以下异常:

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.

Any help regarding this problem? 关于这个问题有帮助吗?

Is FileUtils.copyFile approach same as setting struts.multipart.saveDir property? FileUtils.copyFile方法是否与设置struts.multipart.saveDir属性相同?

How can I place the file in the WEB-INF folder but not in the build folder? 如何将文件放在WEB-INF文件夹中,而不放在build文件夹中?

The files should not get deleted whenever I build my project again. 每当我再次构建项目时,都不应删除文件。 I want to deploy it to OpenShift later on. 我想稍后将其部署到OpenShift。

尝试从servlet上下文中获取它

InputStream is = ServletActionContext.getServletContext().getResourceAsStream("/WEB-INF/files/" + fileName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM