개발

[java] 한글로 된 파일 다운로드시 한글 깨짐현상

지승준 2014. 8. 28. 09:48

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
package download;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLDecoder;
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());
          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


fileDownload.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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();
    
}
cs


FileHelper.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
private static String input_proc( int cnt, int no, String folder_nm, String table_nm ) throws SQLException {
        
        //변수 초기화
        String html = "";
        List<Map<StringObject>> file_list = null;
        String fPath = "";
        
        //파일 불러오기 
        if( no != 0 ){
            
            file_list = list(no, folder_nm, table_nm );
            fPath = "/" + folder_nm + "/" + no + "/";
            
        }
        
        for( int i=0, j=cnt, k=0; i<j; i++ ){            
            
            String file_html = "";
            if( file_list != null && file_list.size() > k ){
                
                //파일 순번이 같다면,
                if( (i+1) == Cvt.Int( file_list.get(k).get("NUM") ) ){
                    
                    String file_nm = Cvt.String( file_list.get(k).get("FILE_NM") );
                    String encode_file_nm = "";
                    
                    try{
                        
                        encode_file_nm = URLEncoder.encode( file_nm, "UTF-8" );
                        
                    }catch(Exception ex){
                        
                    }
                    
                    file_html += "<div style='margin-top: 3px;'><a href=\"javascript:file_download( '" + fPath + "', '" + encode_file_nm + "' );\">" + file_nm + "</a></div>";
                    k++;
                    
                }
                
            }
            
            if( i == 0 ) {
            
                html += "<div>";
                html += "<input type='file' name='file_nm" + (i+1) + "' />";
                html += file_html;
                html += "</div>";
                
            }else{
                
                html += "<div style='margin-top: 5px; padding-top: 5px; border-top: 1px solid #E4E4E4;'>";
                html += "<input type='file' name='file_nm" + (i+1) + "' />";
                html += file_html;
                html += "</div>";
                
            }
            
        }
        
        return html;
        
    }
cs