標籤:set isp gen puts tco comm 除了 erro xls
/** * 區分ie 和其他瀏覽器的下載檔案亂碼問題 * @param request * @param fileName * @return */ public String getFileName(HttpServletRequest req,String fileName){ String userAgent = req.getHeader("user-agent"); userAgent = userAgent ==null?"":userAgent.toLowerCase(); String name = fileName; try {
//針對IE或者以IE為核心的瀏覽器: if(userAgent.contains("msie") ||userAgent.contains("trident")){ name = URLEncoder.encode(name, "UTF-8"); }else{ name = new String(name.getBytes(), "iso-8859-1"); } } catch (Exception e) { throw new SysException(ERRORConstants.COMMON_SYSTEM_ERROR, e); } return name; }
網上很多例子是用 msie 和like Gecko 來做區分,說ie11去除了msie;
親自測試 Google瀏覽器也帶like Gecko,ie11的 msie沒有移除,並且帶有trident標記
結論是 不用用like Gecko來做區分,最後
public void exportExcel(HttpServletRequest req,HttpServletResponse response) { try{ response.setContentType("application/octet-stream"); String excelName = "檔案名稱"; response.addHeader("Content-Disposition", "attachment;filename="+getFileName(req,excelName)+".xls"); OutputStream out = response.getOutputStream(); testService.export(out); }catch(Exception e){// } }
Java 解決IE瀏覽器下載檔案,檔案名稱出現亂碼問題