我正在做一个项目,我决定把账单的截图保存到mysql数据库中,但是用于截图的ImageIcon没有提供文件的长度,所以,setBinaryStream()没有工作,我试过pstmt.setBlob(1,(BLOB))。但是用于截图的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,);
}
你可以用截图的字节创建一个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)
);
也许不要直接将图像数据保存到数据库中(不推荐),而是将文件保存到某个地方(比如远程文件托管服务)。然后在SQL语句中保存文件的链接。