Open PDF on the client host

1

I have a web application developed with jsp and servlets. On the jsp page I request a pdf document, which is looked for in the servlet, which is obviously on the server. The problem is that, to open, I use the class Desktop d , and when using the method% d.browse(new URI(file://240.10.10.36/Documentos/documento.pdf))
open the document on the server and not on the client PC. What I need is that on the client host that requests that document, that pc will be opened. How could I fix it?

    
asked by Carlos 22.04.2017 в 16:08
source

2 answers

1

To transfer a file from a Java servlet to a client, you must retrieve that file as InputStream and send it in the response that the server will give to the client's request. In this case, it would be done as follows:

try {
  // Consigue el InputStream del archivo. Hay múltiples maneras de hacerlo, utiliza la que mejor te convenga.
  InputStream is = ...;
  // Copia el inputStream en la respuesta que dará el cliente al servidor. "response" es de tipo HttpServletResponse.
  org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
  response.flushBuffer();
} catch (IOException ex) {
  throw new RuntimeException("Error escribiendo en la respuesta del servidor.");
}

In this way, the client (in this case, the browser) will download the recovered file on the server, that is, the PDF. If instead of being downloaded, you want it to be displayed on the page, you should use the HTML element called iframe in the jsp. A possible example would be this:

<iframe src="url que apunta al servlet del archivo"></iframe>

In this way, the PDF will be displayed in the iframe container on your page.

More information about the iframe: link

    
answered by 22.04.2017 / 21:33
source
0

To show PDFs directly in a JSP, I managed to do it in the following way using the class OutputStream :

Connection cn = null;
cn = clsBD.getConexion();
File reportfile = new
File(application.getRealPath("path/nombre_reporte.jasper"));
Map<String, Object> parameter = new HashMap<String, Object>();
String HistoriaClinica = 
 request.getParameter("txtHistoriaClinicaConsumoPaciente");
String FechaInicial = request.getParameter("txtFechaInicialConsumoPaciente");
String FechaFinal = request.getParameter("txtFechaFinalConsumoPaciente");

parameter.put("HistoriaClinica", new String(HistoriaClinica));
parameter.put("FechaInicial", new String(FechaInicial));
parameter.put("FechaFinal", new String(FechaFinal));

byte[] bytes = JasperRunManager.runReportToPdf(reportfile.getPath(), parameter, cn);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(bytes, 0, bytes.length);

outputStream.flush();
outputStream.close();

This I worked with Jasper Reports. In my case, the client consulted, and based on the parameters sent, it generated the PDF and visualized it in the browser. I hope it is useful :) [#FisrtAnswer]

    
answered by 04.05.2017 в 20:05