This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes How to Return a File using Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to Return a File using Servlet" Watch "How to Return a File using Servlet" New topic
Author

How to Return a File using Servlet

MohanRaj Gurubatham
Greenhorn

Joined: Sep 05, 2001
Posts: 16
Hi,
A servlet using which I need to return a file which will automatically invoke the Save As.. window or any other downloading application.
Give direct application link to the user is not needed here.
This some what similar to Java site. In this site for downloading any SDKs or any files we will not be given with the direct URL. A servlet will lead to the file after few clicks. I need to setup the same functionality in my program.
Please clear me this doubt with good examples.
Thank you in-advance.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Try this:

Rene
[ October 10, 2002: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
MohanRaj Gurubatham
Greenhorn

Joined: Sep 05, 2001
Posts: 16
Hi,
This is my code...
/* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.net.*;
public class SendFileN extends HttpServlet {
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
// ##########################################################################
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
String fileURL="http://localhost/webstore/resources/java/app/CConverter111111.jad";
try {
URL url =new URL(fileURL);
URLConnection connection = url.openConnection();
connection.setDoInput( true );
connection.setDoOutput( true );

res.setContentType("text/plain");
res.setHeader("Content-disposition", "attachment; filename=CConverter111111.jad");
ServletOutputStream out = res.getOutputStream();

bis = new BufferedInputStream(connection.getInputStream());
bos = new BufferedOutputStream(out);

byte[] buff = new byte[2048];
int bytesRead;

while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
out.println();
return;

} catch(final MalformedURLException e) {
System.out.println ( "MalformedURLException." );
throw e;
} catch(final IOException e) {
System.out.println ( "IOException." );
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
}
/* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
When I run this servlet sometime I am getting the expected result. But some time it prints the file contents in the browser itself.
Please fix this.
Thank you in advance.
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
Originally posted by MohanRaj Gurubatham:
[QB]When I run this servlet sometime I am getting the expected result. But some time it prints the file contents in the browser itself.
QB]

This happens because setting the ContentType as "text/plain" makes the browser believes it can open it, so it does.
If you always want to automatically invoke the 'Save As..' window try to mislead the browser with a diferent content type it can not manage:
res.setContentType("application/octet-stream"); ,for instance.
HTH
MohanRaj Gurubatham
Greenhorn

Joined: Sep 05, 2001
Posts: 16
Hi,
I am sorry for not informing you in the previous reply itself that I have tried with the content type as application/octet-stream also.
Here also I am getting the same problem.Please fix this.
Thank you in advance.
Mario Rendon
Greenhorn

Joined: Jul 11, 2002
Posts: 24
it depends on the Client not in the
server .
i mean .
if you put context type, what you are doing is to suggest the browser wich application to use .
if the browser doesn't know the application you are suggesting then opens the "Save as" window.
if the browser find's how to manage the info you are sending according to the context type you're putting
then it tries to manage it .
 
 
subject: How to Return a File using Servlet
 
Similar Threads
Help with File download
upload and download file to any machine in a n/w
Where to download i18n.jar file?
Buzz Buzz NOT!
Accessing file on remote windows share