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]
我想知道是否有任何錯誤在這種方法或任何其他方法來解決這個問題。