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]
我想知道是否有任何错误在这种方法或任何其他方法来解决这个问题。