情况是这样的:
http://db.test.net:7777/DB_Server/policy/add是别人提供的接口,他提供了把字段存入数据库的功能。存入数据库的中文转为gbk不乱码。
我通过浏览器输入地址http://db.test.net:7777/DB_Server/policy/add?selfRe=%C4%E3%BA%C3(%C4%E3%BA%C3是“你好”转为GBK的编码),然后回车。
可以看见在数据库表中的存入selfRe字段的值为“你好”,没有乱码.但是我在实际中,我需要通过流的方式去请求http://db.test.net:7777/DB_Server/policy/add?selfRe=%C4%E3%BA%C3这个路径。结果去乱码了。弄了2天了,大家帮帮忙。流的方式的方法这样的:
public static String getContent(String urls)
{
StringBuffer sb=new StringBuffer();
Pattern p = Pattern.compile("\\?");
String [] s=p.split(urls);
BufferedReader reader=null;
try {
URL url = new URL(s[0]);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(s[1].getBytes());
reader = new BufferedReader(new InputStreamReader(
(conn.getInputStream()),"gb2312"));
String line=reader.readLine();
while(line!=null)
{
sb.append(line);
line=reader.readLine();
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}finally{
try {
if(reader!=null)
reader.close();
} catch (IOException e) {
}
}
return sb.toString();
}然后我在main中执行getContent(http://db.test.net:7777/DB_Server/policy/add?selfRe=%C4%E3%BA%C3)。
可以看见记录在数据库也添加了,但是selfRe的值却乱码了。我的结论是通过流请求乱码了,在地址栏请求却没有乱码。大家帮帮忙,看下是什么原因?怎么解决啊
http://db.test.net:7777/DB_Server/policy/add是别人提供的接口,他提供了把字段存入数据库的功能。存入数据库的中文转为gbk不乱码。
我通过浏览器输入地址http://db.test.net:7777/DB_Server/policy/add?selfRe=%C4%E3%BA%C3(%C4%E3%BA%C3是“你好”转为GBK的编码),然后回车。
可以看见在数据库表中的存入selfRe字段的值为“你好”,没有乱码.但是我在实际中,我需要通过流的方式去请求http://db.test.net:7777/DB_Server/policy/add?selfRe=%C4%E3%BA%C3这个路径。结果去乱码了。弄了2天了,大家帮帮忙。流的方式的方法这样的:
public static String getContent(String urls)
{
StringBuffer sb=new StringBuffer();
Pattern p = Pattern.compile("\\?");
String [] s=p.split(urls);
BufferedReader reader=null;
try {
URL url = new URL(s[0]);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(s[1].getBytes());
reader = new BufferedReader(new InputStreamReader(
(conn.getInputStream()),"gb2312"));
String line=reader.readLine();
while(line!=null)
{
sb.append(line);
line=reader.readLine();
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}finally{
try {
if(reader!=null)
reader.close();
} catch (IOException e) {
}
}
return sb.toString();
}然后我在main中执行getContent(http://db.test.net:7777/DB_Server/policy/add?selfRe=%C4%E3%BA%C3)。
可以看见记录在数据库也添加了,但是selfRe的值却乱码了。我的结论是通过流请求乱码了,在地址栏请求却没有乱码。大家帮帮忙,看下是什么原因?怎么解决啊
就是这个方法 public static String encode(String s,
String enc)
throws UnsupportedEncodingException
java.net.URLEncoder 转码
java.net.URLDecoder 解码
这个就相当于selfRe=java.net.URLEncoder("你好","gbk")啊。
两边的编码方式是一样的啊。
存入是数据库是gbk格式的中文。我在浏览器上请求可以存进没有乱码啊。我还需要哪里转码?
你自己测试一下 你的方法都没有执行到 return sb.toString(); 这行
{
StringBuffer sb=new StringBuffer();
Pattern p = Pattern.compile("\\?");
String [] s=p.split(urls);
BufferedReader reader=null;
try {
URL url = new URL(s[0]);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(s[1].getBytes());
reader = new BufferedReader(new InputStreamReader(
(conn.getInputStream()),"gbk"));
String line=reader.readLine();
while(line!=null)
{
sb.append(line);
line=reader.readLine();
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}finally{
try {
if(reader!=null)
reader.close();
} catch (IOException e) {
}
}
return sb.toString();
}