예제 #1
0
  /**
   * 设置让浏览器弹出下载对话框的Header.
   *
   * @param fileName 下载后的文件名.
   */
  public static void setFileDownloadHeader(
      HttpServletRequest request, HttpServletResponse response, String fileName) {
    // 中文文件名支持
    String encodedfileName = null;
    // 替换空格,否则firefox下有空格文件名会被截断,其他浏览器会将空格替换成+号
    encodedfileName = fileName.trim().replaceAll(" ", "_");
    String agent = request.getHeader("User-Agent");
    boolean isMSIE = (agent != null && agent.toUpperCase().indexOf("MSIE") != -1);
    if (isMSIE) {
      encodedfileName = Encodes.urlEncode(fileName);
    } else {
      encodedfileName = new String(fileName.getBytes(), Charsets.ISO_8859_1);
    }

    response.setHeader(
        HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedfileName + "\"");
  }
예제 #2
0
 /** 客户端对Http Basic验证的 Header进行编码. */
 public static String encodeHttpBasic(String userName, String password) {
   String encode = userName + ":" + password;
   return "Basic " + Encodes.encodeBase64(encode.getBytes());
 }