Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何以字符串形式存储大型二进制数据?_Java - Fatal编程技术网

Java 如何以字符串形式存储大型二进制数据?

Java 如何以字符串形式存储大型二进制数据?,java,Java,可能重复: 我从web服务中获得了responses字符串,它是XML格式的大量二进制字符串。我可以将响应数据存储到对象中。但是我想解析响应并将其存储到本地数据库中。因此,我必须将对象数据转换为字符串。如果我将对象数据转换为字符串。它会发生内存问题 我的回答是这样的 <xmlstring> <oneslip>78364</oneslip> <threeslip>Hello</threeslip> <twoslip>3<

可能重复:

我从web服务中获得了responses字符串,它是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>
试一试


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

如果您想传输输入流的全部内容,甚至不需要查看它,您甚至可以使用将BLOB列直接设置为来自该流的数据


如果您必须将内容转换为字符,因为数据库使用clob而不是blob,那么clob也有类似的方法,您仍然可以避免使用中间字符串。

尝试使用StringBuffer而不是blobString@imrankhan如果可以使用StringBuilder,请不要使用StringBuffer。为什么不使用StringBuilder?首先,为什么字符串不接受大数据?我们有没有试着找出原因?
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();