• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to Return a File using Servlet  RSS feed

 
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.
 
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.
 
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.
 
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 .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!