有没有办法为Java的Charset名称添加别名 [英] Is there a way to add aliases for Java's Charset names

查看:189
本文介绍了有没有办法为Java的Charset名称添加别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个异常,埋没在第三方库中,有这样的消息:

I'm getting an exception, buried way inside a 3rd party library, with a message like this:


java.io。 UnsupportedEncodingException:BIG-5

java.io.UnsupportedEncodingException: BIG-5

我认为这是因为Java没有为 java.nio定义此名称.charset.Charset Charset.forName(big5)没问题,但是 Charset.forName(big-5)会抛出例外。 (所有这些名称似乎都不区分大小写。)

I think this happening because Java doesn't define this name for java.nio.charset.Charset. Charset.forName("big5") is fine, but Charset.forName("big-5") throws the exception. (All these names appear to be case insensitive.)

这与utf-8不同,后者有一些别名可以更宽容。例如, Charset.forName(utf8)和 Charset.forName(utf-8)工作很好。

This is different from "utf-8", which has some aliases to be more forgiving. For example, both Charset.forName("utf8") and Charset.forName("utf-8") work fine.

问题:有没有办法添加别名,以便big-5映射到big5?

Question: is there a way to add the alias so that "big-5" maps to "big5"?

推荐答案

您可以尝试 mail.mime.contenttypehandler 系统属性:


在某些情况下,JavaMail无法使用无效的Content-Type标头处理消息。标头可能具有不正确的语法或其他问题。此属性指定在JavaMail使用它之前将用于清除Content-Type标头值的类的名称。该类必须具有带有此签名的方法:public static String cleanContentType(MimePart mp,String contentType)每当JavaMail访问消息的Content-Type标头时,它都会将值传递给此方法并使用返回的值。

In some cases JavaMail is unable to process messages with an invalid Content-Type header. The header may have incorrect syntax or other problems. This property specifies the name of a class that will be used to clean up the Content-Type header value before JavaMail uses it. The class must have a method with this signature: public static String cleanContentType(MimePart mp, String contentType) Whenever JavaMail accesses the Content-Type header of a message, it will pass the value to this method and use the returned value instead.

这方面的一个例子是:

import java.util.Arrays;
import javax.mail.Session;
import javax.mail.internet.ContentType;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimePart;

public class FixEncodingName {

    public static void main(String[] args) throws Exception {
        MimeMessage msg = new MimeMessage((Session) null);
        msg.setText("test", "big-5");
        msg.saveChanges();
        System.out.println(msg.getContentType());
        System.out.println(Arrays.toString(msg.getHeader("Content-Type")));
    }

    public static String cleanContentType(MimePart p, String mimeType) {
        if (mimeType != null) {
            String newContentType = mimeType;
            try {
                ContentType ct = new ContentType(mimeType);
                String cs = ct.getParameter("charset");
                if ("big-5".equalsIgnoreCase(cs)) {
                    ct.setParameter("charset", "big5");
                    newContentType = ct.toString();
                }
            } catch (Exception ignore) {
                newContentType = newContentType.replace("big-5", "big5");
            }

            /*try { //Fix the header in the message.
                p.setContent(p.getContent(), newContentType);
                if (p instanceof Message) {
                    ((Message) p).saveChanges();
                }
            } catch (Exception ignore) {
            }*/
            return newContentType;
        }
        return mimeType;
    }
}

使用 -Dmail运行时.mime.contenttypehandler = FixEncodingName 将输出:

text/plain; charset=big5
[text/plain; charset=big-5]

这篇关于有没有办法为Java的Charset名称添加别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆