This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Return a File using Servlet

 
MohanRaj Gurubatham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

Rene
[ October 10, 2002: Message edited by: Rene Larsen ]
 
MohanRaj Gurubatham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic