`
snoopy7713
  • 浏览: 1123360 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

zk下载文件中文乱码解决方案

    博客分类:
  • ZK
zk 
阅读更多
本文来自http://sun4love.iteye.com/blog/1068254 转载请注明
IE,chrome,ff,opera测试通过
Java代码 复制代码 收藏代码
  1. /**
  2. * User: sunflower
  3. * Date: 11-6-2
  4. * Time: 上午10:45
  5. */
  6. public class IndexController extends GenericForwardComposer {
  7. public void onDownfile() {
  8. try {
  9. String charset = "UTF-8";
  10. //服务器文件名
  11. String fileName = "Sip坐席通讯协议.txt";
  12. //编码后文件名
  13. String encodedName = null;
  14. encodedName = URLEncoder.encode(fileName,charset);
  15. //将空格替换为+号
  16. encodedName =encodedName.replace("%20","+");
  17. HttpServletRequest httpRequest =(HttpServletRequest) Executions.getCurrent().getNativeRequest();
  18. //解决ie6 bug 或者是火狐浏览器
  19. if (encodedName.length() > 150
  20. ||Servlets.isGecko(httpRequest)
  21. ||Servlets.isGecko3(httpRequest)) {
  22. encodedName = new String(fileName.getBytes(charset), "ISO8859-1");
  23. }
  24. Filedownload.save(new FileInputStream(ZkUtils.getRealPath("/" + fileName)),
  25. "application/octet-stream", encodedName);
  26. } catch (Exception e) {
  27. }
  28. }
  29. }
我直接修改了org.zkoss.web.servlet.http包底下的Https类的encodeFilename方法也可以解决这个问题.
Java代码 复制代码 收藏代码
  1. private static String encodeFilename(String flnm) {
  2. String filename = Strings.escape(flnm, "\"") ;
  3. [color=red]try {
  4. filename =URLEncoder.encode(filename, "UTF-8");
  5. } catch (UnsupportedEncodingException e) {
  6. e.printStackTrace();
  7. }[/color]
  8. return '"' + filename+ '"';
  9. }
private static String encodeFilename(String flnm) { String filename = Strings.escape(flnm, "\"") ; [color=red]try { filename =URLEncoder.encode(filename, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }[/color] return '"' + filename+ '"'; }

我用的是zk-5.0.5版本.
1 楼 sokoo108 2011-08-05 引用
你这也是一种方式,我也留下我的一种处理方式吧:

Java代码 复制代码 收藏代码
  1. Execution ex = Executions.getCurrent();
  2. HttpServletRequest request = (HttpServletRequest) ex.getNativeRequest();
  3. if (request.getHeader("User-Agent").indexOf("MSIE") != -1) {// ie
  4. docFilename = URLEncoder.encode(docFilename, "UTF-8");
  5. } else {// firefox
  6. docFilename = MimeUtility.encodeText(docFilename, "GBK", "B");
  7. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics