Blame view

src/main/java/com/daeucna/board/common/FileDownloadUtil.java 2.45 KB
a6468920   sangkiham   Spring Boot Board...
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  package com.daeucna.board.common;

   

  import java.io.File;

  import java.io.FileInputStream;

  import java.io.OutputStream;

  import java.net.URLEncoder;

  import java.util.Map;

   

  import javax.servlet.http.HttpServletRequest;

  import javax.servlet.http.HttpServletResponse;

  

  import org.springframework.stereotype.Component;

  import org.springframework.util.FileCopyUtils;

  import org.springframework.web.servlet.view.AbstractView;

   

  public class FileDownloadUtil extends AbstractView {

   

      public FileDownloadUtil() {        

          // 객체가 생성될 때 Content Type을 다음과 같이 변경 

          setContentType("application/download; charset=utf-8");

      }

   

      @Override

      protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {

           

          @SuppressWarnings("unchecked")

          Map<String, Object> fileInfo = (Map<String, Object>) model.get("fileInfo"); // 전송받은 모델(파일 정보)

          

          String fileNameKey = (String) fileInfo.get("fileNameKey"); // 암호화된 파일명(실제 저장된 파일 이름)

          String fileName    = (String) fileInfo.get("fileName");    // 원본 파일명(화면에 표시될 파일 이름)

          String filePath    = (String) fileInfo.get("filePath");    // 파일 경로

           

          File file = new File(filePath, fileNameKey);

           

          response.setContentType(getContentType());

          response.setContentLength((int) file.length());

   

          // 브라우저, 운영체제정보

          String userAgent = request.getHeader("User-Agent");

           

          // IE

          if (userAgent.indexOf("MSIE") > -1 || userAgent.indexOf("Trident") > -1) {

              fileName = URLEncoder.encode(fileName, "UTF-8");

          } else {

              fileName = new String( fileName.getBytes("UTF-8"), "8859_1");

          }

   

          response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");

          response.setHeader("Content-Transfer-Encoding", "binary");

          

          OutputStream out = response.getOutputStream();

   

          FileInputStream fis = null;

   

          try {

              

              fis = new FileInputStream(file);

              FileCopyUtils.copy(fis, out);

              

          } finally {

              

              if(fis != null) {

                  fis.close();

              }

          }

          

          out.flush();

      }

  }