简体   繁体   English

我可以将从 .netbeans 截取的屏幕截图保存到 Mysql

[英]Can i save a screenshot taken from netbeans to Mysql

I am doing a project in which i decided to save screenshot of the bill to mysql database.我正在做一个项目,我决定将账单的屏幕截图保存到 mysql 数据库。 But the ImageIcon used to take the screenshot does not provide the length of file so, the setBinaryStream() isn't working, i have tried pstmt.setBlob (1, (BLOB)icon), still not working.但是用于截屏的 ImageIcon 不提供文件的长度,所以 setBinaryStream() 不工作,我试过 pstmt.setBlob (1, (BLOB)icon),仍然不工作。

Can you save the taken screenshot directly to database, if so please tell me.你能不能把截取的截图直接保存到数据库中,如果可以请告诉我。 Or can i create and save custom bills in.netbeans.或者我可以在 .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,);

}

You can create a ByteArrayInputStream using the bytes of the screenshot, and then pass it to the prepared statement using setBinaryStream(int,InputStream) like this...您可以使用屏幕截图的字节创建一个 ByteArrayInputStream,然后像这样使用 setBinaryStream(int,InputStream) 将其传递给准备好的语句...

ps.setBinaryStream (1,new ByteArrayInputStream(baos.toByteArray()));

Here is a full program that takes a screenshot, writes it to a BLOB, and then reads it from the BLOB, and writes it to a file (for verification).这是一个完整的程序,它截取屏幕截图,将其写入 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();
        }
    }

}

Note, this requires a table like below to exist in the default schema connected to.请注意,这需要像下面这样的表存在于连接到的默认模式中。

CREATE TABLE test.picture (
    id int AUTO_INCREMENT ,
    Pictures MEDIUMBLOB,
PRIMARY KEY (id)
);

Maybe instead of saving the image data directly to the database (not recommended) save the file somewhere (like a remote file hosting service).也许不是将图像数据直接保存到数据库(不推荐),而是将文件保存在某个地方(如远程文件托管服务)。 Then in the SQL statement save the link of the file.然后在SQL语句中保存文件的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM