使用 Java 从 MySQL 读取 blob
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14610011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading a blob from MySQL with Java
提问by FredM
I have a problem reading a blob from a MySQL database with Java. I need to write a webservice with jax-rs to deliver an image saved in the database. For transport, it has to be encoded using Base64.
我在使用 Java 从 MySQL 数据库读取 blob 时遇到问题。我需要用 jax-rs 编写一个 web 服务来传递保存在数据库中的图像。对于传输,它必须使用 Base64 进行编码。
This is my code:
这是我的代码:
public String getImage(@PathParam("id") int id) throws SQLException{
System.out.println(id);
String img64str = "null";
Blob image = null;
Connection conn = MySQLConnection.getInstance();
if(conn != null)
{
try{
// Anfrage-Statement erzeugen.
Statement query;
query = conn.createStatement();
// Ergebnistabelle erzeugen und abholen.
String sql = "SELECT bild FROM beitraege where id="+id;
ResultSet result = query.executeQuery(sql);
//Ergebniss zur?ckliefern
while (result.next()) {
System.out.println("while");
image = result.getBlob("bild");
InputStream binaryStream = image.getBinaryStream(1, image.length());
String str= binaryStream.toString();
byte[] bdata=str.getBytes();
byte[] img64 = Base64.encode(bdata);
img64str = new String(img64);
}
}catch (SQLException e) {
e.printStackTrace();
}
}
return img64str;
}
Somehow, it only returns something like this (shown result list decoded):
不知何故,它只返回这样的东西(显示结果列表解码):
java.io.ByteArrayInputStream@cc90a0a
回答by Shaun
java.io.ByteArrayInputStream@cc90a0a
is the result of calling toString()
on the InputStream
. It doesn't actually convert it into a String
- it just uses the default toString()
behavior of returning the identifier of the object within the current environment.
java.io.ByteArrayInputStream@cc90a0a
是调用的结果toString()
上InputStream
。它实际上并没有将其转换为 a String
- 它只是使用toString()
返回当前环境中对象标识符的默认行为。
There are several read()
methods defined on the InputStream
interface to get at the underlying sequence of bytes represented by the stream. You'll need to use one of those to extract the content, rather than trying to convert it into a String
via toString()
.
接口上read()
定义了几种方法InputStream
来获取由流表示的底层字节序列。您需要使用其中之一来提取内容,而不是尝试将其转换为String
via toString()
。
回答by Peanut
The problem lies in the "toString()" call: binaryStream.toString();
BinaryInputStream do not implement toString()
.
Use something like this to read the bytes:
问题在于“toString()”调用:binaryStream.toString();
BinaryInputStream 没有实现toString()
。使用这样的东西来读取字节:
int ch;
//read bytes from ByteArrayInputStream using read method
while((ch = binaryStream.read()) != -1)
{
System.out.print((char)ch);
// store it to an array...
}
回答by jarnbjo
You are returning the result of binaryStream.toString()
, which is something like "java.io.ByteArrayInputStream@cc90a0a".
您正在返回 的结果binaryStream.toString()
,类似于“java.io.ByteArrayInputStream@cc90a0a”。
You have to use the read methods on the InputStream to get the actual content.
您必须使用 InputStream 上的读取方法来获取实际内容。
回答by m0skit0
This line is wrong:
这一行是错误的:
binaryStream.toString();
InputStream
does not override toString()
, thus uses Object.toString()
, which is defined like this. This explains why you're getting that String
.
InputStream
不会覆盖toString()
,因而用途Object.toString()
,其被定义像这样。这解释了为什么你会得到那个String
。
This other questionshows you how to correctly convert from InputStream
to String
.
另一个问题向您展示了如何正确地从 转换InputStream
为String
.
This said you should notconvert binary data (like Base64-encoded image) to string (see NullUserException's comments below).
这就是说,你应该不转换二进制数据(如Base64编码图像)的字符串(见下文NullUserException的评论)。