I have a problem downloading a pdf file, I get it from Database (Sql Server 2008 R2), the data is of type image in BD, but I capture in a blob and then turn it into byte array. But to transform it into a StreamedContent to download the pdf file goes corrupted and I can not open it.
Code Java DaoImpl:
@Transactional(readOnly=true)
@Override
public DetalleSolicitudBean obtenerDocumentoImg(DetalleSolicitudBean detalle) throws Exception {
StringBuilder sql = new StringBuilder();
sql.append("SELECT iCorImagen, siCodMun, iCodDocumento, ");
sql.append("vNomArcDig, imgImagen ");
sql.append("FROM GAImgDocumento ");
sql.append("WHERE iCodDocumento = ? AND siCodMun = ? ");
List<DetalleSolicitudBean> lista = jdbcTemplateImgDoc.query(sql.toString(),
new Object[] { detalle.getIcodDocumento(), detalle.getSiCodMun() },
new RowMapper<DetalleSolicitudBean>() {
public DetalleSolicitudBean mapRow(ResultSet rs, int rowNum) throws SQLException {
DetalleSolicitudBean det = new DetalleSolicitudBean();
det.setIcodImagen(rs.getShort("iCorImagen"));
det.setSiCodMun(rs.getShort("siCodMun"));
det.setIcodDocumento(rs.getInt("iCodDocumento"));
det.setVnomArcDig(rs.getString("vNomArcDig"));
Blob blob = rs.getBlob("imgImagen");
byte[] imagen = blob.getBytes(1L, (int) blob.length());
det.setImgImagen(imagen);
return det;
}
});
}
Code Java Controller:
private StreamedContent file;
---------------------------------------
DetalleSolicitudBean detalle = new DetalleSolicitudBean();
detalle.setIcodDocumento(Integer.valueOf(codigoDocumento));
detalle.setSiCodMun(Constantes.CODIGO_MUNICIPALIDAD);
DetalleSolicitudBean imagenDocSolicitud = iSolicitudService.obtenerDocumentoImg(detalle);
if(imagenDocSolicitud == null){
addWarnMessage(null, getMessage("solicitudes.form.valAdjDocAsociado"));
}else{
InputStream stream = null;
try {
String nameFile = imagenDocSolicitud.getVnomArcDig();
stream = new ByteArrayInputStream(imagenDocSolicitud.getImgImagen());
file = new DefaultStreamedContent(stream, "application/pdf", nameFile);
} catch (Exception ex) {
depurador.error(getGenerarError(Thread.currentThread()
.getStackTrace()[1].getMethodName(),
Constantes.NIVEL_APP_CONSTROLLER,
this.getClass().getName(), ex.getMessage()));
} finally {
stream.close();
}
}
Code .xhtml:
<p:dataTable id="tblSolicitudes" var="item"
value="#{cBusquedaSolicitud.listSolicitudes}"
rowIndexVar="rowIndex" lazy="true" rows="10"
paginator="true" paginatorPosition="top"
rowKey="#{solicitud.codSolicitud}"
selection="#{cBusquedaSolicitud.selectSolicitud}">
....
<p:column style="text-align: center;">
<p:commandLink actionListener="#{cAtencionSolicitud.descargarDocumento}"
rendered="#{item.icodDocumento != null}" ajax="false"
action="docVistaEscrito" update=":listaMensajes">
<span class="ui-icon ui-icon-pdf" />
<f:param name="paramDocumento" value="#{item.icodDocumento}" />
<p:fileDownload value="#{cAtencionSolicitud.file}" />
</p:commandLink>
</p:column>
</p:dataTable>
You don't need field file in class controller. You should have method in your controler:
public StreamedContent getFile(){}
I was testing it and I think the problem saving to save the pdf to the database (so I think I bring a damaged pdf). The pdf file I keep it as a byte array.
Code Java Controller:
public void guardarDocSolicitud(){
try{
if(this.fileDocSolicitud) == null){
addWarnMessage(null, getMessage("solicitudes.form.valAdjuntarDocumento"));
}else{
InputStream stream = null;
try {
stream = this.fileDocSolicitud.getInputstream();
byte[] b = new byte[stream.available()];
this.detalle.setImgImagen(b);
this.detalle.setVnomArcDig(this.fileDocSolicitud.getFileName());
iSolicitudService.guardarDocumentoImg(detalle, getCVariableSesion().getSiCodUsu(), getCVariableSesion().getCnomTer());
} catch (IOException ex) {
depurador.error(getGenerarError(Thread.currentThread()
.getStackTrace()[1].getMethodName(),
Constantes.NIVEL_APP_CONSTROLLER,
this.getClass().getName(), ex.getMessage()));
} finally {
stream.close();
}
addInfoMessage(null, getMessage("msg.info.operacionExitosa"));
buscar(null);
}
}catch (Exception ex) {
addErrorMessage(null, getMessage("msg.error.errorGrabar"));
depurador.error(getGenerarError(Thread.currentThread()
.getStackTrace()[1].getMethodName(),
Constantes.NIVEL_APP_CONSTROLLER,
this.getClass().getName(), ex.getMessage()));
}
}
Code Java DaoImpl:
@Transactional
@Override
public void guardarDocumentoImg(DetalleSolicitudBean detalle) throws Exception {
try{
StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO GAImgDocumento ");
sql.append("(iCorImagen, siCodMun, iCodDocumento, ");
sql.append("vNomArcDig, imgImagen, bActivo, ");
sql.append("siCodUsu, sdFecAct, cNomTer) ");
sql.append("VALUES(?,?,?,?,?,?,?,?,?) ");
jdbcTemplateImgDoc.update(sql.toString(),
new Object[] { detalle.getIcodImagen(), detalle.getSiCodMun(), detalle.getIcodDocumento(),
detalle.getVnomArcDig(), detalle.getImgImagen(), Constantes.TRUE,
detalle.getSiCodUsu(), detalle.getSdFecAct(), detalle.getCnomTer()
});
}catch(Exception e){
throw new Exception( getGenerarError(Thread.currentThread().getStackTrace()[1].getMethodName(),
Constantes.NIVEL_APP_DAO,
this.getClass().getName(),
e.getMessage()) );
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.