servlet和action中获取URL中的汉字(解决URL中汉字为乱码的问题)
2012-11-05 10:50
288 查看
最近在项目中又遇到一个小问题,通过HttpURLConnection来传递汉字时,服务端获取汉字参数时都为乱码,以下分别为在servlet或action中获取URL中的汉字解决办法:
1. 以下代码为 通过HttpURLConnection连接来传递参数,其中,对待汉字的操作需要先进行编码的操作,之后在服务端进行解码操作即可。
[java]
view plaincopyprint?
public
static void main(String[] args)
throws UnsupportedEncodingException {
// String url = "http://61.154.14.46:8080/exter.shtml?serviceType=1011";
String name = java.net.URLEncoder.encode("行子爱上大叔的","UTF-8");
String url = "http://localhost:8081/exter.shtml?serviceType=1023&guid=11&mobile=13696900475&content=123" + name;
// String url = "http://localhost:8080/webtest/servlet/URLTest?name=这个是测试用的" + name;
// String url = "http://localhost:8081/exter.shtml?serviceType=1022&menuId=4481&mobile=15806092760&text_data=linlinlin&imgName=testa.jpg";
// getReturnData1(url);
sendPost(url,null);
}
[java]
view plaincopyprint?
/**
* 通过HTTP协议以POST形式发送指定文件至指定url
* @param url
* @throws IOException
*/
public static
void sendPost(String url,InputStream in) {
HttpURLConnection conn = null;
OutputStreamWriter osw = null;
try {
File file = new File("D:/test2.jpg");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
URL url1 = new URL(url);
conn = (HttpURLConnection)url1.openConnection();
conn.setReadTimeout(10000);
// 缓存的最长时间
conn.setDoInput(true);// 允许输入
conn.setDoOutput(true);// 允许输出
conn.setUseCaches(false);
// 不允许使用缓存
conn.setRequestMethod("POST");
conn.setRequestProperty("Charsert",
"UTF-8");
//conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + java.util.UUID.randomUUID().toString());
//需要传递流时,一定要添加的参数,而且ACTION中通过request.getInputStream获取流的情况下,也必须添加该参数
conn.setRequestProperty("content-type",
"text/html");
OutputStream o = conn.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
int BUFFER_SIZE = 1024;
byte[] buf =
new byte[BUFFER_SIZE];
int size = 0;
try {
while ((size = bis.read(buf)) != -1)
o.write(buf, 0, size);
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
bis.close();
o.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK)
System.out.println( "connect failed!");
} catch (IOException e) {
e.printStackTrace();
}
finally
{
if (osw != null)
try {
osw.close() ;
} catch (IOException e1) {
e1.printStackTrace();
}
if (conn !=
null)
conn.disconnect() ;
}
}
2. servlet端的解析汉字操作: 直接进行编码的转换即可显示为汉字
[java]
view plaincopyprint?
public
void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setContentType("text/html");
request.setCharacterEncoding("UTF-8");
String s = request.getParameter("name");
System.out.println("s22 is " +
new String(s.getBytes("iso-8859-1"),"UTF-8"));
// InputStream in = request.getInputStream();
// if(in != null) {
// System.out.println("流不是空的。");
// this.writeInputStreamToFile(in);
// System.out.println("server time is " + new Date());
// } else {
// System.out.println("流是空的。");
// }
3. 在action中解析汉字的操作: 在action中直接设置下编码格式,直接获取就可。
request.setCharacterEncoding("utf-8");
System.out.println(form.getContent());
1. 以下代码为 通过HttpURLConnection连接来传递参数,其中,对待汉字的操作需要先进行编码的操作,之后在服务端进行解码操作即可。
[java]
view plaincopyprint?
public
static void main(String[] args)
throws UnsupportedEncodingException {
// String url = "http://61.154.14.46:8080/exter.shtml?serviceType=1011";
String name = java.net.URLEncoder.encode("行子爱上大叔的","UTF-8");
String url = "http://localhost:8081/exter.shtml?serviceType=1023&guid=11&mobile=13696900475&content=123" + name;
// String url = "http://localhost:8080/webtest/servlet/URLTest?name=这个是测试用的" + name;
// String url = "http://localhost:8081/exter.shtml?serviceType=1022&menuId=4481&mobile=15806092760&text_data=linlinlin&imgName=testa.jpg";
// getReturnData1(url);
sendPost(url,null);
}
public static void main(String[] args) throws UnsupportedEncodingException { // String url = "http://61.154.14.46:8080/exter.shtml?serviceType=1011"; String name = java.net.URLEncoder.encode("行子爱上大叔的","UTF-8"); String url = "http://localhost:8081/exter.shtml?serviceType=1023&guid=11&mobile=13696900475&content=123" + name; // String url = "http://localhost:8080/webtest/servlet/URLTest?name=这个是测试用的" + name; // String url = "http://localhost:8081/exter.shtml?serviceType=1022&menuId=4481&mobile=15806092760&text_data=linlinlin&imgName=testa.jpg"; // getReturnData1(url); sendPost(url,null); }
[java]
view plaincopyprint?
/**
* 通过HTTP协议以POST形式发送指定文件至指定url
* @param url
* @throws IOException
*/
public static
void sendPost(String url,InputStream in) {
HttpURLConnection conn = null;
OutputStreamWriter osw = null;
try {
File file = new File("D:/test2.jpg");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
URL url1 = new URL(url);
conn = (HttpURLConnection)url1.openConnection();
conn.setReadTimeout(10000);
// 缓存的最长时间
conn.setDoInput(true);// 允许输入
conn.setDoOutput(true);// 允许输出
conn.setUseCaches(false);
// 不允许使用缓存
conn.setRequestMethod("POST");
conn.setRequestProperty("Charsert",
"UTF-8");
//conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + java.util.UUID.randomUUID().toString());
//需要传递流时,一定要添加的参数,而且ACTION中通过request.getInputStream获取流的情况下,也必须添加该参数
conn.setRequestProperty("content-type",
"text/html");
OutputStream o = conn.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
int BUFFER_SIZE = 1024;
byte[] buf =
new byte[BUFFER_SIZE];
int size = 0;
try {
while ((size = bis.read(buf)) != -1)
o.write(buf, 0, size);
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
bis.close();
o.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK)
System.out.println( "connect failed!");
} catch (IOException e) {
e.printStackTrace();
}
finally
{
if (osw != null)
try {
osw.close() ;
} catch (IOException e1) {
e1.printStackTrace();
}
if (conn !=
null)
conn.disconnect() ;
}
}
/** * 通过HTTP协议以POST形式发送指定文件至指定url * @param url * @throws IOException */ public static void sendPost(String url,InputStream in) { HttpURLConnection conn = null; OutputStreamWriter osw = null; try { File file = new File("D:/test2.jpg"); if(!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } URL url1 = new URL(url); conn = (HttpURLConnection)url1.openConnection(); conn.setReadTimeout(10000); // 缓存的最长时间 conn.setDoInput(true);// 允许输入 conn.setDoOutput(true);// 允许输出 conn.setUseCaches(false); // 不允许使用缓存 conn.setRequestMethod("POST"); conn.setRequestProperty("Charsert", "UTF-8"); //conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + java.util.UUID.randomUUID().toString()); //需要传递流时,一定要添加的参数,而且ACTION中通过request.getInputStream获取流的情况下,也必须添加该参数 conn.setRequestProperty("content-type", "text/html"); OutputStream o = conn.getOutputStream(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); int BUFFER_SIZE = 1024; byte[] buf = new byte[BUFFER_SIZE]; int size = 0; try { while ((size = bis.read(buf)) != -1) o.write(buf, 0, size); } catch (IOException e) { e.printStackTrace(); } finally { try { bis.close(); o.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) System.out.println( "connect failed!"); } catch (IOException e) { e.printStackTrace(); } finally { if (osw != null) try { osw.close() ; } catch (IOException e1) { e1.printStackTrace(); } if (conn != null) conn.disconnect() ; } }
2. servlet端的解析汉字操作: 直接进行编码的转换即可显示为汉字
[java]
view plaincopyprint?
public
void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setContentType("text/html");
request.setCharacterEncoding("UTF-8");
String s = request.getParameter("name");
System.out.println("s22 is " +
new String(s.getBytes("iso-8859-1"),"UTF-8"));
// InputStream in = request.getInputStream();
// if(in != null) {
// System.out.println("流不是空的。");
// this.writeInputStreamToFile(in);
// System.out.println("server time is " + new Date());
// } else {
// System.out.println("流是空的。");
// }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // response.setContentType("text/html"); request.setCharacterEncoding("UTF-8"); String s = request.getParameter("name"); System.out.println("s22 is " + new String(s.getBytes("iso-8859-1"),"UTF-8")); // InputStream in = request.getInputStream(); // if(in != null) { // System.out.println("流不是空的。"); // this.writeInputStreamToFile(in); // System.out.println("server time is " + new Date()); // } else { // System.out.println("流是空的。"); // }
3. 在action中解析汉字的操作: 在action中直接设置下编码格式,直接获取就可。
request.setCharacterEncoding("utf-8");
System.out.println(form.getContent());
相关文章推荐
- servlet和action中获取URL中的汉字(解决URL中汉字为乱码的问题)
- servlet和action中获取URL中的汉字(解决URL中汉字为乱码的问题)
- jquery获取URL中参数解决中文乱码问题的两种方法
- asp.net 解决js传递参数时,url汉字乱码的问题
- jquery获取URL中参数解决中文乱码问题的两种方法
- HttpURLConnection 从网页获取内容与乱码问题解决
- 解决乱码问题的最佳解决方式(struts struts2 jsp servlet action等)
- 解决httpurlconnection获取网页数据部分中文乱码问题
- Javascript escape 解决 Jquery Ajax 异步请求 URL中文传参乱码问题 一般处理程序Handler获取参数
- servlet通过URL传参到jsp页面出现乱码问题的解决
- 前台页面使用utf-8编码,url中包含中文参数时后台获取参数乱码问题解决
- PHP采用get获取url汉字出现乱码的解决方法
- 解决url汉字参数乱码问题
- Js获取url中的参数(已解决出现的中文乱码问题)
- PHP采用get获取url汉字出现乱码的解决方法
- 完美解决servlet传数据到绿色版的mySQL的汉字乱码问题
- jquery获取URL中参数,解决中文乱码问题
- 解决js中传值,Action获取是乱码问题
- jquery获取URL中参数,解决中文乱码问题