display in browser blob files (.doc, .pdf...) stored in the Database
843844Nov 12 2007 — edited Nov 15 2007Hi,
I want to display blob file from de DB to the browser, but when I click on the link of the document I want to open, nothing append. If I check the length of the file it is correct. I tried to write it in a directory to see what appends. I can only write txt file. The other files are corrupted.
I did something like in the topik 532271.
Did I do something wrong ? Do I have to write some code in the web.xml ?
Here is my code :
public DataOutputStream showBlob() throws SQLException, IOException{
//it is my class not those from java.io
File file = (File) this.getFiles().getRowData();
FacesContext faces = FacesContext.getCurrentInstance();
HttpServletResponse resp = (HttpServletResponse)faces.getExternalContext().getResponse();
Blob b = file.getBlob();
int len = 0;
InputStream is = b.getBinaryStream();
long length = b.length();
byte[] x = new byte[10240];
resp.setHeader("Content-Disposition","filename=\"" + file.getFileName() + "\"");
resp.setContentType(file.getMimeType().getMime());
resp.setContentLength((int)length);
DataOutputStream o = new DataOutputStream(resp.getOutputStream());
while ( (len = is.read(x)) != -1)
o.write(x, 0, len);
o.flush();
o.close();
is.close();
faces.responseComplete();
return o;
}
In my xhtml page I have :
<ice:commandLink id="downloadLink"
action="#{managerBean.layoutManager.ediTraining.showBlob}"
value="#{file.fileName}" target="_blank">
</ice:commandLink>
Thank you for your response.
Edited by: joce77 on Nov 12, 2007 7:39 AM