Abrir un fichero en el navegador con Struts

2781 vistas

Tomemos el ejemplo de un fichero PDF.
En la página (por ejemplo) tenemos:



html4strict
  1. <html:link page="/openpdf.do"
  2.           name="pdfForm"
  3.           property="pdfName"
  4.           target="_blank">
  5.   <html:img src="./images/PDF.gif" border="0" alt="View PDF"/>
  6. </html:link>



En el método execute de la Action:



java
  1. // Rellenar aqui el flujo del PDF
  2.  
  3.  
  4. httpServletResponse.setContentType("application/pdf");
  5. httpServletResponse.setContentLength(bos.size());
  6.  
  7. OutputStream os = httpServletResponse.getOutputStream();
  8. os.write(bos.toByteArray(), 0, bos.size());
  9. os.flush();
  10. os.close();
  11.  
  12. return null;



Ahora el fichero PDF se abre en el navegador.