Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 672285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:28:04+00:00 2026-05-14T00:28:04+00:00

In struts2 upload methods, can I choose where the uploaded file must be saved.

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:28:05+00:00Added an answer on May 14, 2026 at 12:28 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Struts2 (2.1.8.1) web application. My web.xml looks like, <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
How can i get the URL from which the user came to my Struts2
All, I have a question regarding Tabbed Panel in Struts2. If I have a
The struts2 validation framework allows you to define your validation for an action or
While using Struts2, I am using several annotations to do my validations inside the
I have a struts2 application with a single page that may show one of
I'm trying to have my Struts2 app redirect to a generated URL. In this
Well.. we've developed a j2ee application using struts2 ajax capabilities. We find that the
I have a jsp page with two radio tags. The page contains a struts2
I'm developing a Struts2 application that uses Sitemesh as template engine. What I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.