Green World

반응형

크로스브라우징 파일다운로드 파일명 안깨지게하기







public static void sendExcel(HttpServletRequest request, HttpServletResponse response, XSSFWorkbook


workBook, String fileName) throws Exception {




 String browser = getBrowser(request);


 String finalFileName = fileName + "_" + (new SimpleDateFormat("yyyyMMdd")).format(new Date()) +


".xlsx";





 String dispositionPrefix = "attachment; filename=";


 String encodedFilename = null;




 if (browser.equals("MSIE")) {


  encodedFilename = URLEncoder.encode(finalFileName, "UTF-8").replaceAll("\\+", "%20");


 } else if (browser.equals("Trident")) { // IE11 문자열 깨짐 방지


  encodedFilename = URLEncoder.encode(finalFileName, "UTF-8").replaceAll("\\+", "%20");


 } else if (browser.equals("Firefox")) {


  encodedFilename = "\"" + new String(finalFileName.getBytes("UTF-8"), "8859_1") + "\"";


 } else if (browser.equals("Opera")) {


  encodedFilename = "\"" + new String(finalFileName.getBytes("UTF-8"), "8859_1") + "\"";


 } else if (browser.equals("Chrome")) {


  StringBuffer sb = new StringBuffer();


  for (int i = 0; i < finalFileName.length(); i++) {


  char c = finalFileName.charAt(i);


  if (c > '~') {


      sb.append(URLEncoder.encode("" + c, "UTF-8"));


  } else {


      sb.append(c);


  }


  }








반응형