본문 바로가기

프로그래밍/초보자를 위한 Java프로그래밍

첨부파일 한글 깨짐 확인 fileupload

반응형

첨부파일 한글깨짐 현상
1. 파일 업로드시 파일 첨부가 안되는 경우
한글깨짐 현상으로, iter에서 FileData 및 첨부파일 정보를 가져오지 못함

fileupload.* 방식

 

<%@ page contentType="text/html; charset=euc-kr" import="java.util.*,
java.io.*, java.net.*, org.apache.commons.fileupload.*"%>

while (iter.hasNext()) {

        item = (FileItem)iter.next();

        if (!item.isFormField()) {  // input type="file" 인경우
            if ("FileData".equals(item.getFieldName())) {   
            // rMate Uploader 컴포넌트의 uploadFieldName 속성 값 - 기본값은 'FileData' 입니다

=> 해결방안 맨밑에 utf-8 추가

    upload.setSizeMax(Long.parseLong(G_attach_maxsize)*1024*1024);  // 파일 업로드 최대 size
    upload.setSizeThreshold(1024*100);                      // 메모리에 저장할 최대 size
    upload.setRepositoryPath(tempPath);                     // 파일 임시 저장소
    upload.setHeaderEncoding("utf-8");

2. 한글명, 공백 파일 다운로드시 에러
   2.1 우선 파일의 full경로를 찍어본다. 한글이 깨지거나 문제가 없는지 확인
   2.2 full경로에 물리적으로 파일이 존재하는 확인
   2.3 AIX나 유닉스에서 공백을 인식못하므로 혹시 윈도우(외부연계)쪽에서 공백있는 파일이 들어왔는지 확인
   2.4 "attachment; filename="+file_name1+"\"" ); 부분에서 file_name에 명시적으로 "a.xls" 와 같이 파일명을 받아서
       다운로드가 제대로 되는지 확인한다.
   2.5 유닉스에서는 물리적 파일명 보단 저장시 파일명에서 문제가 있을 수 있으므로,
       저장시 파일명(원래 파일명)에 공백이 존재하지 않게, ISO8859_1 으로 _가 붙도록 한다.
       아님 공백을 replace ?

     

       String att_file = request.getParameter("file_type");//다운로드 하위 디렉토리 경로및 파일 이름(저장된)
       String file_name = request.getParameter("src_file_name");
       String desc_file_name = request.getParameter("desc_file_name");

       //String file_name1 = URLDecoder.decode(request.getParameter("src_file_name"));
       String file_name1 = new String(file_name.getBytes(),"ISO8859_1");
       String desc_file_name1 = request.getParameter("desc_file_name");

       File file = new File(down_root+"/"+desc_file_name1); //file_name

       in = new FileInputStream(file);
       response.reset() ;
       response.setContentType("application/smnet");
       response.setHeader ("Content-Disposition", "attachment; filename="+file_name1+"\"" );
       response.setHeader ("Content-Length", ""+file.length() );
       os = response.getOutputStream();


* 기타
1. AIX 서버 -
   ftp 설정 : 서버종류 Unix, ASCII
2. 울트라에디터 같은 경우 ftp설정에 AIX 용이 따로 있음

3. 다운로드 버튼을 눌렀을 때 download 파일명이 다르게 나오는 경우

   열기 버튼이 활성화가 안된 경우

   -> 논리적인(보여지는) 파일명이 잘못된 것이다.

   -> 내 컴퓨터에서 지원하지 않는 파일 형식 또는 설치가 안된, 열수 없는 파일

   -> 내부 / aaa도면.xls 라는 파일도 어떤 pc에서는 제대로 다운로드명이 보이고, 어떤 pc에서는 download 라고 파일명이 보임

     -> 파일명이 제대로 보여지는 pc는 2010 오피스가 깔려있는 경우 공백 / 로 파일을 만들 수는 없지만 인식은 하는 듯함

     -> 인식 못하는 pc는 버전이 낮은 오피스가 깔려있는 경우
4. 인코딩 테스트 소스

<%@ page contentType="text/html;charset=euc-kr" %>
<%

String a= request.getParameter("name") ;

//String a= "한글" ;
out.println("Real Date : "+a+"<p>") ;
System.out.println(a) ;

byte[] b= null ;
String c= null ;

// No Encoding GetBytes
b= a.getBytes() ;
c= new String(b) ;
out.println("No Encoding GetBytes, No Encoding String : "+c+"<br>") ;

c= new String(b, "EUC-KR") ;
out.println("No Encoding GetBytes, EUC-KR Encoding String : "+c+"<br>") ;

c= new String(b, "UTF-8") ;
out.println("No Encoding GetBytes, UTF-8 Encoding String : "+c+"<br>") ;

c= new String(b, "ISO-8859-1") ;
out.println("No Encoding GetBytes, ISO-8859-1 Encoding String : "+c+"<br>") ;

out.println("<p>") ;

// Encoding EUC-KR GetBytes
b= a.getBytes("EUC-KR") ;
c= new String(b) ;
out.println("EUC-KR Encoding GetBytes, No Encoding String : "+c+"<br>") ;

c= new String(b, "EUC-KR") ;
out.println("EUC-KR Encoding GetBytes, EUC-KR Encoding String : "+c+"<br>") ;

c= new String(b, "UTF-8") ;
out.println("EUC-KR Encoding GetBytes, UTF-8 Encoding String : "+c+"<br>") ;

c= new String(b, "ISO-8859-1") ;
out.println("EUC-KR Encoding GetBytes, ISO-8859-1 Encoding String : "+c+"<br>") ;

out.println("<p>") ;

// Encoding UTF-8 GetBytes
b= a.getBytes("UTF-8") ;
c= new String(b) ;
out.println("UTF-8 Encoding GetBytes, No Encoding String : "+c+"<br>") ;

c= new String(b, "EUC-KR") ;
out.println("UTF-8 Encoding GetBytes, EUC-KR Encoding String : "+c+"<br>") ;

c= new String(b, "UTF-8") ;
out.println("UTF-8 Encoding GetBytes, UTF-8 Encoding String : "+c+"<br>") ;

c= new String(b, "ISO-8859-1") ;
out.println("UTF-8 Encoding GetBytes, ISO-8859-1 Encoding String : "+c+"<br>") ;

out.println("<p>") ;

// Encoding ISO-8859-1 GetBytes
b= a.getBytes("ISO-8859-1") ;
c= new String(b) ;
out.println("ISO-8859-1 Encoding GetBytes, No Encoding String : "+c+"<br>") ;

c= new String(b, "EUC-KR") ;
out.println("ISO-8859-1 Encoding GetBytes, EUC-KR Encoding String : "+c+"<br>") ;

c= new String(b, "UTF-8") ;
out.println("ISO-8859-1 Encoding GetBytes, UTF-8 Encoding String : "+c+"<br>") ;

c= new String(b, "ISO-8859-1") ;
out.println("ISO-8859-1 Encoding GetBytes, ISO-8859-1 Encoding String : "+c+"<br>") ;

out.println("<p>") ;

%>
반응형