Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Seemingly simple jsp question

Thread view: 
grasp06110 - 16 Jan 2006 05:05 GMT
Hi,

I am new to this forum so please forgive me if this is not the
appropriate place to post this question.

I am working on a simple web application (jsp/servlet).  I would like
to send a file back to the client without ever creating the file on the
server.  Is there an easy way to do this?

Thanks
Grasp
Chris Smith - 16 Jan 2006 05:42 GMT
> I am working on a simple web application (jsp/servlet).  I would like
> to send a file back to the client without ever creating the file on the
> server.  Is there an easy way to do this?

Sure.  Don't use a JSP; put this in a servlet.  Use
HttpServletResponse.setContentType(String) to set the MIME type for the
result, and then use getOutputStream() to get a stream and write the
result to it.  If you don't know the length ahead of time, don't set the
content length and the response will use chunked encoding.  Otherwise,
use setContentLength(int) to set the content length header.  If you want
to strongly suggest that the client save the file rather than open it
within the browser, google for "Content-Disposition".

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

grasp06110 - 16 Jan 2006 16:11 GMT
Hi,

Thanks for the help.  The writeString method below works well but the
writeFile method does not.  The writeFile method executes but data are
not sent to the client.  The files I am sending are small so it
shouldn't be an issue for this application but it would be nice to get
the file method to write data to the client.

Thanks again,
Grasp

   private void writeString(HttpServletResponse response,
       String string) throws Exception {
       response.setContentType("application/x-download");
       response.setHeader("Content-Disposition",
               "attachment; filename=certificate.txt");
       PrintWriter out = response.getWriter();
       out.write("hello world");
   }

   private void writeFile(HttpServletResponse response, String string)
           throws Exception {
       response.setContentType("application/x-download");
       response.setHeader("Content-Disposition",
               "attachment; filename=certificate.txt");

       OutputStream out = response.getOutputStream();
       InputStream in = new ByteArrayInputStream(string.getBytes());
       int bufferSize = 1024;
       BufferedOutputStream dest =
         new BufferedOutputStream(out, bufferSize);
       int count = 0;
       byte[] data = new byte[bufferSize];
       System.out.println("writing data");
       while ((count = in.read(data, 0, bufferSize)) != -1) {
           dest.write(data, 0, count);
           System.out.write(data, 0, count);
       }
       System.out.println("");
       out.flush();
       out.close();
   }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.