tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From h...@informatiefabriek.nl
Subject Re: Writing to response's OutputStream cause problem.
Date Thu, 01 May 2003 09:54:10 GMT
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


Mime
View raw message