IE 11은 한글깨짐 현상이 있어서 제어문에서 추가함
몇년전에 파일다운로드 할 일이 생겨서 브라우저 체크는 어떤분 블로그에서 정보를 얻었는데..
어디였는지 기억이 안남 ㅠㅠ
본인은 스프링과 연동을 해서 쓰고 있는 중이라 바이트로 파일을 쓰지 않고,
스프링내에서 지원하는 copy 유틸을 사용해서 다운로드 처리를 하였음.
jsp 또는 다른 프레임워크를 쓰시는 분들은
FileCopyUtils.copy() <<<< 이부분을 변경해주셔야 할듯...
- 다운로드 유틸코드 -
package com.util; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.util.FileCopyUtils; public class FileUtil { public void filDown(HttpServletRequest request, HttpServletResponse response, String filePath, String realFilNm, String viewFileNm) throws IOException { String dftFilePath = request.getSession().getServletContext().getRealPath("/"); File file = new File(dftFilePath + filePath.substring(1) + realFilNm); if (file.exists() && file.isFile()) { response.setContentType("application/octet-stream; charset=utf-8"); response.setContentLength((int) file.length()); String browser = getBrowser(request); String disposition = getDisposition(realFilNm, browser); response.setHeader("Content-Disposition", disposition); response.setHeader("Content-Transfer-Encoding", "binary"); OutputStream out = response.getOutputStream(); FileInputStream fis = null; fis = new FileInputStream(file); FileCopyUtils.copy(fis, out); if (fis != null) fis.close(); out.flush(); out.close(); } } private String getBrowser(HttpServletRequest request) { String header = request.getHeader("User-Agent"); if (header.indexOf("MSIE") > -1 || header.indexOf("Trident") > -1) return "MSIE"; else if (header.indexOf("Chrome") > -1) return "Chrome"; else if (header.indexOf("Opera") > -1) return "Opera"; return "Firefox"; } private String getDisposition(String filename, String browser) throws UnsupportedEncodingException { String dispositionPrefix = "attachment;filename="; String encodedFilename = null; if (browser.equals("MSIE")) { encodedFilename = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+", "%20"); } else if (browser.equals("Firefox")) { encodedFilename = "\"" + new String(filename.getBytes("UTF-8"), "8859_1") + "\""; } else if (browser.equals("Opera")) { encodedFilename = "\"" + new String(filename.getBytes("UTF-8"), "8859_1") + "\""; } else if (browser.equals("Chrome")) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < filename.length(); i++) { char c = filename.charAt(i); if (c > '~') { sb.append(URLEncoder.encode("" + c, "UTF-8")); } else { sb.append(c); } } encodedFilename = sb.toString(); } return dispositionPrefix + encodedFilename; } }
사용법은 다음과 같다.
FileUtil fileUtil = new FileUtil(); fileUtil.filDown("/upload/dir1/dir2/","실제파일명+확장자","화면에보여줄파일명+확장자");
해당 샘플은 다운로드 파일들이 WebContent 하단에 해당되는 폴더+파일이라고 가정한다.
그리고 1번째 파라미터인 디렉토리 경로는 webcontent/upload/dir1/dir2/ 형식으로 존재한다고 가정하는거다.
만약 경로앞에 슬러시가 빠진다면 fileDown 메서드에서 filePath.substring(1) 을 filePath로 변경하고 적용해주도록 하자
POI 라이브러리를 이용하여 엑셀파일(XLSX) 만들기 - 6만건 이상일 경우 추천 (0) | 2014.07.14 |
---|---|
json-simple 라이브러리를 이용한 ObjecToString 과 StringToObject 코드구현 (2) | 2014.07.04 |
엑셀다운 POI 라이브러리 - 엑셀파일에서 내용 읽기 (0) | 2014.06.10 |
게시물 번호 구하기 샘플소스 (0) | 2014.06.09 |
자바 썸네일 이미지 생성 (0) | 2014.06.09 |