web-dev-qa-db-ja.com

MalformedByteSequenceException:2バイトUTF-8シーケンスの無効なバイト2

アラビア語の文字を含むxmlファイルがあります。ファイルを解析しようとすると、Exception、MalformedByteSequenceException:Invalid byte 2 of 2-byte UTF-8 sequence.I POI DOMを使用してドキュメントを解析します。

ログは、

2012-03-19 11:30:00,433 [ERROR] (com.infomindz.remitglobe.bll.remittance.BlackListBean) - Error 

com.Sun.org.Apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence.

    at com.Sun.org.Apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unknown Source)

    at com.Sun.org.Apache.xerces.internal.impl.io.UTF8Reader.read(Unknown Source)

    at com.Sun.org.Apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)

    at com.Sun.org.Apache.xerces.internal.impl.XMLEntityScanner.skipChar(Unknown Source)

    at com.Sun.org.Apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)

    at com.Sun.org.Apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)

    at com.Sun.org.Apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)

    at com.Sun.org.Apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)

    at com.Sun.org.Apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)

    at com.Sun.org.Apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)

    at com.Sun.org.Apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)

    at com.Sun.org.Apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)

    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)

    at com.infomindz.remitglobe.bll.remittance.BlackListBean.updateGeneralBlackListDetail(Unknown Source)

    at com.infomindz.remitglobe.bll.remittance.schedulers.BlackListUpdateScheduler.executeInternal(Unknown Source)

    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.Java:86)

    at org.quartz.core.JobRunShell.run(JobRunShell.Java:216)

    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.Java:549)

例外はWindows Machineでのみ発生し、Linux Machineでは発生しません。どのように問題を解決できますか。

UTF8形式を使用してXMLファイルを作成することにより、問題を解決しました。

OutputStreamWriter bufferedWriter = new OutputStreamWriter(filePath +
                        System.getProperty("file.separator") + fileName), "UTF8");

上記のコードを使用してファイルを作成すると、エンコードの問題は解決されます。すべての人に感謝します。ここに努力してください。

jvmパラメータを追加できます -Dfile.encoding = utf-8 jvmに。

9
Hsin

メッセージからわかることは、ファイルがUTF-8で適切にエンコードされていないことだけです。理由を解明するには、ファイルが作成された履歴を追跡する必要があります。実際のエンコーディングが何であるかを確認するために、バイナリレベルでファイルの内容を調べると役立つ場合があります(またはそうでない場合があります)。たとえば、ファイル全体が間違ったエンコーディングになっているかどうか、または間違ったエンコーディングにいくつかの浮遊文字が含まれているかどうかを知ることが役立つ場合があります。

3
Michael Kay

非常に簡単なソリューション:

File file = new File("c:\\file-utf.xml");
InputStream inputStream= new FileInputStream(file);
Reader reader = new InputStreamReader(inputStream,"UTF-8");

InputSource is = new InputSource(reader);
// is.setEncoding("UTF-8"); -> This line causes error! Content is not allowed in prolog

saxParser.parse(is, handler);

参照: http://www.mkyong.com/Java/sax-error-malformedbytesequenceexception-invalid-byte-1-of-1-byte-utf-8-sequence/

2
Raaam

あなたのパーサーはUTF-8でエンコードされたバイトを期待し、異なるエンコードでそれを受け取ると思います。ファイルのエンコードを確認してください。

考えられる解決策は、ファイルをUTF-8に変換することです。

UNIXシステムがある場合は、このツールを使用できます

iconv -f original_charset -t utf-8 your_file > new_file
0
user219882

これは、OSベースのドキュメント開始文字です。バイトビューアーを使用して、ドキュメントから削除する必要があります。 nix2dos のようなものを使用して、制御文字を変換することができます。

0
Alex Stybaev