2013-04-04 74 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的目的对它进行编码。