2013-04-04 28 views
0

我想讀取pdf文件的內容並以JSON字符串將其發送到服務器。我用谷歌guava庫來讀取PDF文件的內容到一個字符串。然後我使用拋棄JSON庫來轉義與JSON衝突的必需字符。將pdf文件編碼爲JSON字符串時出錯

String content = Files.toString(new File("C:/Users/Sudhagar/Desktop/GAME.pdf"), Charset.defaultCharset()); 

String escapedContent = org.codehaus.jettison.json.JSONObject.quote(content); 

我將JVM的默認字符集設置爲UTF-8。

得到的JSON字符串創建如下,

String respStr = "{\n"; 
respStr = respStr + "\"mimetype\" : \"" + "text/plain" + "\",\n"; 
respStr = respStr + "\"value\" : " + escapedContent + "\n"; 
respStr = respStr + "}\n"; 
System.out.println(respStr); 
StringEntity entity = new StringEntity(respStr); 
httpput.setEntity(entity); 

當我把這個JSON來我得到一個異常的服務器,

org.codehaus.jackson.JsonParseException: Invalid UTF-8 middle byte 0xfc at [Source: [[email protected]; line: 3, column: 25] 

我想知道是否有任何錯誤在這種方法或任何其他方法來解決這個問題。

回答

3

我相信一個PDF文件應被視爲不透明的二進制數據,就像一個圖像或加密的數據。

不要讀它,彷彿它是一個純文本文件。像其他二進制數據一樣對待它 - 這可能意味着base64-爲了JSON的目的對它進行編碼。

相關問題