【问题标题】:How can i save my pdf in the client side?如何在客户端保存我的pdf?
【发布时间】:2018-12-05 22:39:13
【问题描述】:

我有一个服务已经保存了 pdf 但保存在服务器中, 我将不胜感激,我尝试使用 servlet,但我认为我做得不对 这是我的代码:

@POST
@Path("reporteComisiones")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public void reporteComisiones (Dtorest2 dtorest2) throws DocumentException, FileNotFoundException, ParseException, IOException
{      
        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;
       try{ 
            FileInputStream archivo = new FileInputStream("C:/reportes/"+dtorest2.persona_id+"-"+formateador.format(fecha)+".pdf"); 
            int longitud = archivo.available(); 
            byte[] datos = new byte[longitud]; 
            archivo.read(datos); 
            archivo.close(); 
            response.setContentType("application/pdf"); 
            response.setHeader("Content-Disposition","attachment;filename="+dtorest2.persona_id+"-"+formateador.format(fecha)+".pdf"); 
            ServletOutputStream ouputStream = response.getOutputStream(); 
            ouputStream.write(datos); 
            ouputStream.flush(); 
            ouputStream.close(); 
          }catch(Exception e){  
              e.printStackTrace();  
          }  
        */
    }

【问题讨论】:

  • 这是什么意思:“在客户端保存 pdf”?
  • 我将我的项目部署在服务器中,但是当在其他电脑(客户端)中使用应用程序时,应用程序将文件保存在服务器中
  • 我的回答对你有帮助吗?

标签: java pdf servlets itext report


【解决方案1】:

在服务器端生成文件后,您可以将其下载到客户端。此代码可能对您有所帮助:

@RequestMapping(path = "/download", method = RequestMethod.GET)
public ResponseEntity<Resource> download(String param) throws IOException {

    //First generate your file here (jasper, etc)

    InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

    return ResponseEntity.ok()
            .headers(headers)
            .contentLength(file.length())
            .contentType(MediaType.parseMediaType("application/octet-stream"))
            .body(resource);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 2019-12-10
    • 2013-07-15
    • 2023-03-09
    • 2012-07-10
    • 2015-12-28
    • 1970-01-01
    相关资源
    最近更新 更多