0
var name = "Świat";

jQuery.ajax({
  async: false,
  contentType: "application/x-www-form-urlencoded;charset=UTF-8",
  type: 'POST',
  url: "test.do?name="+encodeURIComponent(name),
  noCache:true,
  data:{
  },
  beforeSend: function(xhr) {
    xhr.setRequestHeader("Accept-Charset","UTF-8");
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
...

java中,我将具有如下价值:

String pName = request.getParameter("name");

pName = "Å?wiat";

现在我想使用此名称从数据库中搜索记录,但在 db 中具有“Świat”的值

我怎样才能找到这个或解码这个?

4

2 回答 2

0

使用 Web 服务时一个非常常见的任务是编码/解码特定的 URI 组件,而 Java 中没有直接的 API 用于相同的任务,这变得很困难。下面是我已经使用了一段时间的代码片段。

希望这可以帮助,

public static final String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";

 public static String encodeURIComponent(String input) {
  if(StringUtils.isEmpty(input)) {
   return input;
  }

  int l = input.length();
  StringBuilder o = new StringBuilder(l * 3);
  try {
   for (int i = 0; i < l; i++) {
    String e = input.substring(i, i + 1);
    if (ALLOWED_CHARS.indexOf(e) == -1) {
     byte[] b = e.getBytes("utf-8");
     o.append(getHex(b));
     continue;
    }
    o.append(e);
   }
   return o.toString();
  } catch(UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return input;
 }

 private static String getHex(byte buf[]) {
  StringBuilder o = new StringBuilder(buf.length * 3);
  for (int i = 0; i < buf.length; i++) {
   int n = (int) buf[i] & 0xff;
   o.append("%");
   if (n < 0x10) {
    o.append("0");
   }
   o.append(Long.toString(n, 16).toUpperCase());
  }
  return o.toString();
 }

 public static String decodeURIComponent(String encodedURI) {
  char actualChar;

  StringBuffer buffer = new StringBuffer();

  int bytePattern, sumb = 0;

  for (int i = 0, more = -1; i < encodedURI.length(); i++) {
   actualChar = encodedURI.charAt(i);

   switch (actualChar) {
    case '%': {
     actualChar = encodedURI.charAt(++i);
     int hb = (Character.isDigit(actualChar) ? actualChar - '0'
       : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
     actualChar = encodedURI.charAt(++i);
     int lb = (Character.isDigit(actualChar) ? actualChar - '0'
       : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
     bytePattern = (hb << 4) | lb;
     break;
    }
    case '+': {
     bytePattern = ' ';
     break;
    }
    default: {
     bytePattern = actualChar;
    }
   }

   if ((bytePattern & 0xc0) == 0x80) { // 10xxxxxx
    sumb = (sumb << 6) | (bytePattern & 0x3f);
    if (--more == 0)
     buffer.append((char) sumb);
   } else if ((bytePattern & 0x80) == 0x00) { // 0xxxxxxx
    buffer.append((char) bytePattern);
   } else if ((bytePattern & 0xe0) == 0xc0) { // 110xxxxx
    sumb = bytePattern & 0x1f;
    more = 1;
   } else if ((bytePattern & 0xf0) == 0xe0) { // 1110xxxx
    sumb = bytePattern & 0x0f;
    more = 2;
   } else if ((bytePattern & 0xf8) == 0xf0) { // 11110xxx
    sumb = bytePattern & 0x07;
    more = 3;
   } else if ((bytePattern & 0xfc) == 0xf8) { // 111110xx
    sumb = bytePattern & 0x03;
    more = 4;
   } else { // 1111110x
    sumb = bytePattern & 0x01;
    more = 5;
   }
  }
  return buffer.toString();
 }
于 2013-01-17T08:25:10.587 回答
0

只需要将字符串从 ISO 编码为 UTF,我就会得到记录。喜欢

name = new String(name.trim().getBytes("ISO-8859-1"),"UTF-8");
于 2013-01-17T11:23:33.893 回答