In my class, the attribute where the image will be saved is defined like this:
@Lob
@Column(name = "NOTA_FISCAL", nullable = false, columnDefinition = "mediumblob")
public byte[] getNOTA_FISCAL() {
return NOTA_FISCAL;
}
public void setNOTA_FISCAL(byte[] NOTA_FISCAL) {
this.NOTA_FISCAL = NOTA_FISCAL;
}
In a project I made using Swing, I used JFileChooser to read an image from my computer to later insert it into the database, we can see that the file is converted to Bytes before inserting the item.
private void selecionarNF(ActionEvent event) {
JFileChooser fc = new JFileChooser();
int res = fc.showOpenDialog(null);
if (res == JFileChooser.APPROVE_OPTION) {
File arquivo = fc.getSelectedFile();
System.out.println("Arquivo: " + arquivo + "");
textCaminhoNF.setText(String.valueOf(arquivo));
try {
byte[] fotonf = new byte[(int) arquivo.length()];
FileInputStream fileInputfotonf = new FileInputStream(arquivo);
fileInputfotonf.read(fotonf);
fileInputfotonf.close();
fotoNF = fotonf;
} catch (Exception ex) {
// System.out.println(ex.printStackTrace().toString());
}
} else {
JOptionPane.showMessageDialog(null, "Você nao selecionou nenhum arquivo.");
}
I'm implementing it with JavaFX, I could even continue using Swing, but I intend to use the Windows file explorer, this way using FileChooser, however I'm having problems capturing the image and passing it to an object of type File, the . getSelectedFile() doesn't exist for FileChooser, I searched a lot on the internet and couldn't find a way to capture this file. What would be the correct way?
Stage s = new Stage();
FileChooser fc = new FileChooser();
textCaminhoNF.setText(fc.showOpenDialog(s).getAbsolutePath());
File arquivo = fc.getSelectedFile(); //his line shows the error, because there is no .getSelectItem
if (fc != null) {
System.out.println("No is null");
try
{
byte[] fotonf = new byte[(int) fc.length()];
FileInputStream fileInputfotonf = new FileInputStream(fc);
fileInputfotonf.read(fotonf);
fileInputfotonf.close();
fotoNF = fotonf;
} catch (Exception ex) {
// System.out.println(ex.printStackTrace().toString());
}
}