2013-09-25 3 views
4

PDF 파일을 표시해야하는 JSP 사이트를 만들고 있습니다. 나는 webservice에 의해 PDF 파일의 바이트 배열을 가지고 있으며 HTML로 PDF 파일로 바이트 배열을 표시해야합니다. 내 질문은 PDF로 그 바이트 배열을 비밀리에하고 새로운 탭에 그 PDF를 표시하는 것입니다.바이트 배열을 PDF로 변환하고 JSP 페이지에 표시

답변

1

슬프게도, 당신이 사용하는 기술을 알려주지 않습니다. 스프링 MVC와

, 당신의 컨트롤러 메소드의 주석으로 @ResponseBody를 사용하여 간단히과 같이 바이트를 반환 : 새 탭에서

@ResponseBody 
@RequestMapping(value = "/pdf/shopping-list.pdf", produces = "application/pdf", method=RequestMethod.POST) 
public byte[] downloadShoppingListPdf() { 
    return new byte[0]; 
} 

열기는 HTML에서 처리 할 수있는 관련이없는 문제입니다.

+0

스피 사용하여 최대 절전 모드 및 JSP, 어떻게 수를 나는 그 동작을 JSP로 수행한다. – abhi

3

출력 스트림을 사용하여 디스크에 이러한 바이트를 저장하십시오.

FileOutputStream fos = new FileOutputStream(new File(latest.pdf)); 

//create an object of BufferedOutputStream 
bos = new BufferedOutputStream(fos); 

byte[] pdfContent = //your bytes[] 

bos.write(pdfContent); 

그런 다음 클라이언트 쪽에서 해당 링크를 열어 보냅니다. http://myexamply.com/files/latest.pdf처럼

3

더 나은 당신이 어떤 HTML을 제공하지 않기 때문에, 이것에 대한 서블릿을 사용하는 것입니다,하지만 당신은 바이트 [] 스트리밍하려는 :

public class PdfStreamingServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

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

    public void processRequest(final HttpServletRequest request, 
     final HttpServletResponse response) throws ServletException, 
     IOException { 

     // fetch pdf 
     byte[] pdf = new byte[] {}; // Load PDF byte[] into here 
     if (pdf != null) { 
      String contentType = "application/pdf"; 
      byte[] ba1 = new byte[1024]; 
      String fileName = "pdffile.pdf"; 
      // set pdf content 
      response.setContentType("application/pdf"); 
      // if you want to download instead of opening inline 
      // response.addHeader("Content-Disposition", "attachment; filename=" + fileName); 
      // write the content to the output stream 
      BufferedOutputStream fos1 = new BufferedOutputStream(
       response.getOutputStream()); 
      fos1.write(ba1); 
      fos1.flush(); 
      fos1.close(); 
     } 
    } 
} 
+0

URL의 사용법은 무엇인가? url1 = new URL (url); 왜 이런 식으로 설정합니까? byte [] ba1 = new byte [1024]; – abhi

+0

당신은 URL이 필요하지 않습니다, 이것은 내가 다른 것을 위해 사용했던 일부 코드 였고 그것을 제거하는 것을 잊었습니다. byte []와 마찬가지로, 이것은 BufferedOutputStream의 Buffer입니다. –

관련 문제