Java

[JAVA] 백엔드 Response 결과로 alert() 호출시 한글 깨짐 처리 방법?

파일 다운로드 기능을 구현중인데, 파일이 존재하지 않는 경우에 대한 처리로 alert() 스크립트를 생성하여 처리해주고 있다. 하지만 한글깨짐현상이 발생하고 있다.


사용중인 코드는 다음과 같다.

프론트엔드 개발쪽에서는 window.open()함수를 사용하여 새창 띄우기를 하고 있다.

      const link = `/test/fileDownload.do?file_path=${filePath}$file_name=${encodeURI(fileName)}`;
      window.open(link, '_blank');

 

[백엔드코드]

@RequestMapping(value = {"/fileDownload.do", "/test/fileDownload.do"})
public FileDownService fileDownload(HttpServletRequest request, ModelMap model) throws IOException 
{
    ....생략.....
    return new FileDownService();
}

//FileDownService.class
public class FileDownService extends AbstractView
{
	......생략............
	private void fileNotFound(HttpServletResponse res)
	{
		res.setCharacterEncoding("UTF-8");
		res.setContentType("text/html; Charset=UTF-8");
		
		try(PrintWriter out = new PrintWriter(res.getOutputStream());)
		{
			out.println("<script language="javascript">alert('요청된 파일을 찾을 수 없습니다.');window.close();</script>");			
		}
		catch(Exception ex)
		{
			log.info(ex.toString());
			log.info(ex.getMessage());
		}
	}
}

HttpServletResponse에 문자열 인코딩으로 UTF-8로 처리하고 있음에도 여전히 동일하게 한글이 깨진다.

euc-kr로 변경해도 동일하게 한글은 깨진다.

 

위와 같이 처리했을 때 한글이 깨지는 경우는 드물다.

한글을 제거할까?ㅋㅋㅋ

 

Leave a Reply

error: Content is protected !!