编码转换统一防止乱码

    public static void main(String[] args) throws UnsupportedEncodingException {
        String origin = new String("元字符".getBytes(StandardCharsets.UTF_8), "iso8859-1");
        String fileName1 = new String(origin.getBytes("iso8859-1"), "utf-8");
        String fileName2 = URLDecoder.decode(URLEncoder.encode(origin, "iso8859-1"), "utf-8");
        String encodeTo = URLEncoder.encode(origin, "utf-8");
        String encode = URLEncoder.encode(origin, "iso8859-1");
        String encodeUtf8 = URLEncoder.encode("元字符", "utf-8");
        String decode = URLDecoder.decode(origin, "iso8859-1");
        System.out.println(encode);
        System.out.println(decode);
        // System.out.println(fileName1.equals(fileName2));
        // 结果为true
    }

posted @ 2024-05-31 15:32  liftsail  阅读(6)  评论(0编辑  收藏  举报