簡體   English   中英

使用具有不同字符集的套接字形式mx讀取行-iso-8859-2

[英]Reading line using sockets form mx with different charset- iso-8859-2

我正在嘗試使用Java中的套接字編寫簡單的應用程序,以逐行讀取郵件服務器中的標頭。 問題是有時郵件主題具有不同的字符集(iso-8859-2),並且我無法正確顯示一些特殊字符。使用System.out.println(“ńł”)時顯示特殊字符的正確方法是什么? 我知道我必須使用“ String s.get bytes”,但是無論哪種方式都行不通。 是否有可能正確顯示特殊字符。 也許我做錯了什么(我是java的新手),但是花了將近一個星期的時間檢查了不同的代碼后,我已經很累了。 這是來自示例測試應用程序的簡單代碼(不是原始代碼):

String s = "=?ISO-8859-2?Q?Zesp=F3=B3_Gmaila?=";
byte[] bytes = s.getBytes(Charset.forName("ISO-8859-2"));
String foo = new String(bytes, Charset.forName("UTF-8"));
System.out.println(foo);
//System.out.println(Charset.defaultCharset().name());

輸出:=?ISO-8859-2?Q?Zesp = F3 = B3_Gmaila?=

MX的示例答案:

主題:=?ISO-8859-2?Q?Gmail_jest_inny = 2E_Oto = 2C_co_musisz_o_nim_wiedzie = E6 = 2E?=

先感謝您

亞當

我無法在新評論中完整列出所有內容,因此Esailija可以幫助您解決所有問題:

package javaapplication7;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.QuotedPrintableCodec;

public class JavaApplication7 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)  {

    String s = "=?ISO-8859-2?Q?Zesp=F3=B3_Gmaila?=";
    Pattern p = Pattern.compile("=\\?([a-zA-Z0-9\\-]+)\\?Q\\?"); 
    Matcher m = p.matcher(s);
    if( m.find() ) {
    String encoding = m.group(1);
    String target = s.replaceAll(p.toString(),"");
    QuotedPrintableCodec qpc = new QuotedPrintableCodec(encoding);
    try {
         System.out.println(qpc.decode(target));
         } catch (DecoderException e) {
        e.printStackTrace();
     }
}
    }
}

和錯誤:

run:
org.apache.commons.codec.DecoderException: Invalid quoted-printable encoding
    at      org.apache.commons.codec.net.QuotedPrintableCodec.decodeQuotedPrintable(QuotedPrintableCodec.java:189)
at org.apache.commons.codec.net.QuotedPrintableCodec.decode(QuotedPrintableCodec.java:230)
at org.apache.commons.codec.net.QuotedPrintableCodec.decode(QuotedPrintableCodec.java:279)
at org.apache.commons.codec.net.QuotedPrintableCodec.decode(QuotedPrintableCodec.java:300)
at javaapplication7.JavaApplication7.main(JavaApplication7.java:32)

成功建立(總時間:1秒)

您可以查看James Server(Java中的郵件服務器)的源代碼,您可能會找到答案:

和Mime4J

使用Apache Commons Codec的 QuotedPrintableCodec

String s = "=?ISO-8859-2?Q?Zesp=F3=B3_Gmaila";
Pattern p = Pattern.compile("=\\?([a-zA-Z0-9\\-]+)\\?Q\\?"); //For detecting the encoding
Matcher m = p.matcher(s);
if( m.find() ) {
    String encoding = m.group(1);
    String target = s.replaceAll( p.toString(), ""); //Remove the encoding header
    QuotedPrintableCodec qpc = new QuotedPrintableCodec(encoding);
    try {
        System.out.println(qpc.decode(target));
        // prints "Zespół_Gmaila"
    } catch (DecoderException e) {
        e.printStackTrace();
    }

}

您可能對波蘭文字有疑問。 我得到了簡單的答案。

String s = "=?ISO-8859-2?Q?Zesp=F3=B3_Gmaila?=";
String test=MimeUtility.decodeText(s);

System.out.println(test);

println是,,ZespółGmaila''。 您需要的庫是:

import javax.mail.internet.MimeUtility;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM