Click a link and that submits a form to DownloadFile.ofm. The only thing included in the form is the fully-qualified path and filename to download (security isn't an issue in this instance). The DownloadFile action contains the following code:
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
byte[] ba = new byte[in.available()];
int bytesRead = in.read(ba);
if (bytesRead > 0 && !response.isCommitted()) {
out.write(ba);
}
out.flush();
And that's it. Things worked out great. I just return null from here, since there is no view to forward to (I assume it's valid to do that within Struts? Can't think of too many instances where returnign null from an action would be what you wanted to do, but I didn't seem to see any complaints, so I guess it's OK to do).
Maybe that'll help someone else out some time.
From: "None None" <[EMAIL PROTECTED]> Reply-To: "Struts Users Mailing List" <[EMAIL PROTECTED]> To: [EMAIL PROTECTED] Subject: Implementing a file download Date: Mon, 10 May 2004 14:23:04 -0400
Ok, so in my continuing effort to learn Struts by writing a file manager app, things are going very well. But, now I find myself at another stumbling block...
Is there any easy way to implement a file download in Struts WHEN THE TARGET FILE COULD BE ANYWHERE IN THE FILE SYSTEM? It doesn't seem that a direct or forward will work because they both seem to rely on paths relative to the servlet container. I need to be able to specify an absolute path and filename to forward to (I wouldn't expect a redirect to work under any circumstances, I thnik it has to be a forward).
I'm thinking of using streams to stream the file through the response object, but before I go through all that effort I thought I'd ask if anyone has solved this problem already in a less work-intensive manner?
Thanks in advance!
_________________________________________________________________
Is your PC infected? Get a FREE online computer virus scan from McAfeeŽ Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
_________________________________________________________________ Check out the coupons and bargains on MSN Offers! http://youroffers.msn.com
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]