Translate

[Firefox] 파일 다운로드 시 한글깨짐 원인 및 수정방법





Java로 파일 다운로드 기능을 만들었는데 Firefox 에서만 한글이 깨져서 다운로드되어 이번에 원인과 수정방법을 알아보게 되었다.


다운로드되는 파일명을 결정하는 헤더인 Content-Disposition 에 대해서 모질라사이트(링크)에서 알아보니 filename* 라는 항목이 있었다.
1004lucifer
아래에 해당 내용의 일부를 긁어왔다.
filename* 라는 항목은 RFC5987에 정의된 인코딩을 사용할때 사용되며 filename 항목보다 우선한다고 한다.





filename
Is followed by a string containing the original name of the file transmitted. The filename is always optional and must not be used blindly by the application: path information should be stripped, and conversion to the server file system rules should be done. This parameter provides mostly indicative information. When used in combination with Content-Disposition: attachment, it is used as the default filename for an eventual "Save As" dialog presented to the user. 1004lucifer
filename*
The parameters "filename" and "filename*" differ only in that "filename*" uses the encoding defined in RFC 5987. When both "filename" and "filename*" are present in a single header field value, "filename*" is preferred over "filename" when both are understood.


따라서 abcd efg 한글제목 `~!@#$%^&*()-_=+[{]}\|;:'",<.>/?.txt 라는 이름의 파일을 다운로드 할 때 아래와 같이 filename* 항목을 추가하게되면 정상적으로 한글파일이 다운로드 되는것을 확인할 수 있다.
(파일명은 인코딩된 값이다.)
1004lucifer


attachment; filename="abcd%20efg%09%ED%95%9C%EA%B8%80%EC%A0%9C%EB%AA%A9%09%60%7E%21%40%23%24%25%5E%26*%28%29-_%3D%2B%5B%7B%5D%7D%5C%7C%3B%3A%27%22%2C%3C.%3E%2F%3F.txt";filename*="UTF-8''abcd%20efg%09%ED%95%9C%EA%B8%80%EC%A0%9C%EB%AA%A9%09%60%7E%21%40%23%24%25%5E%26*%28%29-_%3D%2B%5B%7B%5D%7D%5C%7C%3B%3A%27%22%2C%3C.%3E%2F%3F.txt";


PS.
위의 작업을 반영한 소스는 아래의 링크에서 확인이 가능하다.
링크 - [Java] 파일 다운로드 시 파일명 한글 깨짐 방지 처리



참고
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
https://tools.ietf.org/html/rfc5987



댓글