简体   繁体   中英

Best practices for file system of application data JavaFX

I am trying to build an inventory management system and have recently asked a question about how to be able to store images in a package within my src file. I was told that you should not store images where class files are stored but have not been told what the best practices are for file systems. I have created a new page that allows the user to input all the data about a new part that they are adding to the system and upload an image associated with the part. When they save, everything worked fine until you try to reload the parts database. If you 'refresh' eclipse and then update the database, everything was fine because you could see the image pop into the package when refreshed. (All database info was updated properly as well.

I was told not to store these types of 'new' images with the program files but to create a separate file system to store these types of images. Is there a best practice for these types of file systems? My confusion is when the program gets saved where ever it is going to be saved, I can't have it point to an absolute path because it might not be saved on a C drive or K drive and I wouldn't want an images folder just sitting on the C drive that has all of the parts images for anyone to mess with. Please give me some good resources on how to build these file systems. I would like the images folder 'packaged' with the program when I compile it and package all the files together, I have not been able to find any good information on this, thanks!

To answer this question, probably not in the best way, but works pretty well.

在此处输入图片说明

I ended up making another menuItem and menu that you can see at the top 'Image Management', where it lets the user set the location that they would like to save all the images as well as a location to back up the images. it creates the directory if it is not there or it will save over the images if the directory is already there. This menu will only appear if the user has admin privileges. I would think that this could be set up with an install wizard, but I have no idea how to make one, where it only runs on installation. I am also going to add an autosave feature to save to both locations if a backup location has been set. This is the best way I can think of managing all the parts images, if anyone has some good input, please let me know. I considered a server, but think that is too much for this application and retrieving images every time the tableView populates would take a lot of time. If interested the code I used is:

public class ImageDirectoryController implements 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();

        }
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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