Display Pdf from client side Servlets

0

someone could help me see my error, I am trying to see or save a pdf on the client side, I am new to servlets, I would appreciate any support, thank you This is my code so far

@POST
@Path("reporteComisiones")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public void reporteComisiones (Dtorest2 dtorest2) throws DocumentException, FileNotFoundException, ParseException, IOException
{      
        Double TotalPositivo = 0.00;
        Double TotalNegativo = 0.00;
        Double TotalFinal = 0.00;
        Date fecha = new Date();

        SimpleDateFormat formateador = new SimpleDateFormat("dd-MM-yyyy");
        List<DtoOutrest2> lista = serComisiones(dtorest2);
        Document document = new Document();
        FileOutputStream FOS = new FileOutputStream("C:\reportes\"+dtorest2.persona_id+"-"+formateador.format(fecha)+".pdf");
        PdfWriter.getInstance(document, FOS);
        document.open();
        Paragraph titulo = new Paragraph("Reporte Comisiones",FontFactory.getFont(FontFactory.TIMES_BOLD,20,Font.BOLD,BaseColor.BLUE));

        titulo.setAlignment(titulo.ALIGN_CENTER);
        document.add(titulo);
        Paragraph Indicefecha = new Paragraph("Del "+ dtorest2.FechaIni + "hasta el "+ dtorest2.FechaFin,FontFactory.getFont(FontFactory.TIMES_BOLD,12));
        Indicefecha.setAlignment(Indicefecha.ALIGN_CENTER);
        document.add(Indicefecha);
        document.add(new Paragraph("----------------------------------------------------------------------------------------------------------------------------------"));

        document.close();

        HttpServletResponse response = null;
        InputStream is = new FileInputStream("C:\reportes\"+dtorest2.persona_id+"-"+formateador.format(fecha)+".pdf");
        byte[] data = new byte[is.available()];
        is.read(data);

        response.setContentType("application/pdf;");
        response.setHeader("Content-Disposition","attachment;filename="" + dtorest2.persona_id+"-"+formateador.format(fecha)+".pdf";");
        response.setContentLength(data.length);
        javax.servlet.ServletOutputStream servletoutputstream = response.getOutputStream();

        servletoutputstream.write(data);
        servletoutputstream.flush();
        servletoutputstream.close();

    }
    
asked by Henrry José Portilla Paredes 05.12.2018 в 21:43
source

1 answer

0

I have the following implementation, I hope it works.

@Path("/path")
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response descargarArchivo() {
  File file = new File("/ruta_archivo_servidor.ext");
    if(file.exists()) {
      ResponseBuilder response = Response.ok((Object) file);
      response.header("Content-Disposition", "attachment;filename=" + archivo);
      return response.build();
    }else {
      return Response.status(Status.NOT_FOUND).build();
    }
}
    
answered by 06.12.2018 в 01:07