Vert. x Http 를 사용 하여 데 이 터 를 발표 할 때의 인 코딩 과 디 코딩

2805 단어 Vert.x
자바 에서 볼 때 인 코딩 은 String 회전 Stream 이 고 디 코딩 은 Stream 회전 String 입 니 다.
router.get("/" + urlName).handler(event -> event.response().putHeader("content-type", "text/html").end(result));

브 라 우 저 중국어 난 장 판, URLConnection 분석 결 과 를 사용 할 때 기본 디 코딩 을 사용 하면 중국 어 를 인식 합 니 다.
    /**
     *    URL  GET     
     *
     * @param url
     *                 URL
     * @param param
     *                ,        name1=value1&name2=value2    。
     * @return URL             
     */
    public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            //    URL     
            URLConnection connection = realUrl.openConnection();
            //          
            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.connect();
            //          
            Map> map = connection.getHeaderFields();
            //           
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            //    BufferedReader      URL   
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("  GET      !" + e);
            e.printStackTrace();
        }
        //   finally       
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

브 라 우 저의 중국어 난 장 판 을 해결 하기 위해 서 Vert. x 는 인 코딩 을 추가 해 야 하고 인 코딩 방식 은 GBK 여야 합 니 다.
router.get("/" + urlName).handler(event -> event.response().putHeader("content-type", "text/html").end(result, "GBK"));

이 때 URLConnection 을 사용 하여 결 과 를 분석 할 때 기본 디 코딩 을 사용 하면 난해 합 니 다. GBK 디 코딩 방식 을 사용 해 야 합 니 다.
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));

브 라 우 저 에서 중국어 가 어 지 럽 지 않 고 테스트 하기 편리 하도록 보장 합 니 다.자바 코드 를 통 해 중국어 가 어 지 럽 지 않 고 사용 할 수 있 도록 요청 합 니 다.

좋은 웹페이지 즐겨찾기