我正在做一个项目,在这个项目中,我决定将帐单的屏幕快照保存到mysql数据库。但是用于截屏的ImageIcon不能提供文件的长度,因此setBinaryStream()无法正常工作,我尝试了pstmt.setBlob(1,(BLOB)icon),仍然无法正常工作。
能否将拍摄的屏幕截图直接保存到数据库中,如果可以,请告诉我。 或者我可以在netbeans中创建和保存自定义账单。
try
{
Rectangle screen = new Rectangle (Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screen);
ImageIcon icon = new ImageIcon(capture);
int len = icon.getWidth();
ImageIO.write(capture,"jpg",new File("screenshot.jpg"));
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Sign_up.class.getName()).log(Level.SEVERE, null, ex);
}
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/webapp","root","")) {
Statement at = con.createStatement();
String qt = "insert into picture (Pictures) values (?)";
PreparedStatement pstmt = con.prepareStatement(qt);
pstmt.setBinaryStream (icon,);
}
答案 0 :(得分:0)
您可以使用屏幕快照的字节创建一个ByteArrayInputStream,然后使用setBinaryStream(int,InputStream)这样将其传递给准备好的语句。
ps.setBinaryStream (1,new ByteArrayInputStream(baos.toByteArray()));
这是一个完整的程序,它带有屏幕截图,将其写入BLOB,然后从BLOB中读取,然后将其写入文件(用于验证)。
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.imageio.ImageIO;
public class ScreenShot {
public static void main(String[] args) throws Exception {
if (args.length != 3) {
// Note: Don't pass passwords on the command line in real life.
System.err.println("Usage: java ScreenShot <dbUrl> <dbUser> <dbPwd>");
return;
}
// Capture the screenshot
Rectangle screen = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screen);
// Write it to a ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(capture, "jpg", baos);
// Insert the bytes into the database
Connection connection = DriverManager.getConnection(args[0], args[1], args[2]);
String sql = "insert into picture (Pictures) values (?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setBinaryStream (1,new ByteArrayInputStream(baos.toByteArray()));
ps.execute();
// Select the image from the database.
sql = "select Pictures from picture where id = 1";
ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery(sql);
if (rs.next()) {
InputStream input = rs.getBinaryStream(1);
// Write the image to a file (for verification).
FileOutputStream fos = new FileOutputStream("screenshot.jpg");
byte[] buffer = new byte[1024];
while (input.read(buffer) > 0) {
fos.write(buffer);
}
input.close();
fos.close();
}
}
}
请注意,这要求在与之连接的默认架构中存在如下表。
CREATE TABLE test.picture (
id int AUTO_INCREMENT ,
Pictures MEDIUMBLOB,
PRIMARY KEY (id)
);
答案 1 :(得分:0)
也许不是将图像数据直接保存到数据库(不推荐),而是将文件保存在某个地方(例如远程文件托管服务)。然后在SQL语句中保存文件的链接。