关于URLEncoder.encode的用法

公司项目需要访问jira平台,需要对传输数据进行转码,否则乱码导致查询出错。

一、前端代码

 //获取系统和相应系统的领域负责人
  function getSystem(){
  	$.ajax({
        url : '${pageContext.request.contextPath}/IT00015Controller/doGet.action',
        type : 'get',
        async : false,
        data : {
        	   url: 'http://***.***.com/rest/insight/1.0/iql/objects?objectSchemaId=21&iql=objectTypeId=266'
        },
        dataType : 'json',
        success : function (result) {
        	systemManager = result;
        }
      });
  }

这里将"服务名称"中文字符转换成了

%E7%B3%BB%E7%BB%9F%E5%B7%A5%E5%85%B7

二、Controller层

 @RequestMapping("/doGet")
    @ResponseBody
    public List<Map<String,Object>> doGet(String url) {
        Session session = SecurityUtils.getSubject().getSession();
        try {
            String objectType = URLEncoder.encode("服务名称", "UTF-8");
            URL urlStr = new URL(url + objectType + "&resultPerPage=500");
            List<Map<String, Object>> result = it00015Service.doGet(urlStr,"s-servicedesk:BenDan11");
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

Service层

 public List<Map<String,Object>> doGet(URL urlStr, String token) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;// 返回结果字符串
        List<Map<String,Object>> maps = new ArrayList<>();
        try {
            // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
            connection = (HttpURLConnection) urlStr.openConnection();
            connection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestProperty("Authorization","Basic " + new Base64Encoder().encode(token.getBytes()));
            //connection.setRequestProperty("Authorization","Basic " + new Base64Encoder().encode("60055859:169533#A".getBytes()));
            // 设置连接方式:get
            connection.setRequestMethod("GET");
            // 设置连接主机服务器的超时时间:15000毫秒
            connection.setConnectTimeout(15000);
            // 设置读取远程返回的数据时间:60000毫秒
            connection.setReadTimeout(600000);
            // 发送请求
            connection.connect();
            // 通过connection连接,获取输入流
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                // 封装输入流is,并指定字符集
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                // 存放数据
                StringBuilder sbf = new StringBuilder();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
                Map<String,Object> map = JSON.parseObject(result);

                JSONArray jsonArray = new JSONArray(map.get("objectEntries").toString());
                for(int i = 0;i<jsonArray.length();i++){
                    Map<String,Object> map2 = new HashMap<>();
                    map2.put("name",jsonArray.getJSONObject(i).get("name").toString());
                    String str = jsonArray.getJSONObject(i).get("attributes").toString();
                    JSONArray jsonArray1 = new JSONArray(str);
                    for(int j = 0;j <jsonArray1.length();j++ ){
                        //生产环境
                        if(("2345").equals(jsonArray1.getJSONObject(j).get("objectTypeAttributeId").toString())){
                            String str1 = jsonArray1.getJSONObject(j).get("objectAttributeValues").toString();
                            if("[]".equals(str1)){
                                map2.put("owner","null");
                            }else{
                                JSONArray jsonArray2 = new JSONArray(str1);
                                map2.put("owner",jsonArray2.getJSONObject(0).get("displayValue").toString());
                            }
                            break;
                        }
                    }
                    maps.add(map2);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            connection.disconnect();// 关闭远程连接
        }
        return maps;
    }

你可能感兴趣的:(Java,java)