• Post Reply Bookmark Topic Watch Topic
  • New Topic

ByteArrayOutputStream.writeTo(response.getOutputStream()); hang on when has a larger output size.  RSS feed

 
jc yin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my java program, It use ByteArrayOutputStream.writeTo(response.getOutputStream()); to create a pdf out put report. When the ByteArrayOutputStream size geting larger, the proccess hang on this step for ever if the jsp page is open from IE, but works well from Google Chrome. If the ByteArrayOutputStream size is not too big. The program works no problem on both IE and Google Chrome. Need help. Thanks in advance.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you setting the Content-Length header to the actual size with setContentLength( )?

I seem to recall some browser problems related to that.

You are not really doing this in a JSP are you? Thats a recipe for disaster since JSP will probably have already sent HTML.

Bill
 
jc yin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I set the contentlength

int size = outPut.size();
response.setContentLength(size);
 
jc yin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no problem with jsp page.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jc yin wrote:there is no problem with jsp page.


But there IS a problem. That's why you posted the question, isn't it? So ignoring possible causes of the problem isn't a good idea.

I agree with Bill, writing a PDF to the response (or in fact any file which isn't text) shouldn't be done in a JSP. So I suggest you should revise your code to do that from a servlet.
 
jc yin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java process is called from a jsp page. But after processing the application creates pdf file directly, and not going back to any other jsp page.

try {
outPut = new ByteArrayOutputStream();
response.setHeader("Cache-Control", "");
response.setHeader("Pragma", "");
if (transform) {
outPut = combinePDF(outPuts); <----------------outPuts got all out content from previous processing.
response.setContentType("application/pdf");
response.setHeader("" + outPut.size() + "", "Content-Length");
response.setHeader("attachment;filename=" +
getUserContext(request).getUserID() +
".pdf", "Content-Disposition");
} else {
response.setContentType("text/xml");
outPut = combineText(outPuts);
}
int size = outPut.size();
response.setContentLength(size);
OutputStream outstrm = response.getOutputStream();
outPut.writeTo(outstrm);
outstrm.flush();
} catch (Exception e) {
response.setContentType("application/html");
response.setHeader("Content-Disposition", "inline;filename=print.html");

outPut = writeError(e);
response.setHeader("" + outPut.size() + "", "Content-Length");
outPut.writeTo(response.getOutputStream());
throw new ServletException(e);

} finally {
if (outPut != null)
outPut.close();
}
return null;
}

more question, why it works for Google Chrome and Firefox but not for IE? Is there anything can do for IE setting? thank.


 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, do you actually mean that the request to produce this PDF is generated by HTML which was generated by a JSP? Your original post sort of suggested that the Java code in question was in the JSP. If it's the former then (of course) the fact that the original HTML was generated by JSP is irrelevant. And it's confusing to point that fact out.

Just in case you didn't already know: A JSP generates HTML (usually) and sends it to the browser as the response to a request. The browser doesn't know or care that a JSP generated the HTML.

Back to your question: IE has a long history of not behaving nicely with PDF downloads. You may want to look at other people's working PDF downloads and inspect the headers to make sure yours are right. And are you sure that the Content-Length header is supposed to have quotes (or apostrophes, or whatever those are) around it like that?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java process is called from a jsp page. But after processing the application creates pdf file directly, and not going back to any other jsp page.


Do you mean the JSP generates a link which when clicked creates a request for the pdf and the pdf is generated by a servlet - OR
do you mean that the code for pdf generation and writing to the response output stream is embedded in a JSP?

Bill
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if it's the cause of your problem but you seem to have the parameters in the wrong order for a number of your setHeader calls.

response.setHeader("Cache-Control", "");
response.setHeader("Pragma", "");
response.setHeader("" + outPut.size() + "", "Content-Length");
response.setHeader("attachment;filename=" + getUserContext(request).getUserID() + ".pdf", "Content-Disposition");
response.setHeader("Content-Disposition", "inline;filename=print.html");
response.setHeader("" + outPut.size() + "", "Content-Length");
 
jc yin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make my question simple.
The following Java is the most end of the program. outPut.writeTo(response.getOutputStream()); will directly create an output pdf file.

The program allway works perfectly if the web application run from the browser Google Chrome or Firefox. The pdf is showed up as soon as the step of
outPut.writeTo(response.getOutputStream(); processed.

The application also works well in most of cases when it running from IE, But when the ByteArrayOutputStream size getting larger, the process could hang on at the step of
outPut.writeTo(response.getOutputStream(); for ever.

Why outPut.writeTo(response.getOutputStream()); runs different from IE and Google Chrome (Firefox)? Anything need to be setup for Respose or ByteArrayOutputStream? What is returned by response.getOutputStream(); ? Is it return different from different browsers? Thanks.


try{
outPut = new ByteArrayOutputStream();

.....

response.setContentLength(outPut.size());
outPut.writeTo(response.getOutputStream());
outPut..flush();

} catch (Exception e) {
.....;

} finally {
.....;
}

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell you what . just to humor us, go find the Java code for the servlet that your JSP gets translated into.

Now - look at all the output written to the response output stream before your PDF writing statement gets to run.

How much got written by the JSP assuming it was creating a HTML page?

Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!