Using NetBeans 6.5, Internet Explorer 7.
I am using the code example from BalusC at the site:
http://balusc.blogspot.com/2006/05/pdf-handling.html
I am having no problem reading and displaying the PDF file, but it displays it in the same window,
not a new window and it overwrites the current page, so I can't use the back arrow to fetch the page that is overwritten.
I am using a
commandLink to fetch the file.
Does anyone know why I am not getting a new window or tab for the display?
The jsp portion for the link is:
<h:commandLink action="#{MainPage.linkAction3_action}" id="linkAction3"
style="color: rgb(0, 0, 0); font-family: Arial,Helvetica,sans-serif; font-size: 12px; font-weight: bold; left: 280px; top: 0px; position: absolute"
target="_blank" value="Insurance Document"/>
The "MainPage.linkAction_action" method just makes a call to the display function:
public String linkAction3_action() {
sb1.setMessage("");
dsb.setFilePath("C:/");
dsb.setFileName("Insurance_Summary.pdf");
dsb.downloadPDF();
return null;
}
And the downloadPDF() method is basically a cut and paste from the BalusC site;
public void downloadPDF()
{
// Reference: http://balusc.blogspot.com/2006/05/pdf-handling.html
// Prepare.
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
File file = new File(getFilePath(), getFileName());
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
// Open file.
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
// Init servlet response.
response.reset();
response.setContentType("application/pdf");
response.setContentLength( (int)file.length());
response.setHeader("Content-disposition", "inline; filename=\"" + fileName + "\"");
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
// Write file contents to response.
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Finalize task.
output.flush();
}
catch( Exception e )
{
System.out.println( "Error displaying file.");
}
finally {
// Gently close streams.
close(output);
close(input);
}
// Inform JSF that it doesn't need to handle response.
// This is very important, otherwise you will get the following exception in the logs:
// java.lang.IllegalStateException: Cannot forward after response has been committed.
facesContext.responseComplete();
}