【发布时间】:2014-01-06 23:14:22
【问题描述】:
我在内存中有一个图像(类型:java.awt.Image),我想使用 jdk 1.7 将其转换为 Blob(类型:java.sql.Blob)。
我在这个主题上找到的所有内容都使用流和文件。当然我不需要在转换之前将此图像保存到文件中??
这里不多展示,下面是一个例子:
导入 java.sql.Blob; 导入 java.awt.Image;
public GenericResponseType savePhoto(Image image)
{
Connection conn = ds.getConnection();
<< some DB code and assembly of PreparedStatement >>
Blob blob = conn.createBlob();
<< here's where the magic needs to happen I need to get the image parameter to blob >>
// I've tried the following but doesn't quite work as it wants a RenderedImage
// OutputStream os = blob.setBinaryStream(1);
// ImageIO.write(parameters.getPhoto().getImage(), "jpg", os);
pstmt.setBlob(4, blob);
}
更多细节(尽管我怀疑它很重要)是上面的内容是使用来自 WSDL 的 Web 服务/JAX-WS 生成的,并且使用 MTOM 声明了一个操作。所以它会生成一个签名,其中一个 Image 作为变量传递。
【问题讨论】:
-
否,但您需要先将其放入 BufferedImage