Return-Path: Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 94343 invoked from network); 25 Apr 2003 14:08:36 -0000 Received: from unknown (HELO frankenstein.everyage.com) (62.58.174.90) by daedalus.apache.org with SMTP; 25 Apr 2003 14:08:36 -0000 Received: by frankenstein.everyage.com with Internet Mail Service (5.5.2653.19) id ; Fri, 25 Apr 2003 16:08:32 +0200 Message-ID: From: Nico Krijnen To: 'Jakarta Commons Users List' Subject: RE: FileUpload question Date: Fri, 25 Apr 2003 16:08:29 +0200 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: text/plain; charset="iso-8859-1" X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N > Is it possible to simply stream the file contents to the response? Yes, streaming files is actualy very easy. The best way is not to use a jsp page but a simple servlet: public class MyFileServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedInputStream in; response.setContentType(...); response.setContentLength(...); try { in = new BufferedInputStream(new FileInputStream(...)); OutputStream out = response.getOutputStream(); int fileByte; while( (fileByte = in.read()) != -1 ) { out.write(fileByte); } out.flush(); } catch (IOException e) { // Download was interupted } finally { // ??? if (in != null) in.close(); } } } Make sure you set the mime-type of the response using 'setContentType(String)'. That way the browser knows it is not dealing with a html page. After the content type has been set, you can use the 'getOutputStream()' method to get the servlets output stream and write your file to it. I don't know how to get an InputStream to the file data in your repository so I used a FileInputStream as example here. That should do it! Nico Krijnen Every Age Multimedia -----Oorspronkelijk bericht----- Van: Ross Allard [mailto:Ross.Allard@sas.com] Verzonden: vrijdag 25 april 2003 15:43 Aan: commons-user@jakarta.apache.org Onderwerp: FileUpload question This is probably a basic question, but once I've gotten a file to the mid-tier (and beyond), how do I deliver it back to a user when requested? I have a struts web application that needs to handle attachments. I can use fileupload to get the file from the client into a repository. What's the process of delivering it back to the user for viewing, in such a way that the browser has enough information to know what to do? Is it possible to simply stream the file contents to the response? Any help will be appreciated. Ross Allard Sas Institute --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org