-
File Download with Java (5 messages)
- Posted by: abhishek bhargava
- Posted on: March 07 2005 07:30 EST
I want to implement the facility of downloading files through my web application, which are stored on some directory (say c:\files) on the server. How can i do this using jsp,EJB?Threaded Messages (5)
- File Download with Java by Fred Bloggs on March 07 2005 09:28 EST
- File Download with Java by Fred Bloggs on March 07 2005 16:07 EST
- File Download with Java by Surya Rani on March 21 2005 03:21 EST
- Can be done from anywhere in the filesystem by Antonie Malan on August 11 2005 10:33 EDT
- File Download with Java by Fred Bloggs on March 07 2005 16:07 EST
- File Download with Java by abhishek bhargava on March 09 2005 09:11 EST
-
File Download with Java[ Go to top ]
- Posted by: Fred Bloggs
- Posted on: March 07 2005 09:28 EST
- in response to abhishek bhargava
Do you mean downloading (from server to client) or upload (from client to server)? -
File Download with Java[ Go to top ]
- Posted by: Fred Bloggs
- Posted on: March 07 2005 16:07 EST
- in response to Fred Bloggs
Re-reading you post I guess you meant from the server. I don't know if you can do this out of the web context but within the web context there are various techniques. One the works well is to use a servlet output stream – I'm passing the resource path info as extra path info on the HTTP – you could also do this as a query string or whatever.
So: given a path like http://localhost:8080/twofish/servlet/ViewResource/mp3s/TimesTablas.mp3
My view resource servlet:
// Use a ServletOutputStream because we may pass binary information
final ServletOutputStream out = res.getOutputStream();
res.setContentType("application/octet-stream");
//Get the URL
// Get the resource to view
String file = req.getPathInfo();
if (file == null) {
out.println("Extra path info was null; should be a resource to view");
return;
}
// Convert the resource to a URL
URL url = getServletContext().getResource(file);
if (url == null) { // some servers return null if not found
out.println("Resource " + file + " not found");
return;
}
//Serve the file
InputStream in = url.openStream();
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = in.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}
Pushes out the TimesTablas.mp3 from the mp3's directory.
Most browsers, when they receive the “application/octet-stream“ content type, offer the user a dialog box asking them where to save the content. The exception to this rule is, of course, Microsoft’s Internet Explorer, which, if it recognises the content type delivered, ignores the server-assigned content and displays the content normally so you may need to prompt your users to right click and do "Save As" -
File Download with Java[ Go to top ]
- Posted by: Surya Rani
- Posted on: March 21 2005 15:21 EST
- in response to Fred Bloggs
Hi,
How can I implement this using with multiple threads.
I mean I would like to down load a file from a webserver with multiple connections to the file and download it.
Like download manager apps. -
Can be done from anywhere in the filesystem[ Go to top ]
- Posted by: Antonie Malan
- Posted on: August 11 2005 22:33 EDT
- in response to Fred Bloggs
First of all, thanks to Fred Bloggs who set me on the right path. The problem with storing your files in the web-context is that everyone with a browser can get at them by entering the right string in the address field of the browser. This solution is very close to that of Fred Bloggs.
First, it's done in the doGet of a servlet. Send the filepath info like so:
Click <a href="DownLoadServlet?file=C:\\uploads
FSG.doc">
here</a> to download FSG.doc
The following is the same as in Fred's posting, just for completeness sake:
final ServletOutputStream out = res.getOutputStream(); res.setContentType("application/octet-stream");
Some differences in the following:
File file = new File(req.getParameter("file"));
BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = is.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}
is.close();
out.close();
And that's it. The only change I'm going to make is to open this .doc file in OpenOffice and export it as a pdf. -
File Download with Java[ Go to top ]
- Posted by: abhishek bhargava
- Posted on: March 09 2005 09:11 EST
- in response to abhishek bhargava
Thanks that worked!