应用程序数据 JavaFX 文件系统的最佳实践

Best practices for file system of application data JavaFX

我正在尝试构建一个库存管理系统,并且最近问了一个关于如何能够将图像存储在我的 src 文件中的包中的问题。有人告诉我你不应该在存储 class 文件的地方存储图像,但没有被告知文件系统的最佳实践是什么。我创建了一个新页面,允许用户输入有关他们要添加到系统的新零件的所有数据,并上传与该零件相关的图像。当他们保存时,一切正常,直到您尝试重新加载零件数据库。如果你 'refresh' eclipse 然后更新数据库,一切都很好,因为你可以看到图像在刷新时弹出到包中。 (所有数据库信息也已正确更新。

我被告知不要将这些类型的 'new' 图像与程序文件一起存储,而是创建一个单独的文件系统来存储这些类型的图像。这些类型的文件系统是否有最佳实践?我的困惑是,当程序保存到要保存的位置时,我不能让它指向绝对路径,因为它可能不会保存在 C 驱动器或 K 驱动器上,而且我不想要图像文件夹只是坐在 C 驱动器上,其中包含所有零件图像供任何人使用。请给我一些关于如何构建这些文件系统的好资源。我想在编译程序时将图像文件夹'packaged'与程序打包在一起,并将所有文件打包在一起,但我找不到任何关于这方面的好资料,谢谢!

回答这个问题,可能不是最好的方式,但效果很好。

我最后制作了另一个 menuItem 和菜单,您可以在顶部看到它 'Image Management',它允许用户设置他们想要保存所有图像的位置以及要返回的位置上图像。如果目录不存在,它会创建目录;如果目录已经存在,它将保存图像。此菜单仅在用户具有管理员权限时才会出现。我认为这可以通过安装向导来设置,但我不知道如何制作它,它只在安装时运行。如果设置了备份位置,我还将添加一个自动保存功能以保存到两个位置。这是我能想到的管理所有零件图像的最佳方式,如果有人有好的意见,请告诉我。我考虑过一个服务器,但认为这对于这个应用程序来说太多了,每次 tableView 填充时检索图像都会花费很多时间。如果有兴趣,我使用的代码是:

public class ImageDirectoryController 实现 Initializable{

@FXML private AnchorPane imageDirectory;
@FXML private Label imageDirLbl, backupLbl;
@FXML private Button setLocationButton, backupButton;
@FXML private TextField imageDirPathTxtField;

Stage window;
String image_directory;

@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub

}

public void setImageDirectory(String image_address, String backup_address) {

    imageDirLbl.setText(image_address);
    backupLbl.setText(backup_address);

}

@FXML
public void setLocationButtonClicked () {

    String imagesPath = imageDirPathTxtField.getText() +  "tolmarImages\";
    File files = new File(imagesPath + "asepticImages");
    File generalFiles = new File(imagesPath + "generalImages");
    File facilitiesFiles = new File(imagesPath + "facilitiesImages");

    boolean answer = ConfirmBox.display("Set Image", "Are you sure you want to set this location?");

    if(answer) {

        if (!files.exists()) {
            if (files.mkdirs() && generalFiles.mkdirs() && facilitiesFiles.mkdirs()) {

                JOptionPane.showMessageDialog (null, "New Image directories have been created!", "Image directory created", JOptionPane.INFORMATION_MESSAGE);

            } else {

                JOptionPane.showMessageDialog (null, "Failed to create multiple directories!", "Image directory not created", JOptionPane.INFORMATION_MESSAGE);

            }
        }

        DBConnection dBC = new DBConnection();
        Connection con = dBC.getDBConnection();
        String updateStmt = "UPDATE image_address SET image_address = ? WHERE rowid = ?";

        try {

            PreparedStatement myStmt = con.prepareStatement(updateStmt);
            myStmt.setString(1, imageDirPathTxtField.getText());
            myStmt.setInt(2, 1);
            myStmt.executeUpdate();
            myStmt.close();

            imageDirPathTxtField.clear();

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

@FXML
public void backupButtonClicked () {

    String backupStatus = null;

    if (backupLbl.getText().equals("")&& !imageDirPathTxtField.getText().equals("")) {

        backupStatus = imageDirPathTxtField.getText();

    } else if (!imageDirPathTxtField.getText().equals("")) {

        backupStatus = imageDirPathTxtField.getText();

    } else if (!backupLbl.getText().equals("")){

         backupStatus = backupLbl.getText();

    } else {

        JOptionPane.showMessageDialog(null, "You must create a directory.", "No directory created", JOptionPane.INFORMATION_MESSAGE);
        return;
    }

    boolean answer = ConfirmBox.display("Set Image", "Are you sure you want to backup the images?");

    if(answer) {

        DBConnection dBC = new DBConnection();
        Connection con = dBC.getDBConnection();
        String updateStmt = "UPDATE image_address SET image_address = ? WHERE rowid = 2";

        try {

            PreparedStatement myStmt = con.prepareStatement(updateStmt);
            myStmt.setString(1, backupStatus);
            myStmt.executeUpdate();
            myStmt.close();

            String source = imageDirLbl.getText() + "tolmarImages";
            File srcDir = new File(source);

            String destination = backupStatus + "tolmarImages";
            File destDir = new File(destination);

            try {

                FileUtils.copyDirectory(srcDir, destDir);
                JOptionPane.showMessageDialog(null, "Images copied successfully.", "Images copied", JOptionPane.INFORMATION_MESSAGE);

            } catch (IOException e) {

                e.printStackTrace();

            }

        } catch (SQLException e) {

            e.printStackTrace();

        }
    }
}

}