Chinaunix首页 | 论坛 | 博客
  • 博客访问: 529669
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2012-04-05 10:31:46



 TestDownload.zip  


DownloadFileNameEncoder.java

点击(此处)折叠或打开

  1. /*
  2.  * @(#)DownloadFileNameEncoderImpl.java
  3.  *
  4.  * Copyright @ Hangzhou Shengren Software Tech. Co., Ltd.
  5.  */
  6. package me.test;

  7. import java.io.UnsupportedEncodingException;
  8. import java.net.URLEncoder;
  9. import java.util.Arrays;
  10. import java.util.List;

  11. import nl.bitwalker.useragentutils.UserAgent;

  12. import org.apache.commons.codec.EncoderException;
  13. import org.apache.commons.codec.net.BCodec;

  14. /**
  15.  * 此类实现了DownloadFileNameEncoder接口,用户对文件下载时的文件名进行编码。 由于各个浏览器对文件下载时文件名解析的方式不一样,
  16.  * 因此需要扩展,以适应不同浏览器在下载时不会出现乱码。
  17.  * IE时,需要先将文件名转为UTF-8,然后再x-www-form-url编码,Firefox则使用RFC2047规定的方式。
  18.  *
  19.  * 参考:
  20.  * http://java-xp.iteye.com/blog/903048
  21.  *
  22.  *
  23.  * 类库:
  24.  * user-agent-utils
  25.  *
  26.  *
  27.  * @author btpka3@163.com
  28.  */
  29. public class DownloadFileNameEncoder {
  30.     private final BCodec bCodec = new BCodec();

  31.     /**
  32.      * 按照浏览器信息不同,对文件名进行编码,默认情况下使用IE的编码方式。
  33.      */
  34.     public String encode(String fileName, String userAgent) {
  35.         if (fileName == null) {
  36.             return "";
  37.         }
  38.         String safeFileName = getSafeFileName(fileName);

  39.         UserAgent agent = UserAgent.parseUserAgentString(userAgent);
  40.         switch (agent.getBrowser().getGroup()) {
  41.         case IE:
  42.             return encodeURLEncoder(safeFileName);
  43.         case OPERA:
  44.             return encoedeRFC2231(safeFileName);
  45.         case SAFARI:
  46.             return encodedISO(safeFileName);
  47.         case FIREFOX:
  48.             return encodeBase64(safeFileName);
  49.         default:
  50.             return encodeURLEncoder(safeFileName);
  51.         }
  52.     }

  53.     // Content-Disposition: attachment;
  54.     // filename="struts2.0%E4%B8%AD%E6%96%87%E6%95%99%E7%A8%8B.chm"
  55.     protected String encodeURLEncoder(String safeFileName) {
  56.         try {
  57.             String result = URLEncoder.encode(safeFileName, "UTF-8");
  58.             result = result.replace("+", "%20");
  59.             return result;
  60.         } catch (UnsupportedEncodingException e) {
  61.             return safeFileName;
  62.         }
  63.     }

  64.     // Content-Disposition: attachment;
  65.     // filename="=?UTF8?B?c3RydXRzMi4w5Lit5paH5pWZ56iLLmNobQ==?="
  66.     protected String encodeBase64(String safeFileName) {
  67.         try {
  68.             return bCodec.encode(safeFileName);
  69.         } catch (EncoderException e) {
  70.             return safeFileName;
  71.         }
  72.     }

  73.     // Content-Disposition: attachment; filename*=UTF-8''%E5%9B%9E%E6%89%A7.msg
  74.     protected String encoedeRFC2231(String safeFileName) {
  75.         try {
  76.             String result = "Content-Disposition: attachment; filename*=UTF-8''"
  77.                     + URLEncoder.encode(safeFileName, "UTF8");
  78.             return result;
  79.         } catch (UnsupportedEncodingException e) {
  80.             return safeFileName;
  81.         }
  82.     }

  83.     // Content-Disposition: attachment;filename="测试.txt"
  84.     protected String encodedISO(String safeFileName) {
  85.         try {
  86.             String result = "Content-Disposition: attachment; filename="
  87.                     + new String(safeFileName.getBytes("UTF-8"), "ISO8859-1");
  88.             return result;
  89.         } catch (UnsupportedEncodingException e) {
  90.             return safeFileName;
  91.         }
  92.     }

  93.     /**
  94.      * 将特殊字符替换成下划线
  95.      *
  96.      * @param fileName
  97.      * 可能含有特殊字符的文件名
  98.      * @return 替换后的文件名
  99.      */
  100.     protected static String getSafeFileName(String fileName) {
  101.         // / \ : * ? " < > |
  102.         List<Character> reservedChars = Arrays.asList('\\', '/', ':', '*', '?',
  103.                 '"', '<', '>', '|');
  104.         char[] charArr = fileName.toCharArray();
  105.         for (int i = 0; i < charArr.length; i++) {
  106.             if (reservedChars.contains(charArr[i])) {
  107.                 charArr[i] = ' ';
  108.             }
  109.         }
  110.         return new String(charArr);
  111.     }
  112. }


阅读(1679) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~