我们可以使用java从MySql数据库中获取添加的图像吗?
我已将图像添加到 MySql 数据库中。我试图通过使用 java 创建一个新文件来从数据库中获取图像。问题是一切顺利,图像文件已创建,但图像文件不包含图像,它什么都没有,并且新创建的图像文件的大小与原始图像文件的大小不同...... Sql 代码:
CREATE TABLE `pictures` ( `id` int(11) NOT NULL auto_increment, `description` varchar(20) default NULL, `photo` blob, PRIMARY KEY (`id`) );
insert into pictures values('1','pic1','D:\java.jpg');
和 java 代码:
public class ReadBlobPicture
{
static String url = "jdbc:mysql://localhost:3306/imgdatabase";
static String username = "root";
static String password = "tiger";
public static void main(String[] args)
throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT photo FROM pictures where id=1";
Statement stmt = conn.prepareStatement(sql);
ResultSet resultSet = stmt.executeQuery(sql);
Object obj = resultSet;
while (resultSet.next())
{
File image = new File("E:\\java12.jpg");
FileOutputStream fos = new FileOutputStream(image);
System.out.println(resultSet.getString(1));
byte[] buffer = new byte[1];
InputStream is2 = resultSet.getBinaryStream(1);
System.out.println(is2.available());
while (is2.read() > 0)
{
fos.write(buffer);
fos.flush();
}
fos.close();
}
conn.close();
}
}
I have added an image into a MySql database. And I am trying to fetch the image from the database by creating a new file by using java. The problem is that everything goes fine and image file has been created but the image file doesn't contain the image, it has just nothing and the size of the new created image file differs from the original one.....
The Sql Code:
CREATE TABLE `pictures` ( `id` int(11) NOT NULL auto_increment, `description` varchar(20) default NULL, `photo` blob, PRIMARY KEY (`id`) );
insert into pictures values('1','pic1','D:\java.jpg');
And the java code:
public class ReadBlobPicture
{
static String url = "jdbc:mysql://localhost:3306/imgdatabase";
static String username = "root";
static String password = "tiger";
public static void main(String[] args)
throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT photo FROM pictures where id=1";
Statement stmt = conn.prepareStatement(sql);
ResultSet resultSet = stmt.executeQuery(sql);
Object obj = resultSet;
while (resultSet.next())
{
File image = new File("E:\\java12.jpg");
FileOutputStream fos = new FileOutputStream(image);
System.out.println(resultSet.getString(1));
byte[] buffer = new byte[1];
InputStream is2 = resultSet.getBinaryStream(1);
System.out.println(is2.available());
while (is2.read() > 0)
{
fos.write(buffer);
fos.flush();
}
fos.close();
}
conn.close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您从流中读取,但从未将读取的字符保存到缓冲区中...
类似:(
编辑 - 来自另一个答案的评论...)
对于不插入图像的插入语句,请参阅 此链接
You read from the stream, but never save the read character into the buffer...
something like:
(edit - from the comment on another answer...)
For the insert statement not inserting the image see this link
你在写入输出流时遇到问题,应该是这样的:
并且你的图片应该作为 BLOB 保存在数据库中!
PS 你的缓冲区是无用的,因为它的大小为 1,你应该将其设置为至少 512 或 1024 以避免逐字节读取。
希望有帮助。
you have a problem writing to your outputstream, it should be like this:
And your picture should be saved in the database as a BLOB!
P.S. your buffer is useless since it has a size of 1, you should make it at least 512 or 1024 to void reading byte by byte.
Hope it helped.
不会将二进制 jpg 数据插入数据库,而是插入字符串
D:\java.jpg
Does not insert binary jpg data into the database, it inserts the string
D:\java.jpg