tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Cox, Charlie" <c...@cincom.com>
Subject RE: Writing to response's OutputStream cause problem.
Date Thu, 01 May 2003 13:52:12 GMT
does your userfilename end in ".zip"? If not try appending it to the file
name. Does the 'filename=' need quotes? I wouldn't think that's a problem,
but I've never used quotes in content-disposition.

Alternatively you could put the zip file where the DefaultServlet can handle
it and use a redirect to get it. You could do any processing necessary in a
filter.

As a side note, I would recommend grabbing the OutputStream(as the sample
provided does) and writing to it since getOutputStream() does more than just
return the stream. It has to check to make sure that a writer was not
already requested, and also sets a flag that you requested it. It may do
more and there's no need to do those extra steps for every time you write
data.

Charlie

> -----Original Message-----
> From: Victor Hadianto [mailto:victorh@nuix.com.au]
> Sent: Thursday, May 01, 2003 6:23 AM
> To: Tomcat Users List
> Subject: Re: Writing to response's OutputStream cause problem.
> 
> 
> Hmm tried that and it still doesn't work. I have all the 
> header and content 
> type information as you pointed out. The following is the code
> 
> <snip>
>         File file = new File(zipFilePath);
>         
>         response.setContentType(MimeUtils.APPLICATION_X_ZIP);
>         response.setContentLength((int) file.length());
> 
>         response.setHeader("Content-Disposition", 
> "attachment; filename=\"" + 
> userFileName + "\";");
>         response.setHeader("Pragma", "cache");
>         response.setHeader("Cache-control", "cache");
>         
>         // Now write the zip file to the response's output stream.
>         FileInputStream in = new FileInputStream(zipFilePath);
>         BufferedInputStream bin = new BufferedInputStream(in);
>         
>         byte[] buf = new byte[1024];
>         int length;
>         while ((length = bin.read(buf)) != -1) {
>             response.getOutputStream().write(buf, 0, length);
>         }
>         
>         bin.close(); in.close();
>         response.getOutputStream().close();
>         
>         // Need to delete the zip file.
>         file.delete();
> <snip>
> 
> Don't know what's wrong though. I looked through your code 
> and I don't think I 
> can find any significant differences with mine.
> 
> Stumped again.
> 
> thanks,
> 
> victor
> 
> 
> On Thu, 1 May 2003 07:54 pm, harm@informatiefabriek.nl wrote:
> > First of all, you are better of using a buffer when reading files.
> > I had the same problem you described. Look at my code which 
> now works:
> >
> > /* Read the file and output it to the servlet outputstream */
> > try {
> >         InputStream is = new BufferedInputStream(new
> > FileInputStream(valueObj.getPath()));
> >         File f = new File(valueObj.getPath());
> >
> >         OutputStream os = response.getOutputStream();
> >
> >         //      Report the type and size of content being sent back.
> >         response.setContentType("application/pdf");
> >         response.setContentLength((int) f.length());
> >
> >         response.setHeader("Cache-Control", "no-cache");
> >         response.addHeader("Content-disposition",
> >                                   "attachment; filename=" +
> >                                   f.getName());
> >         byte[] buffer = new byte[1024]; /*or whatever size*/
> >
> >         int read = is.read(buffer);
> >         while (read >= 0) {
> >                 if (read > 0)
> >                         os.write(buffer, 0, read);
> >                 read = is.read(buffer);
> >         }
> >
> >         is.close();
> >         os.close();
> > } catch (IOException e) {
> >         e.printStackTrace();
> > }
> >
> >
> >
> >
> >
> > Victor Hadianto <victorh@nuix.com.au>
> > 05/01/2003 11:35 AM
> > Please respond to
> > "Tomcat Users List" <tomcat-user@jakarta.apache.org>
> >
> >
> > To
> > tomcat-user@jakarta.apache.org
> > cc
> >
> > Subject
> > Writing to response's OutputStream cause problem.
> >
> >
> >
> >
> >
> >
> > Hi List,
> >
> > I have a servlet that fetches a file from the server and 
> send this to the
> > reponse's output stream. The code looks something like this:
> >
> > <snip>
> >
> > FileInputStream in = new FileInputStream("/tmp/filename");
> > byte[] buf = new byte[1024];
> > int length;
> > while ((length = in.read(buf)) != -1) {
> >             response.getOutputStream().write(buf, 0, length);
> > }
> >
> > <snip>
> >
> > Now this is working fine, but I have this irritating 
> problem. Say from the
> > web
> > browser my user clicks on the link to retrieve the file:
> >
> > .../process.do?fileId=5
> >
> > The first thing that the user see is the dialog box saying:
> >
> > "You are downloading the file:
> > ../process.do?fileId=5 from hostName
> >
> > Would you save ... "
> >
> > After this dialog box I got the "real" dialog box that says:
> >
> > "Downloading file xyz.zip from hostName ..."
> >
> > I'm puzzled I don't know what went wrong here. Does anyone 
> have a solution
> > for
> > this problem?
> >
> > Many thanks in advance, any help will be much appreciated.
> >
> > victor
> >
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> >
> >
> >
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> -- 
> Victor Hadianto
> 
> NUIX Pty Ltd
> Level 8, 143 York Street, Sydney 2000
> Phone: (02) 9283 9010
> Fax:   (02) 9283 9020
> 
> This message is intended only for the named recipient. If you 
> are not the
> intended recipient you are notified that disclosing, copying, 
> distributing
> or taking any action in reliance on the contents of this message or
> attachment is strictly prohibited.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message