2012-03-29 29 views
16

Tôi có tệp xml chứa ký tự arabic. Khi tôi cố gắng phân tích cú pháp tệp, nó phát sinh Ngoại lệ, MalformedByteSequenceException: byte không hợp lệ 2 của UTF 2 byte 8 sequence.I Sử dụng POI DOM để phân tích cú pháp tài liệu.MalformedByteSequenceException: byte không hợp lệ 2 của chuỗi UTF-8 2 byte

The Log là,

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) 

Ngoại lệ duy nhất phát sinh trong các cửa sổ máy, không phát sinh trong Linux Machine.How tôi có thể giải quyết các đề nghị issue.Any nên đáng.

Trả lời

13

Tôi đã giải quyết được sự cố, bằng cách tạo tệp XML sử dụng định dạng UTF8.

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

Sau khi tạo tệp bằng mã trên, vấn đề về mã hóa được giải quyết.Cảm ơn mọi người, hãy nỗ lực ở đây.

+3

Đây là giải pháp mà làm việc cho tôi, nhưng tôi đã phải thực hiện một chút thay đổi: OutputStream os = new FileOutputStream (file); và OutputStreamWriter bufferedWriter = new OutputStreamWriter (os, "UTF8"); – maxivis

0

Tôi nghĩ rằng trình phân tích cú pháp của bạn mong đợi một byte được mã hóa bằng UTF-8 và nhận nó theo mã hóa khác nhau. Kiểm tra mã hóa của tệp.

Một giải pháp có thể có thể chuyển đổi tệp thành UTF-8.

Nếu bạn có một hệ thống Unix, bạn có thể sử dụng công cụ này

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

là hệ điều hành dựa trên nhân vật tài liệu bắt đầu này. Bạn nên sử dụng một số trình xem byte và xóa nó khỏi tài liệu của bạn. Bạn có thể thử sử dụng một cái gì đó như unix2dos để chuyển đổi các ký tự điều khiển.

3

Tất cả những gì chúng tôi có thể nói từ thông báo là tệp không được mã hóa đúng theo UTF-8. Để tìm hiểu lý do tại sao, bạn sẽ cần phải theo dõi lịch sử về cách tạo tệp. Có thể (hoặc có thể không) hữu ích khi nghiên cứu nội dung tệp ở cấp nhị phân để xem mã hóa thực tế là gì. Ví dụ, nó có thể hữu ích để biết liệu toàn bộ tập tin có sai mã hóa hay không hoặc liệu nó có chứa một vài ký tự đi lạc trong mã hóa sai hay không.

7

bạn có thể thêm tham số jvm -Dfile.encoding = utf-8 vào jvm của bạn.

Các vấn đề liên quan