简体   繁体   中英

File upload and Download in a web app using struts2

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 ?

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) .

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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