Search code examples
javamysqljdbcprocessingfileinputstream

Can I use FileInputStream without giving it a path right at the start


I am trying to upload a file to a database (a .mp4 file to be specific). But when I run my code the file path is not yet chosen because, in the program a window pops up where you can select a file you want to upload. Is there a way to achieve this?

The problem I am having with FileInputStream is that it asks for a file path at the start of my program but the path is still uncertain at that point.

Greetings Pygesux

This is where I try to INSERT into the db

public void draw() {
    open.draw();
    openText.draw();
    if (video != null) {
        upload.draw();
        uploadText.draw();
    }
}
public void mouseClick() {
    if (open.mouseOverMe()) {
        selectInput("Select a file to process:", "fileSelected");
    } else if (upload.mouseOverMe()) {
        uploadFile();
    }
}
public void fileSelect(File selection) {
    video = selection;
}
public void uploadFile() {
    try {
        con = database.getConnect();
        java.sql.PreparedStatement statement = con.prepareStatement("INSERT INTO filmpje (filmpje) VALUES (?)");
        FileInputStream input = new FileInputStream(video);
        statement.setBlob(1, input);
        statement.executeUpdate();
    }
    catch (SQLException e) {
        e.printStackTrace();
    }
}

Solution

  • Why you do not create FileInputStream after the path is chosen?

    Make a method, that take path, and then creates the FileInputStream, and do other job. Call this method after you know your path, after user eneter it, and accept by pressing button/enter (it is your app logic).