[JSP] 파일 읽어들여 pdf 출력하기

HTML 2018. 2. 9. 10:14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<%
 
FileInputStream fis = null;
BufferedOutputStream bos = null;
 
try{
    String fileName = "example.pdf";
    File file = new File(fileName);
    
    // 보여주기
    response.setContentType("application/pdf");
    response.setHeader("Content-Description""JSP Generated Data");
    // 다운로드
    //response.addHeader("Content-Disposition", "attachment; filename = " + file.getName() + ".pdf");
 
    fis = new FileInputStream(file);
    int size = fis.available();
    
    byte[] buf = new byte[size];
    int readCount = fis.read(buf);
 
    response.flushBuffer();
 
    bos = new BufferedOutputStream(response.getOutputStream());
    bos.write(buf, 0, readCount);
    bos.flush();
catch(Exception e) {
    response.setContentType("text/html;charset=euc-kr");
    out.println("<script language='javascript'>");
    out.println("alert('파일 오픈 중 오류가 발생하였습니다.');");
    out.println("</script>");
    e.printStackTrace();
finally{
    try{
        if(fis != null) fis.close();
        if(bos != null) bos.close();
    } catch(IOException e){
        e.printStackTrace();
    }    
}
%>
cs