• get方式中文乱码问题


    <a target="_blank" href="ftpFileAction!downloadFile.action?filename=测试.xml">点击下载</a>

    如果当前页面编码是utf-8.

    这里是main.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

    以及head头

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    并且main.jsp本身的文件编码是utf-8.

    如果是post方式,Servlet后台设置request.setCharacterEncoding('utf-8')后一定得到正确中文。

    但对于get方式,get方式提交参数若包含中文,

    在我本机测试,ie下提交的使用iso-8859-1转码gbk可得到正常中文。

    filename=new String(filename.getBytes("ISO-8859-1"),"gbk");

    在firefox下提交的参数在Servlet中使用iso-8859-1转码utf-8可得到正常中文。

    filename=new String(filename.getBytes("ISO-8859-1"),"utf-8");

    一定要转码的原因是对于get方式提交的参数,tomcat默认不会使用request.setCharacterEncoding的编码方式转码,而是使用ISO-8859-1。

    要保证各浏览器兼容性,

    可以在get方式提交参数时,把参数进行转码。

    encodeURI(适合编码整个URI)和encodeURIComponent(适合编码URI的一部分参数)

    js脚本:

    filename=encodeURIComponent('测试.xml');

    '<a target="_blank" href="ftpFileAction!downloadFile.action?filename='+filename+'">点击下载</a>'

    注明:

    encodeURI, encodeURIComponent 是 UTF-8 的
    escape 是  Unicode 的

    escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

    encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

    encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

  • 相关阅读:
    简化窗口的控制和异常处理的复杂性
    集合运算 字典排序 按值 按键
    单条insert
    Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '=
    反黑
    curl HTTP_USER_AGENT
    location.replace
    提升600百万数据的处理速度 禁止图片下载
    https://security.stackexchange.com/questions/68405/what-is-tmunblock-cgi-and-can-it-be-exploited-by-shellshock-linux-apache-w
    逻辑分离
  • 原文地址:https://www.cnblogs.com/tazi/p/4346340.html
Copyright © 2020-2023  润新知