개발

[java] 한글파일 다운로드시 파일명이 깨지는 증상, 띄어쓰기시 다운로드 안되는 증상

지승준 2014. 10. 8. 10:27

fileDownload.js

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
function file_download( fPath, fName ){
        
    if( $( "#down_form" ).length > 0 ){
        
        $("#fPath").val( fPath );
        $("#fName").val( fName );
        
    }else{
    
        var html = "<form action='../../download/download.do' name='down_form' id='down_form' method='post'>";
        html += "<input type='hidden' id='fPath' name='fPath' value='" + fPath + "' />";
        html += "<input type='hidden' id='fName' name='fName' value='" + fName + "' />";
        html += "</form>";
        $( document.body ).append( html );
        
    }
    
    $( "#down_form" ).get(0).submit();
    
}
 
/*
 *http://test.com/down/파일명.확장자 
 * file_download('/down/', '파일명.확장자');
 */
cs


DownloadController.java

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package download;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
 
import comm.SitePath;
 
@Controller
public class DownloadController {
    
    @RequestMapping(value = "/download/download.do")
    public void download(HttpServletRequest request, HttpServletResponse response,    ModelMap modelMap) throws Exception {
        
        String sFileBasePath = SitePath.getFileDir( request );
        String sFilePath = "";
        String sFileName = "";
 
        sFilePath = request.getParameter("fPath");            
        sFileName = request.getParameter("fName");    
        
        File file = new File( sFileBasePath + sFilePath + sFileName );
        
        if( !file.exists() ){
 
            System.out.println(sFileBasePath + sFilePath + sFileName );
 
        }else{
          
          BufferedInputStream fin   = new BufferedInputStream(new FileInputStream(file));
          BufferedOutputStream outs  = new BufferedOutputStream(response.getOutputStream());
 
          String userAgent = request.getHeader("User-Agent");
          if (userAgent.indexOf("Trident") > 0 || userAgent.indexOf("MSIE") > 0 ) {
              
              response.setHeader("Content-Disposition""attachment;filename=" +  URLEncoder.encode(sFileName, "utf-8").replace("+""%20") + ";");
          
          } else if (userAgent.indexOf("Firefox") > 0) {
              
              response.setHeader("Content-Disposition""attachment;filename=\"" + new String(sFileName.getBytes(), "utf-8") + "\";");
         
          } else {
              
              response.setHeader("Content-Disposition""attachment;filename=" + new String(sFileName.getBytes(), "utf-8") + ";");
              
          }
                    
          byte b[]  = null;
          int read   = 0;
         
          try{
 
            if((int)file.length() == 0){
              b   = new byte[1];
              
            }else{
              b   = new byte[(int)file.length()];
            }
 
            if (file.isFile()){
              while ((read = fin.read(b)) != -1){
                outs.write(b,0,read);
              }
            }
           
          }catch(Exception e){
         
             System.out.println("error===>"+e);
         
            }finally{
         
              if (outs !=nulltry { outs.close();outs = null;} catch (Exception ee){}
              if (fin !=nulltry { fin.close();fin = null;} catch (Exception ee){}
         
            }
 
        }
 
    }
        
}
cs