<action name="downloadFiles" method="downloadFiles" class="org.filemanager.action.SystemAction">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${folderOrFileName}"</param>
<param name="bufferSize">200000</param>
</result>
</action>
As code shown above:
How can I set the content type, so that any content type can be downloaded?
please visit this link to get the detail List of MIME Types by Content Type.
Here is some main types
application/mac-binhex40 hqx
application/msword doc
application/msword dot
application/octet-stream *
application/octet-stream bin
application/octet-stream class
application/octet-stream dms
application/octet-stream exe
And the thing which you are looking for is already used by you application/octet-stream for *
Related
I am developing a web application using struts2 .. In that I am downloding a file when user clicks a link ..
When user clicks the link I am checking in my action class whether requested file is existed or not if it is existed then it is working fine ...
WHen it is not existed I am giving a action error message .. But That page is redirecting to error page that i have mapped globally .. IN my console ther is no exception message except these error lines ..
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.
And I can see that action message in my error page ... I have debugged the problem also .. Mapping also perfect ..
<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
<param name="bufferSize">1024</param>
</result>
Is It possible to map back to the same JSP when The file is not existed in SYSTEM .. Thanks
Check whether the file exists or not in the action, and return success & error respectively. You can do something like this :
<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
<param name="bufferSize">1024</param>
</result>
<result name="error">/error.jsp</result>
error.jsp
<s:actionerror/>
<s:actionmessage/>
or your hard-coded message
But since, you need to go back to the same page, then I suggest calling the download action using ajax and return 200 on success and 204 on error something like this :
<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
<param name="bufferSize">1024</param>
</result>
<result name="error" type="httpheader">
<param name="status">204</param>
</result>
The ajax call can be something like - docs
$.ajax({
statusCode: {
404: function() {
alert( "page not found" );
},
204: function() {
alert( "file not found" );
}
}
});
This question already has answers here:
File is not downloading from server
(3 answers)
Closed 9 years ago.
I am using Struts2 and Liferay for developing an application.
My struts.xml is as follows:
<action name="download" class="com.stp.portal.view.DownloadAction">
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="abc.pdf"</param>
<param name="bufferSize">1024</param>
</result>
</action>
I basically want to download a pdf file from the server. But I get the following error:
10:05:55,782 ERROR [Jsr168Dispatcher:38] Could not execute action
java.lang.IllegalArgumentException: application/pdf is not a supported mime type
at com.liferay.portlet.MimeResponseImpl.setContentType(MimeResponseImpl.java:159)...
Don't know how to resolve this. Would really appreciate someone's help. Really need this to be done.
Looking at the source code seems like only application/vnd.wap.xhtml+xml and text/html are allowed.
Source: http://grepcode.com/file/repo1.maven.org/maven2/com.liferay.portal/portal-impl/6.0.2/com/liferay/portlet/MimeResponseImpl.java
Put portletUrlType="resource" to the <s:url> tag which you use to create download link.
<s:url var="downloadUrl" action="download" portletUrlType="resource" />
I have multiple actions that after completion redirect back to a general page (showStuff). I'm looking for a way to NOT repeat the list of parameters for every redirect-action.
What I have is this:
<action name="doThis" class="com.domain.package.MyAction" method="doThis">
<result type="redirectAction">
<param name="actionName">showStuff</param>
<param name="parse">true</param>
<param name="selectedYear">${selectedYear}</param>
<param name="selectedMonth">${selectedMonth}</param>
<param name="selectedDay">${selectedDay}</param>
</result>
</action>
<action name="doThat" class="com.domain.package.MyAction" method="doThat">
<result type="redirectAction">
<param name="actionName">showStuff</param>
<param name="parse">true</param>
<param name="selectedYear">${selectedYear}</param>
<param name="selectedMonth">${selectedMonth}</param>
<param name="selectedDay">${selectedDay}</param>
</result>
</action>
I would like to keep the parameter list within the showStuff action definition, and then use is like so:
<action name="doThis" class="com.domain.package.MyAction" method="doThis">
<result type="redirectAction">
<param name="actionName">showStuff</param>
</result>
</action>
<action name="doThat" class="com.domain.package.MyAction" method="doThat">
<result type="redirectAction">
<param name="actionName">showStuff</param>
</result>
</action>
Is it possible?
There are a few options.
Honestly, I'd skip most of my workarounds, and put them into session.
Once they're in session, create an interceptor and interface (Dateable or something). In the interceptor check the session for the variables (see below) and if the action is a Dateable, set them on the action, and you're done.
Another option is to encapsulate these variables as a date and either use the built-in converter or use your own converter. Then you'd only need a single parameter. This option would work with the interceptor idea as well.
As it turns out, it is very much possible. This is how you do it:
Add a global result:
<global-results>
<result name="show-stats" type="redirectAction">
<param name="actionName">showStats</param>
<param name="parse">true</param>
<param name="selectedYear">${selectedYear}</param>
<param name="selectedMonth">${selectedMonth}</param>
<param name="selectedDay">${selectedDay}</param>
</result>
And then for the actions:
<action name="doThis" class="com.domain.package.MyAction" method="doThis"/>
<action name="doThat" class="com.domain.package.MyAction" method="doThat"/>
Finally in the java code, just:
return "show-stats";
And you're done.
As a sidenote, why do I have to spend so much time trying to adhere to the very basic DRY principle? Aren't all these frameworks supposed to .. you know.. simplify stuff? Just wondering...
I was facing the same problem with an endless list of params getting longer and longer, repeated in several places. What I ended up doing was that I created an external file and declared it in struts.xml as an entity then included it instead of repeating all the params
This goes in the doctype tag
<!ENTITY referenceName SYSTEM "fileName">
Then you include it like so
&referenceName;
In my struts2 application i want to define a global configuration part and want to use it where needed.
To clarify let me share one action definiton below;
<action name="do_login" class="xxx.actions.AuthAction" method="doLogin">
<result name="success" type="json">
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<param name="excludeProperties">actionErrors,actionMessages,errorMessages,errors,fieldErrors,locale,texts,action,username,password,errorCode,errorMessage,session</param>
<param name="ignoreHierarchy">false</param>
</result>
<result name="error" type="json">
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<param name="excludeProperties">actionErrors,actionMessages,errorMessages,errors,fieldErrors,locale,texts,action,session</param>
<param name="ignoreHierarchy">false</param>
</result>
</action>
as you can see, in result definitions there are some repeating parts which are;
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<param name="excludeProperties">actionErrors,actionMessages,errorMessages,errors,fieldErrors,locale,texts,action,username,password,errorCode,errorMessage,session</param>
<param name="ignoreHierarchy">false</param>
i want to define this repeating parts globally at the beginning of struts.xml file and use it in each result definition like;
<result name="success" type="json">
{include global definition here}
</result>
i know it is possible to split struts.xml by packages or actions, but i'm wondering it is possible to do above my need?
thx in advance for any response.
Struts2 doesnt have anything for global parameters though they have for global result type,
so you can very well have both success and error as global result and use them.
They have something called as static parameters which is for Action class but not for result type as you need.
<action name="trial_employee"
class="parity.action.TrialPrintEmployeeCopyAction">
<result>/protected/trial_employee.pdf</result>
</action>
I've got an action class that generates a PDF file for download. How do I configure the action result to return the PDF?
The above doesn't work, it gives me a 404 error because the pdf isn't physically on the drive, it's being generated dynamically.
If it is being generated dynamically you can return it as a stream something like
<result name="pdf File" type="stream">
<param name="contentType">${contentType}</param>
</result>
more details are here
Stream result