Need help in figuring out, why the downloaded file size is 0 bytes? On click of the download button it the page pops up a save or open dialog box, when I choose save with some location is saves the file, but it is a empty file. Whats wrong with it?
JSP file
<form target="_blank" method="get" action="/csm/download.action" >
<input type="hidden" id="absFileName" name="absFileName" value="">
<input type="submit" class="btn" id="btnDownloadConfig" value="Download Configuration"/>
</form>
Struts.xml
<action name="download" class="com.abc.csm.actions.DownloadConfiguration">
</action>
My code for download
String filePath = ServletActionContext.getServletContext().getRealPath("/")
filePath+=executionResponse
def splits=filePath.split("/")
cfgfileFileName=splits[splits.length-1]
println filePath+", "+cfgfile+", "+cfgfileFileName+", "+executionResponse
File f=new File(filePath)
println("Does file Exists? "+f.exists())
InputStream inputStream = new FileInputStream(f)
response.setContentType("APPLICATION/xml")
response.addHeader("Content-Disposition", "attachment; filename=\""+cfgfileFileName+"\"")
My ouput in console
E:\Tomcat 6\webapps\csm\files//1123/Infa9_1_csmclientbeetle.xml, Infa9_1_csmclientbeetle.xml, files//1123/Infa9_1_csmclientbeetle.xml
Does file Exists? true
My file location in the tomcat webapps
E:\Tomcat 6\webapps\csm\files\1123
Update
I found a similar question that helped me
This is what i did with InputStream
FileInputStream ins = new FileInputStream(f)
OutputStream out = response.getOutputStream()
byte[] buf = new byte[1024]
int len = 0
while ((len = ins.read(buf)) >= 0)
{
out.write(buf, 0, len)
}
ins.close()
out.close()
I believe you can use S2 build in
Stream resulttype to handle your download functionality in more flexible way.All you need to definefileInputStreamin your action class which will be used to download your content.you can set all other things in your configuration file dynamically.Here is a sample code
you can use stream result tin your struts.xml file
All the above param, inside your action tag can be set dynamically. all you need to define the properties in you action class and use them in the configuration.
e.g if you want to set content-type dynamically create a property in your action class with its getters and setters and set the values of this property inside your execute/any other method.
you need to use the dynamic property value in your struts.xml file like
for details about the various properties than can be set inside your stream result, see the official doc
stream-result