Re: File downloads and URLs that can't be reused
Re: File downloads and URLs that can't be reused
- Subject: Re: File downloads and URLs that can't be reused
- From: Davin <email@hidden>
- Date: Fri, 8 Jul 2005 15:54:43 -0700 (PDT)
Hello Danny,
If the media server and the server running the WO App
are the same, then you could just simply read any file
on the disk, not necessarily in the DocumentRoot, by
creating a file input stream, then in your
PageWithMediaFileToDownload.java add:
public WOActionResults myDownload() {
String pathToFile;
File tempFile = new File(pathToFile);
InputStream in = (InputStream)(new
FileInputStream(tempFile));
int contentLength = (int)tempFile.length();
int bufferSize = (contentLength < 1024*1024) ?
contentLength : 1024*1024;
WOResponse response = new WOResponse();
response.setContentStream(in,bufferSize,contentLength);
response.setHeader("application/octet-stream","content-type");
response.setHeader("attachment;filename=\"" +
tempFile.getName().replaceAll(" ","_") + "\"",
"content-disposition");
// Set more headers if necessary
// ...
return response;
}
Bind the Hyperlink's Action to myDownload;
If the media server and the server running the WO App
are different (as you have said they are), you'll need
to some how be able to access the files on the media
server. Then you'll need to create a InputStream for
that file. If it's on your local network you could try
using Samba, to share the files on the media server.
They can be accessed using jcifs
(http://jcifs.samba.org/). Create a SmbFileInputStream
which is a subclass of InputStream so you can use some
of the same code from the myDownload action, e.g.
public WOActionResults myDownload() {
String smbURLToFileOnMediaServer;
SmbFile tempFile = new
SmbFile(smbURLToFileOnMediaServer);
InputStream in = (InputStream)(new
SmbFileInputStream(tempFile));
int contentLength = tempFile.length();
int bufferSize = (contentLength < 1024*1024) ? (int)
contentLength : 1024*1024;
WOResponse response = new WOResponse();
response.setContentStream(in,bufferSize,contentLength);
response.setHeader("application/octet-stream","content-type");
response.setHeader("attachment;filename=\"" +
tempFile.getName().replaceAll(" ","_") + "\"",
"content-disposition");
// Set more headers if necessary
// ...
return response;
}
If you don't like SMB you could try FTP (pure java):
URL url = new
URL("ftp://user01:email@hidden/mediaFile.mp4;type=i");?URLConnection
urlc = url.openConnection();?InputStream in =
urlc.getInputStream();
int contentLength = urlc.getContentLength();
... same stuff as before ....?
This works if your using at least version 5.2 of WO.
You'll also need to catch a few exceptions that are
thrown.
And finally,
To make these links one time only so that they can't
be reused. I see at least two ways to go:
Approach 1.) In your application make sure that access
to the myDownload action is given only once;
Approach 2.) move the myDownload action into the
DirectAction class. Create a table in your database
with 2 non-primary key attributes: mediaUrl and
digestedMediaUrl. The media url is the string that
identifies the file at the remote location, the
digestedMediaUrl is a string which contains some
obscured version of the mediaUrl (look up
java.security.MessageDigest). In your app give people
the digestedMediaUrl. In the DirectAction class get
the digestedMediaUrl from the request, Fetch the EO
whose digestedMediaUrl matches the one from the
request, on that EO get the correponsing mediaUrl, and
proceed through the rest of the myDownloadAction as
usual. But be sure to remove that EO from the database
by the end of the action so that the next time the
action is called there will be no mediaUrl for the
corresponding digestedMediaUrl.
Things become a tad more complicated when you ask for
the media file to be downloaded once _per User_, but
that is left as an exercise for the reader.
Hope this helps,
Davin.
____________________________________________________
Sell on Yahoo! Auctions no fees. Bid on great items.
http://auctions.yahoo.com/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden