【问题标题】:images saved to desktop overwriting previous image保存到桌面的图像覆盖以前的图像
【发布时间】:2020-03-25 03:12:26
【问题描述】:

我在我的 JSP Web 应用程序中添加了一个功能,用户可以在其中截取他正在查看的页面的屏幕截图,并在我截取另一个屏幕截图时将图像保存到桌面,它会覆盖以前的图像。有什么办法可以增加,所以它会保存为图像,如果我再截图,它会保存为 image1、image2 等。

执行此任务的代码是

try {
        Robot robot = new Robot();
        BufferedImage screenShot = robot
                .createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        String home = System.getProperty("user.home");

        ImageIO.write(screenShot, "JPG", new File(home + "\\Desktop\\i.jpg"));

    } catch (IOException ex) {
        System.out.println(ex.toString());
    }

【问题讨论】:

    标签: java jsp web-applications screenshot


    【解决方案1】:

    为此,您首先需要检查桌面中是否已经存在同名文件:

    boolean check = new File(directory, temp).exists();
    

    如果布尔检查为真,则表示该文件已经存在,同名。然后,您将根据需要更改第二个文件的名称(例如,通过添加一个数字)!

    另外,如果用户想要将文件保存为 image3 并且已经有一个以这种方式调用的文件,您可以这样更改它:

    int num;
    
    try {
       num = Integer.parseInt(String.valueOf(string.substring(string.length() - 1)));
       // then change the name of the file that you are saving for example, from "image4" to "image5"
    } catch (exception e) {
       // This means that the name of the file you are saving does not finish with a number, so you can simply proceed by adding a "1" as the last character of the string name (containing the name of the file the user wants to save)
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 2012-05-25
      • 1970-01-01
      • 2010-12-13
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多