2012-07-11 91 views
0

可能重复:
OutOfMemoryError : When receiving XML response of 2.3 MB如何在字符串中存储大型二进制数据?

我从网络服务,这是大量的XML格式的二进制字符串的响应串。我可以将响应数据存储到对象中。但我想分析这些响应并将其存储到本地数据库中。所以我必须将对象数据转换为字符串。如果我将对象数据转换为字符串。它发生“内存问题”。

我的回应是这样的

<xmlstring> 
<oneslip>78364</oneslip> 
<threeslip>Hello</threeslip> 
<twoslip>3</twoslip> 
<Binarydata>"Here the large amount of binary data"</Binarydata> 

<oneslip>78364</oneslip> 
<threeslip>Hello</threeslip> 
<twoslip>3</twoslip> 
<Binarydata>"Here the large amount of binary data"</Binarydata> 

.. 
.. 
.. 

最多的50

</xmlstring> 
+0

尝试使用StringBuffer而不是字符串 – 2012-07-11 11:18:49

+0

@imrankhan当您可以使用StringBuilder时,请不要使用StringBuffer。 – 2012-08-13 07:54:57

回答

0

尝试StringBuffer

bufferedReader = new BufferedReader(
        new InputStreamReader(
         response.getEntity().getContent())); 

StringBuffer stringBuffer = new StringBuffer(""); 
String line = ""; 
String LineSeparator = System.getProperty("line.separator"); 

while ((line = bufferedReader.readLine()) != null) { 
    stringBuffer.append(line + LineSeparator); 
} 

bufferedReader.close(); 
+0

为什么不'StringBuilder'?首先,为什么'String'不接受大尺寸数据?我们是否试图找到原因? – overexchange 2017-07-10 18:28:14

0

如果你的数据库表示数据二进制(而不是字符),那么使用中间字符串似乎是错误的做法。您可以使用Blob.setBinaryStream获取将数据写入数据库的流。然后,您可以将可管理块中的内容直接从输入流传输到输出流,而无需缓冲整个内容。

如果您想要传输输入流的全部内容,甚至无需查看它,甚至可以使用CallableStatement.setBlob(String,InputStream)将BLOB列直接设置为来自该流的数据。

如果你必须把东西转换成字符,因为数据库使用clob而不是blob,那么对于clob也存在类似的方法,并且你仍然可以避免使用中间字符串。