編碼轉換統一防止亂碼

liftsail發表於2024-05-31
    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
    }

編碼轉換統一防止亂碼

相關文章