12

Apache POI クラスを使用して、Outlook .MSG ファイルをテキスト ファイルにデコードしようとしています。

printlnのメソッドを除いて、すべてPrintWriter正常に動作します。新しい行は作成されません。

すべての文を次々と直接連結するだけです。以下のコード スニペットの結果は次のとおりです。

「De:textPara:」 iso
「デ:」
「パラ:」

いくつかのマシンでコードを試してみました。ローカルの tomcat インストール (Windows マシン) では動作しますが、Solaris プラットフォーム上の tomcat または Weblogic インストールでは失敗します。エンコーディングアルゴリズムと関係があると思ったのでPrintStream、代わりに を使用しPrintwriter、エンコーディング ISO-8859-1 を示しましたが、運もありませんでした。

何か案が?

    try {
        byte [] msgByte = Base64.decodeBase64(msgBase64);

        InputStream inputMsg = new ByteArrayInputStream(msgByte);
        msg = new MAPIMessage(inputMsg);

        /* 1. Transform MSG to TXT. */
        try {
            txtOut = new PrintWriter(outputMsg);
            try {
                String displayFrom = msg.getDisplayFrom();
                txtOut.println("De: "+displayFrom);
            } catch (ChunkNotFoundException e) {
                _logger.info("Error extrayendo displayFrom: "+e);
            }
            try {
                String displayTo = msg.getDisplayTo();
                txtOut.println("Para: "+displayTo);
            } catch (ChunkNotFoundException e) {
                _logger.info("Error extrayendo displayTo: "+e);
            }

        } finally {
        if(txtOut != null) {
            txtOut.close();}
        else {
            _logger.error("No se ha podido parsear el mensaje.");
        }

        }
4

1 に答える 1

22

Change the following:

txtOut.print("De: "+displayFrom + "\r\n");
txtOut.print("Para: "+displayTo + "\r\n");

This is related to how PrintWriter.println() generates the Line break depending of the Operating System. For unix systems is LF (\n), for Windows is CR+LF (\r\n).

Notice how I added the "\r\n" which means CR+LF and used print() instead of println(). This way the line break generated is not platform dependent.

You can also add the following method to your class to avoid duplicity and just call this custom println() instead of directly calling txtOut.print().

private static final String LINE_SEPARATOR = "\r\n";

public void println(String str) {
    txtOut.print(str + LINE_SEPARATOR);
}

This way you just call println() method.

于 2011-12-12T16:01:08.997 に答える