工作原因加家里事情多,很久木有更新了,也木有按照预定计划的那样持续介绍resteasy相关的东西,就介绍下项目出现的问题
今天主要介绍下当URI中出现中文时,服务端获取出得Pathparam为乱码的问题:
String urlResouce = null;
try {
urlResouce = "http://10.28.0.203:8080/cloud-storeage-boss/cai:dafile/"+URLEncoder.encode("中文eclipse.zip","utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // create URL
File localFile = new File("Z:\\tools\\eclipse-jee-helios-SR2-win32.zip");
try{
HttpURLConnection urlConnection =
(HttpURLConnection) (new URL(urlResouce)).openConnection();
urlConnection.setRequestProperty("Charset", "UTF-8");
urlConnection.setRequestProperty("auth", "maomaocaicai");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("PUT");
OutputStream urlOutputStream = urlConnection.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(localFile);
IOUtils.copy(fileInputStream, urlOutputStream);
fileInputStream.close();
urlOutputStream.close();
System.out.println(urlConnection.getResponseCode());
}
catch(Exception e)
{
e.printStackTrace();
}
乱码参数的解决方法很简单,就是将相应URI进行UTF-8的编码
这个原理比较简单因为中文时两个字节,而在网络进行传输的时候由于某种原因(具体原因就是编码的问题,请google)会进行单字节的编码,解析出来的中文就变乱码了。----浏览器在传输的时候都会对中文进行编码传输