簡體   English   中英

Java特殊字符替換

[英]Java special chars replace

我有一條文字:“Csuklásirohamgyötörheti是svédeket,annyit emlegetikmostanságismét是svédmodelltMagyarországon。”

在該原始文本中根本沒有換行符。

當我通過電子郵件(帶有gmail)發送此文本時,得到的編碼如下:

Content-Type: text/plain; charset=ISO-8859-2
Content-Transfer-Encoding: quoted-printable

Csukl=E1si roham gy=F6t=F6rheti a sv=E9deket, annyit emlegetik mostans=E1g =
ism=E9t a
sv=E9d modellt Magyarorsz=E1gon. 

在HTML中:

Content-Type: text/html; charset=ISO-8859-2
Content-Transfer-Encoding: quoted-printable


<span class=3D"Apple-style-span" style=3D"font-family: Helvetica, Verdana, = sans-serif; font-size: 15px; ">Csukl=E1si roham gy=F6t=F6rheti a sv=E9deket= , annyit emlegetik mostans=E1g ism=E9t a sv=E9d modellt Magyarorsz=E1gon.

....

當我嘗試將電子郵件正文解析為文本/純文本時,無法擺脫兩個單詞之間的“ mostans = E1g = ism = E9t”中的=符號。 請注意,HTML編碼的消息中缺少相同的字符。 我不知道那個特殊字符可能是什么,但是我需要消除它才能取回原始文本。

我嘗試替換“ \\ n”,但不是那個,如果我在文本中按“ Enter”,則可以將其正確替換為想要的任何字符。 我也嘗試了'\\ r'和'\\ t'。

所以問題是,我想念什么? 這個特殊角色來自哪里? 是因為字符和/或傳輸編碼嗎? 如果是這樣,我該怎么做才能解決問題並取回原始文本。

任何幫助都將受到歡迎。

干杯,巴拉茲

您需要使用MimeUtility。這里是一個示例。

public class Mime {
    public static void main(String[] args) throws MessagingException,
            IOException {
        InputStream stringStream = new FileInputStream("mime");
        InputStream output = MimeUtility.decode(stringStream,
                "quoted-printable");
        System.out.println(convertStreamToString(output));
    }

    public static String convertStreamToString(InputStream is)
            throws IOException {
        /*
         * To convert the InputStream to String we use the Reader.read(char[]
         * buffer) method. We iterate until the Reader return -1 which means
         * there's no more data to read. We use the StringWriter class to
         * produce the string.
         */
        if (is != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is,
                        "ISO8859_1"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            return writer.toString();
        } else {
            return "";
        }
    }
}

文件“ mime”包含編碼的文本:

Csukl=E1si roham gy=F6t=F6rheti a sv=E9deket, annyit emlegetik mostans=E1g =
ism=E9t a
sv=E9d modellt Magyarorsz=E1gon.

更新:

使用番石榴庫:

    InputSupplier<InputStream> supplier = new InputSupplier<InputStream>() {
        @Override
        public InputStream getInput() throws IOException {
            InputStream inStream = new FileInputStream("mime");
            InputStream decodedStream=null;
            try {
                decodedStream = MimeUtility.decode(inStream,
                "quoted-printable");
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return decodedStream;
        }
    };
    InputSupplier<InputStreamReader> result = CharStreams
    .newReaderSupplier(supplier, Charsets.ISO_8859_1);
    String ans = CharStreams.toString(result);
    System.out.println(ans);

傳輸編碼“ quoted-printable”禁止編碼的行超過76個字符的長度。 如果要編碼的文本包含更長的文本行,則必須插入“軟換行符”,用單個“ =”表示編碼行的最后一個字符。 這意味着僅插入下一個換行符以滿足76個字符的限制,並且在解碼傳輸編碼時應刪除下一個換行符。

暫無
暫無

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

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