我可以将从NetBeans中截取的屏幕截图保存到MySQL吗?

huangapple go评论94阅读模式
英文:

Can i save a screenshot taken from netbeans to Mysql

问题

我正在进行一个项目,决定将账单的屏幕截图保存到MySQL数据库中。但是用于截取屏幕截图的ImageIcon没有提供文件长度,因此setBinaryStream()方法无法正常工作。我尝试过pstmt.setBlob (1, (BLOB)icon),但仍然无法正常工作。

如果可以的话,您可以直接将截取的屏幕截图保存到数据库中,请告诉我如何操作。
或者我可以在NetBeans中创建并保存自定义账单吗?

  1. try
  2. {
  3. Rectangle screen = new Rectangle (Toolkit.getDefaultToolkit().getScreenSize());
  4. BufferedImage capture = new Robot().createScreenCapture(screen);
  5. ImageIcon icon = new ImageIcon(capture);
  6. int len = icon.getWidth();
  7. ImageIO.write(capture, "jpg", new File("screenshot.jpg"));
  8. try {
  9. Class.forName("com.mysql.jdbc.Driver");
  10. } catch (ClassNotFoundException ex) {
  11. Logger.getLogger(Sign_up.class.getName()).log(Level.SEVERE, null, ex);
  12. }
  13. try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/webapp", "root", "")) {
  14. Statement at = con.createStatement();
  15. String qt = "insert into picture (Pictures) values (?)";
  16. PreparedStatement pstmt = con.prepareStatement(qt);
  17. pstmt.setBinaryStream(icon, ...);
  18. }
  19. }
英文:

I am doing a project in which i decided to save screenshot of the bill to mysql database. 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.

Can you save the taken screenshot directly to database, if so please tell me.
Or can i create and save custom bills in netbeans.

  1. try
  2. {
  3. Rectangle screen = new Rectangle (Toolkit.getDefaultToolkit().getScreenSize());
  4. BufferedImage capture = new Robot().createScreenCapture(screen);
  5. ImageIcon icon = new ImageIcon(capture);
  6. int len = icon.getWidth();
  7. ImageIO.write(capture,"jpg",new File("screenshot.jpg"));
  8. try {
  9. Class.forName("com.mysql.jdbc.Driver");
  10. } catch (ClassNotFoundException ex) {
  11. Logger.getLogger(Sign_up.class.getName()).log(Level.SEVERE, null, ex);
  12. }
  13. try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/webapp","root","")) {
  14. Statement at = con.createStatement();
  15. String qt = "insert into picture (Pictures) values (?)";
  16. PreparedStatement pstmt = con.prepareStatement(qt);
  17. pstmt.setBinaryStream (icon,);
  18. }

答案1

得分: 0

  1. import java.awt.Rectangle;
  2. import java.awt.Robot;
  3. import java.awt.Toolkit;
  4. import java.awt.image.BufferedImage;
  5. import java.io.ByteArrayInputStream;
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.InputStream;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import javax.imageio.ImageIO;
  14. public class ScreenShot {
  15. public static void main(String[] args) throws Exception {
  16. if (args.length != 3) {
  17. // Note: Don't pass passwords on the command line in real life.
  18. System.err.println("Usage: java ScreenShot <dbUrl> <dbUser> <dbPwd>");
  19. return;
  20. }
  21. // Capture the screenshot
  22. Rectangle screen = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
  23. BufferedImage capture = new Robot().createScreenCapture(screen);
  24. // Write it to a ByteArrayOutputStream
  25. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  26. ImageIO.write(capture, "jpg", baos);
  27. // Insert the bytes into the database
  28. Connection connection = DriverManager.getConnection(args[0], args[1], args[2]);
  29. String sql = "insert into picture (Pictures) values (?)";
  30. PreparedStatement ps = connection.prepareStatement(sql);
  31. ps.setBinaryStream (1,new ByteArrayInputStream(baos.toByteArray()));
  32. ps.execute();
  33. // Select the image from the database.
  34. sql = "select Pictures from picture where id = 1";
  35. ps = connection.prepareStatement(sql);
  36. ResultSet rs = ps.executeQuery(sql);
  37. if (rs.next()) {
  38. InputStream input = rs.getBinaryStream(1);
  39. // Write the image to a file (for verification).
  40. FileOutputStream fos = new FileOutputStream("screenshot.jpg");
  41. byte[] buffer = new byte[1024];
  42. while (input.read(buffer) > 0) {
  43. fos.write(buffer);
  44. }
  45. input.close();
  46. fos.close();
  47. }
  48. }
  49. }

Note, this requires a table like below to exist in the default schema connected to.

  1. CREATE TABLE test.picture (
  2. id int AUTO_INCREMENT ,
  3. Pictures MEDIUMBLOB,
  4. PRIMARY KEY (id)
  5. );
英文:

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...

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).

  1. import java.awt.Rectangle;
  2. import java.awt.Robot;
  3. import java.awt.Toolkit;
  4. import java.awt.image.BufferedImage;
  5. import java.io.ByteArrayInputStream;
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.InputStream;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import javax.imageio.ImageIO;
  14. public class ScreenShot {
  15. public static void main(String[] args) throws Exception {
  16. if (args.length != 3) {
  17. // Note: Don&#39;t pass passwords on the command line in real life.
  18. System.err.println(&quot;Usage: java ScreenShot &lt;dbUrl&gt; &lt;dbUser&gt; &lt;dbPwd&gt;&quot;);
  19. return;
  20. }
  21. // Capture the screenshot
  22. Rectangle screen = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
  23. BufferedImage capture = new Robot().createScreenCapture(screen);
  24. // Write it to a ByteArrayOutputStream
  25. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  26. ImageIO.write(capture, &quot;jpg&quot;, baos);
  27. // Insert the bytes into the database
  28. Connection connection = DriverManager.getConnection(args[0], args[1], args[2]);
  29. String sql = &quot;insert into picture (Pictures) values (?)&quot;;
  30. PreparedStatement ps = connection.prepareStatement(sql);
  31. ps.setBinaryStream (1,new ByteArrayInputStream(baos.toByteArray()));
  32. ps.execute();
  33. // Select the image from the database.
  34. sql = &quot;select Pictures from picture where id = 1&quot;;
  35. ps = connection.prepareStatement(sql);
  36. ResultSet rs = ps.executeQuery(sql);
  37. if (rs.next()) {
  38. InputStream input = rs.getBinaryStream(1);
  39. // Write the image to a file (for verification).
  40. FileOutputStream fos = new FileOutputStream(&quot;screenshot.jpg&quot;);
  41. byte[] buffer = new byte[1024];
  42. while (input.read(buffer) &gt; 0) {
  43. fos.write(buffer);
  44. }
  45. input.close();
  46. fos.close();
  47. }
  48. }
  49. }

Note, this requires a table like below to exist in the default schema connected to.

  1. CREATE TABLE test.picture (
  2. id int AUTO_INCREMENT ,
  3. Pictures MEDIUMBLOB,
  4. PRIMARY KEY (id)
  5. );

答案2

得分: 0

或许可以将图像数据直接保存到数据库(不建议),而是将文件保存在某个地方(比如远程文件托管服务)。然后在SQL语句中保存文件的链接。

英文:

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.

huangapple
  • 本文由 发表于 2020年5月29日 21:45:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/62087428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定