Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Download problem with JSF: Cannot open PDF-file after opening WORD-file

843844Mar 19 2007
Hello,

I have a JSP (created with Java Studio Creator) which displays a table with links to different files. Some links open PDF-documents, other for instance DOC-files (msword). I download the file as follows:

public String downloadFile(String fileName) {
try {
int dotIndex = fileName.lastIndexOf('.');
String fileExtension = "";
String contentType = "";
if(dotIndex > 0) {
fileExtension = fileName.substring(dotIndex+1, fileName.length());
contentType=CONTENT_TYPES.get(fileExtension.toUpperCase()).toString();
}

if(!fileExtension.equals("") && !contentType.equals("")) {
// method that reads file to the byte array
byte[] fileContent = getContentOfFile(new File(fileName));

FacesContext faces = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();
response.setContentType(contentType);
response.setContentLength(fileContent.length);
response.setHeader("Content-disposition", "inline; filename=\""+fileName+"\"");
try {
ServletOutputStream out;
out = response.getOutputStream();
out.write(fileContent);
} catch (IOException e) {
e.printStackTrace();
}
faces.responseComplete();
}

}
catch(Exception ex) {
return ("!!!ERROR: downloadFile(fileName) - Could not completely download file "+ fileName + ex.getMessage());
}

return "";
}

contentType is application/pdf if it`s a PDF-file or application/msword if it`s a word-file.
fileName contains the full file path.

link-properties:

<ui:hyperlink action="#{showOriginalPDF.hyperlinkShowPDF_action}" binding="#{showOriginalPDF.hyperlinkShowPDF}"
id="hyperlinkShowPDF" immediate="true" onClick="testSubmit()" target="_blank" text="#{currentRow.value['display']}"/>

and the code of the JS-function testSubmit() (without this function I couldn�t get the right row selection in the hyperlink-table):

function testSubmit() {
document.getElementById('form1').submit();
return false;
}

All pdf-links are working fine and the correct document is displayed in a new browser window. but if I open a WORD-document and after that I click on another link to a pdf-file, the WORD-file is loaded again in the new window. Only if I close the window which contains the WORD-document, I can download the correct pdf after that.

Is something wrong with the response header (I tried "attachment" instead of "inline" too but without effect)?

Can anybody help?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 16 2007
Added on Mar 19 2007
0 comments
572 views