This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes downloading a file struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "downloading a file struts2" Watch "downloading a file struts2" New topic
Author

downloading a file struts2

Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

Hi,

I am trying to download a txt file and i am getting an error

java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [fileInputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
at org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:217

Below is the code in action Class



In Struts.xml





In Jsp



Anyone please help.

thanks
fres nillo
Greenhorn

Joined: Feb 01, 2011
Posts: 7
I don't know why you use:

# HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
# String filename =request.getParameter("userImageFileName");
# int id =Integer.parseInt(request.getParameter("id"));

when the easy way to do this is only creating an attribute in action with a getter, and setter if you need it, with the same name that you have in the jsp:

# <s:param name="userImageFileName" value="%{userImageFileName}"></s:param> <--
# <s:param name="id" value="%{id}"></s:param></s:url> <--
# <s:a href="%{fileDownload}"><s:property value="userImageFileName" /></s:a>

<s:a href="%{fileDownload}?userImageFileName=<s:property value="userImageFileName" />&id=<s:property value="id" />">

OR

<s:a href="%{fileDownload}?userImageFileName=document.getElementsByName('userImageFileName')[0].value&id=document.getElementsByName('id')[0].value"><s:property value="userImageFileName" /></s:a>


I hope that it was useful for you.
Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

Thanks Fres.

Eventhou I write as you mentiond i still get the same error.

java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [fileInputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
at org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:217
Girish Wakure
Greenhorn

Joined: Apr 14, 2010
Posts: 28
Can you post whole error stack?

I want see where you get java.lang.IllegalArgumentException.

You might get java.io.InputStream because of previous error.
as there is IllegalArgumentException, your inputName may not get initialize
Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

Nothing is log apart from this

rror invoking Action using Hibernate Core Session / Transaction injection
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [fileInputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
at org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:217)
at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:361)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:265)
at com.googlecode.s2hibernate.struts2.plugin.s2hibernatevalidator.interceptor.HibernateValidatorIn


could anyone please help, I am kind of stuck.

Thanks
vallaru
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Just to verify, that code in the action class that you have, that is from com.vaannila.web.UsersAction, right? How is that "download" method getting called? And are you sure it is getting called? Can you check to see if getFileInputStream ever gets called after your action finishes, with some logging or debugging or something?
Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

yeah its in my UserAction.java




this method is called when the image hyperlink is clicked on the jsp



yes getinputstream is getting called but its null. once the action finishes and then its throwing an error

Thanks


Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Did you verify that they download method is being called? Did you see "inside download" in your log file? The reason I'm stuck on this is I don't see anything in your code that would cause struts2 to call a method on the action other than the default of "execute()". If getFileInputStream() is getting called but its returning null, then it sounds like either the code in your "download" method isn't setting fileInputStream or else something else is nulling it back out. Perhaps you should put in some logging just before you return SUCCESS to call the getter and right to your log wheter or not the input stream is null.
Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

Hi Tom,

JSP
<

The aove code is in my jsp which puts a hyperlink on userImageFileName. When I click on the hyperlink it calls the action download. The actionclass is configured in struts.xml.

I am not clear why you feel it is not getting called.


Thanks
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
I agree, it will go to your "download" action that you have set up in your struts.xml file. From what I see in your struts.xml file, it looks like it will call the default method on your action class, which is execute. There are a few ways that I have heard of that cause struts2 to call a method other than the default one, but I don't see anything here that would make it use a method other than execute (maybe I'm just missing it).

Can you let me know how you think struts2 knows to call download and if the output from it is being written to your log file? If download isn't getting called I think that would explain your problem.
Girish Wakure
Greenhorn

Joined: Apr 14, 2010
Posts: 28
I agree with Tom..
I doubt (as tom says) default execute method is being called.
Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

Hi

sorry for wasting your time. That was real stupidity.
I was not calling the method in xml. Thanks for pointing it out.


One last question on it , is there a limit how many files we can upload.
we have 1 excel sheet and 3 images.


Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
To my knowledge there is no limit on the number of files you can uplaod. There is a maximum size restriction, I think its around 2g. You can lower that limit with the maximumSize parameter of the fileUpload interceptor.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: downloading a file struts2
 
Similar Threads
struts2 properties file reload
Dowloading a file from a a struts 2 web application
Accessing param in Struts.xml Action Mapping
problem in file downloading in struts 2.0
File download in struts2