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