2014-07-08 3 views
1

웹 응용 프로그램을 통해 JasperReports 보고서를 생성했습니다. 내 localhost 페이지에있는 응용 프로그램 : 8084/app/pages.jsp (서블릿에 보내는 양식은 어디에서 왔습니까?)는 보고서를 완벽하게 생성합니다. site.com/app/pages에 제공된 보고서를 생성 할 때 문제가 발생했습니다. .jsp는이 결과를 얻었습니다. 서버가 파일을 해석하지 못하는 것 같습니다. pdf 또는 그녀의 세대. 응용 프로그램이 서버에서 보고서를 생성하지 않습니다.

는 실행중인 리눅스 서버이 오래된 프로젝트에서 제공

enter image description here

+3

서버가 적절한 Content-type 헤더를 출력하지 않아서 디스플레이 환경 (브라우저?)이 pdf를 text/plain으로 처리합니다. –

+0

맞아! 파이어 폭스에서는이 이미지 결과가 있고 IE에서는 파일 *을 다운로드합니다. txt. 서버에 무언가를 설치하거나 권한을 변경해야합니까? 고맙습니다! –

+2

서블릿이 어떤 시점에서'Content-type : application/pdf' 헤더를 출력해야합니다 ... –

답변

1

, 당신은 response.setContentType("application/pdf");을 추가하고 출력으로 파일의 바이트를 쓰기 아파치 ..

public class ReportSintesiServlet extends HttpServlet { 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("application/pdf"); 
     Lookup<IReportGeneration> l = new Lookup<IReportGeneration>(true, IReportGeneration.class); 
     IReportGeneration g = null; 
     try { 
      g = l.lookup(); 
     } catch (Exception ex) { 
      return; 
     } 
     if (g != null) { 
      String param = request.getParameter("idc"); 
      System.out.println("genero il report per idc " + param); 
      Integer idc = Integer.parseInt(param); 
      byte[] doc = (byte[]) g.generaSintesiAccordoIniziale(idc); 
      if (doc != null) { 
       FilePathManager fpm = new FilePathManager(); 
       String pathtofile = fpm.pathToNuovaSintesi(idc); 
       File temp = new File(pathtofile); 
       FileUtil.writeBytesToFile(temp, doc); 
       response.addHeader("Content-Disposition", "attachment; filename=" + temp.getName()); 
       response.setContentLength((int) temp.length()); 
       FileInputStream fileInputStream = new FileInputStream(temp); 
       OutputStream responseOutputStream = response.getOutputStream(); 
       int bytes; 
       while ((bytes = fileInputStream.read()) != -1) { 
        responseOutputStream.write(bytes); 
       } 
      } 
     } 
    } 

    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    @Override 
    public String getServletInfo() { 
     return "Short description"; 
    } 
} 

PS입니다 : 나는 이 경우 도움이되지 않은 일부 코드가 제거되었으므로 오류 관리를위한 논리의 모든 부분을 놓칠 수 있습니다.

관련 문제