使用 struts2 在 Web 应用程序中上传和下载文件

发布于 2024-08-25 08:27:20 字数 2565 浏览 5 评论 0原文

在struts2上传方法中,我可以选择上传的文件必须保存在哪里吗?我的意思是,网络中的所有示例都要求我存储在 WEB-INF 中,这肯定不是一个好主意。我希望能够将上传的文件存储在磁盘中的任何位置。

我该怎么做呢?我可以在 ServletContextAware 拦截器的帮助下做到这一点吗?

当我使用

public class DownloadFileAction extends ActionSupport implements ServletContextAware{
    //private InputStream inputStream;
    private int fileid;
    private ServletContext servletContext;
    private FileCrud fileCrud;
    private MyFile myFile;

    public FileCrud getFileCrud() {
        return fileCrud;
    }
    public void setFileCrud(FileCrud fileCrud) {
        this.fileCrud = fileCrud;
    }
    public MyFile getMyFile() {
        return myFile;
    }
    public void setMyFile(MyFile myFile) {
        this.myFile = myFile;
    }
    public InputStream getInputStream(){

        String homepath = "c:\\files";
        String fname = null;
        try{
            fname=getFileCrud().getAFileName(getFileid());
        }catch(Exception e){
            e.printStackTrace();
        }
        String thePathToFile = homepath+File.separator+fname;
        //File targetfile = new File(thePathToFile);

        return getServletContext().getResourceAsStream(thePathToFile);
    }
//  public void setInputStream(InputStream inputStream) {
//      this.inputStream = inputStream;
//  }
    public int getFileid() {
        return fileid;
    }
    public void setFileid(int fileid) {
        this.fileid = fileid;
    }
    public ServletContext getServletContext() {
        return servletContext;
    }
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
    public String execute(){
        return SUCCESS;
    }

}

struts xml 文件时,

<action name="fileDownload" class="com.projit1.file.DownloadFileAction">
            <result type="stream" name="success">
                <param name="inputName">inputStream</param>
                <param name="contentType">application/octet-stream</param>

            </result>
        </action>

我收到以下错误,

javax.servlet.ServletException: 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.
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)

我正在尝试但没有得到任何结果..这有什么问题?

In struts2 upload methods, can I choose where the uploaded file must be saved. I mean, all the examples in web ask me to store in WEB-INF which surely is not a good idea. I want to be able to store the uploaded file in any place in my disk.

How should i do it? Can i do it with help of ServletContextAware interceptor ?

When I use

public class DownloadFileAction extends ActionSupport implements ServletContextAware{
    //private InputStream inputStream;
    private int fileid;
    private ServletContext servletContext;
    private FileCrud fileCrud;
    private MyFile myFile;

    public FileCrud getFileCrud() {
        return fileCrud;
    }
    public void setFileCrud(FileCrud fileCrud) {
        this.fileCrud = fileCrud;
    }
    public MyFile getMyFile() {
        return myFile;
    }
    public void setMyFile(MyFile myFile) {
        this.myFile = myFile;
    }
    public InputStream getInputStream(){

        String homepath = "c:\\files";
        String fname = null;
        try{
            fname=getFileCrud().getAFileName(getFileid());
        }catch(Exception e){
            e.printStackTrace();
        }
        String thePathToFile = homepath+File.separator+fname;
        //File targetfile = new File(thePathToFile);

        return getServletContext().getResourceAsStream(thePathToFile);
    }
//  public void setInputStream(InputStream inputStream) {
//      this.inputStream = inputStream;
//  }
    public int getFileid() {
        return fileid;
    }
    public void setFileid(int fileid) {
        this.fileid = fileid;
    }
    public ServletContext getServletContext() {
        return servletContext;
    }
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
    public String execute(){
        return SUCCESS;
    }

}

and struts xml file is

<action name="fileDownload" class="com.projit1.file.DownloadFileAction">
            <result type="stream" name="success">
                <param name="inputName">inputStream</param>
                <param name="contentType">application/octet-stream</param>

            </result>
        </action>

I get the following error

javax.servlet.ServletException: 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.
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)

I am trying but am not getting any results.. what is wrong in this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷不放纵 2024-09-01 08:27:20

要将 InputStream 写入 File,您需要 FileOutputStream

要从 File 获取 InputStream,您需要 FileInputStream

在您的代码中,您尝试使用 ServletContext#getResourceAsStream() 分配文件,但这旨在分配类路径资源。将其替换为new FileInputStream(file)

To write an InputStream to a File, you need FileOutputStream.

To get an InputStream from a File, you need FileInputStream.

In your code you're trying to allocate a file using ServletContext#getResourceAsStream(), but this is intented to allocate a classpath resource. Replace it by new FileInputStream(file).

一江春梦 2024-09-01 08:27:20

我自己刚刚完成这个实施。我建议分别解决这两个问题。首先进行文件上传,然后进行文件下载。下载支持面临的一个直接问题是,您的结果配置没有 public getInputStream() 方法:

<param name="inputName">inputStream</param>

Having just finished implementing this myself. I suggest tackling the two problems separately. Do the file uploads first then the file downloads. One immediate issue with your download support is that you don't have a public getInputStream() method per your result configuration:

<param name="inputName">inputStream</param>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文