You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by RogerV <ro...@googlemail.com> on 2010/11/19 10:43:40 UTC

File Download with multiple files. Design question

Hi

I have a requirement to present the user with a list of files stored on the
server, and then download the files selected to the user. If the user
selects a single file - no problem. However if the user selects multiple
files, then, unless anyone knows different, all the files have to be sent in
the single input stream read by the stream result. Therefore I create a
temporary file with File.createTempFile() and use the ZipOutputStream to zip
the files and then return an input stream over the temp file. This works
suprisingly well. However, the problem I've now got is, how to get rid of
the temp files I am creating on the server?

file.deleteOnExit() is no use because the server VM will (in theory) never
shutdown. Once the download is complete Struts automatically re-displays the
original selection screen and provided the user uses the applicatoin
supplied navigation to exit then I can delete the temp file, but if they
simply close the browser or use the browser back button to navigate away I'm
still left with the temp files sitting around. 

Creating the zipfile in memory is not an option as some of these can
potentially be huge if the user goes mad and selects everything in sight.

Is there someway of detecting/intercepting the fact that the Stream result
has completed before Struts gets on with deciding what .jsp to display next
so that I can safely clean up? Any other suggested approaches would be
welome.

Regards

-- 
View this message in context: http://old.nabble.com/File-Download-with-multiple-files.-Design-question-tp30256036p30256036.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File Download with multiple files. Design question

Posted by Rahul Mohan <ra...@tcs.com>.
Roger,

In my project, we implemented a FileManager for storing, retrieving and 
cleaning up files. On upload, the filemanager creates a temp file on disk 
and stores the file-URL in a map correlated with the session id. We also 
implemented a SessionListener which calls FileManager.deleteFiles()  with 
the session-id as a parameter whenever a session is invalidated.  Deleting 
file after finish of streaming might not be a good idea, since it is very 
common to download the same file multiple times.

- Rahul



From:
RogerV <ro...@googlemail.com>
To:
user@struts.apache.org
Date:
19-11-2010 15:14
Subject:
File Download with multiple files. Design question




Hi

I have a requirement to present the user with a list of files stored on 
the
server, and then download the files selected to the user. If the user
selects a single file - no problem. However if the user selects multiple
files, then, unless anyone knows different, all the files have to be sent 
in
the single input stream read by the stream result. Therefore I create a
temporary file with File.createTempFile() and use the ZipOutputStream to 
zip
the files and then return an input stream over the temp file. This works
suprisingly well. However, the problem I've now got is, how to get rid of
the temp files I am creating on the server?

file.deleteOnExit() is no use because the server VM will (in theory) never
shutdown. Once the download is complete Struts automatically re-displays 
the
original selection screen and provided the user uses the applicatoin
supplied navigation to exit then I can delete the temp file, but if they
simply close the browser or use the browser back button to navigate away 
I'm
still left with the temp files sitting around. 

Creating the zipfile in memory is not an option as some of these can
potentially be huge if the user goes mad and selects everything in sight.

Is there someway of detecting/intercepting the fact that the Stream result
has completed before Struts gets on with deciding what .jsp to display 
next
so that I can safely clean up? Any other suggested approaches would be
welome.

Regards

-- 
View this message in context: 
http://old.nabble.com/File-Download-with-multiple-files.-Design-question-tp30256036p30256036.html

Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



=====-----=====-----=====
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you



Re: File Download with multiple files. Design question

Posted by Paweł Wielgus <po...@gmail.com>.
Hi Roger,
below is Off-Topic as for struts.

> That's what I'm doing, but the StreamResult requires an InputStream
> which forces the intermediate step of creating a temporary file

I'm not so sure, i haven't done it myself, but there were a discussion
here some time ago
pointing out that one can create some kind of sophisticated
MySpecialBufferedStream
that will do exactly what You want.
In short this stream would need to have access to single files
but creates zip stream on-the-fly.
Google "creating zip stream on the fly"  - there are some nice results.

Best greetings,
Paweł Wielgus.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File Download with multiple files. Design question

Posted by Roger Varley <ro...@googlemail.com>.
On Sat, 2010-11-20 at 09:41 +0100, Paweł Wielgus wrote:
> Hi All,
> read about streamResult (I assume You use struts2), and also there is
> no need for next action in chain, user can simply check as many files
> as he wants and click download what will call downloadAction that will
> simply return zip file for download, after downoading user is still at
> the same page.
> 
Hi Pawel

That's what I'm doing, but the StreamResult requires an InputStream
which forces the intermediate step of creating a temporary file and it's
the issue of how to clean up the temporary file (knowing when the
StreamResult has completed) that's causing the issue.

Li's suggestion of writing directly to the response.getOutputStream()
would work, but it means my action now has to know about the servlet api
and will be awkward to test.

A third alternative I was considering was something like;

public class DeleteOnCloseFileInputStream extends FileInputStream {

	File file;

	public DeleteOnCloseFileInputStream(File file) {
		super(file);
		this.file = file;
	}
	@Override
	public void close() {
		super.close();
		file.delete();
	}
		
}

and passing that to the StreamResult.

Regards


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File Download with multiple files. Design question

Posted by Paweł Wielgus <po...@gmail.com>.
Hi All,
read about streamResult (I assume You use struts2), and also there is
no need for next action in chain, user can simply check as many files
as he wants and click download what will call downloadAction that will
simply return zip file for download, after downoading user is still at
the same page.

Best greetings,
Pawel Wielgus.



2010/11/19 RogerV <ro...@googlemail.com>:
>
>
>
> Li Ying wrote:
>>
>> My suggestion:
>>
>> (1)I believe you can use ZipOutputStream to output the zipped data to
>> the response OutputStream directly, instead of a temp file. So no temp
>> file need to be created.
>>
>
> Hmm, write to the response.outputStream directly from within my action? That
> would work I guess. Any examples of doing this in struts 2 and how to
> "navigate" to the next display after the download?
>
> In this instance the first option would be best as I don't need the temp
> file, it was my first solution to providing the StreamResult with an input
> stream that would download multiple files.
>
> Regards
>
> --
> View this message in context: http://old.nabble.com/File-Download-with-multiple-files.-Design-question-tp30256036p30258354.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File Download with multiple files. Design question

Posted by Li Ying <li...@gmail.com>.
> Any examples of doing this in struts 2

You can get the HttpServletResponse by:
   ServletActionContext.getResponse()

And then, you can set the download file name, by:
   response.setHeader("Content-Disposition",
		"attachment; filename=" + fileName);

And then, you can get the OutputStream, by:
  response.getOutputStream()

Finally, you can output zipped data to the OutputStream.



> how to "navigate" to the next display after the download?

I believe you can not navigate to the next page.
Because for one http request, the server side can send only one response.
If you want to download file and then show next page, it need 2 responses.
I think this is impossible mission for one http request.

If you send a file download response, then the browser will download
it, and don't refresh the page, which means the current displayed page
will remain.

If you really want to implement this, may be you need some client side
JavaScript to send a file download request first, and then send
another request to display the next page.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File Download with multiple files. Design question

Posted by RogerV <ro...@googlemail.com>.


Li Ying wrote:
> 
> My suggestion:
> 
> (1)I believe you can use ZipOutputStream to output the zipped data to
> the response OutputStream directly, instead of a temp file. So no temp
> file need to be created.
> 

Hmm, write to the response.outputStream directly from within my action? That
would work I guess. Any examples of doing this in struts 2 and how to
"navigate" to the next display after the download?

In this instance the first option would be best as I don't need the temp
file, it was my first solution to providing the StreamResult with an input
stream that would download multiple files.

Regards

-- 
View this message in context: http://old.nabble.com/File-Download-with-multiple-files.-Design-question-tp30256036p30258354.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File Download with multiple files. Design question

Posted by Li Ying <li...@gmail.com>.
My suggestion:

(1)I believe you can use ZipOutputStream to output the zipped data to
the response OutputStream directly, instead of a temp file. So no temp
file need to be created.

OR

(2)You can create a batch application, repeatedly run it with some
interval (use cron or something).
And in this batch app, you can check the timestamp of your temp files,
and delete them if they are old enough.

I think the first way is the best, because there is not any side effects.
But the second way is also valuable, because it can help you to clean
up your working folder when you really need temp files.



2010/11/19 RogerV <ro...@googlemail.com>:
>
> Hi
>
> I have a requirement to present the user with a list of files stored on the
> server, and then download the files selected to the user. If the user
> selects a single file - no problem. However if the user selects multiple
> files, then, unless anyone knows different, all the files have to be sent in
> the single input stream read by the stream result. Therefore I create a
> temporary file with File.createTempFile() and use the ZipOutputStream to zip
> the files and then return an input stream over the temp file. This works
> suprisingly well. However, the problem I've now got is, how to get rid of
> the temp files I am creating on the server?
>
> file.deleteOnExit() is no use because the server VM will (in theory) never
> shutdown. Once the download is complete Struts automatically re-displays the
> original selection screen and provided the user uses the applicatoin
> supplied navigation to exit then I can delete the temp file, but if they
> simply close the browser or use the browser back button to navigate away I'm
> still left with the temp files sitting around.
>
> Creating the zipfile in memory is not an option as some of these can
> potentially be huge if the user goes mad and selects everything in sight.
>
> Is there someway of detecting/intercepting the fact that the Stream result
> has completed before Struts gets on with deciding what .jsp to display next
> so that I can safely clean up? Any other suggested approaches would be
> welome.
>
> Regards
>
> --
> View this message in context: http://old.nabble.com/File-Download-with-multiple-files.-Design-question-tp30256036p30256036.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org