How are you?
Within a project that I am doing I need to obtain an image of the computer and save it in a database, for this I used the field input type = file.
To save it I use the following function:
public boolean guardarfoto(int idProducto, String imagen) {
FileInputStream fis = null;
try {
File file = new File("C:\\" + imagen);
fis = new FileInputStream(file);
cpsql.conectar();
connection = ConexionPgSQL.getCn();
PreparedStatement pstm = connection.prepareStatement("UPDATE productos SET imagen_producto = ?, nombre_imagen = ? where id_producto = ?");
pstm.setBinaryStream(1, fis, (int) file.length());
pstm.setString(2, imagen);
pstm.setInt(3, idProducto);
pstm.execute();
System.out.println("Agregando imagen: " + pstm.toString());
pstm.close();
return true;
} catch (FileNotFoundException | SQLException e) {
Logger.getLogger(ControladorProducto.class.getName()).
log(Level.SEVERE, null, e);
}
return false;
}
My problem is that this function only allows me to save an image that is inside the specified directory, in this case C: \, because if in the function I replaced the File file = new File ("C: \\" + image);
by File file = new File (image);
it seems to try to look for the route internally in the project, and not in the directory of the computer, and would therefore like to replace that static directory with a dynamic one so as not to limit the client to having to move the image to the specified route so that Do not mark error when saving.
For this, I am working on a Web project using NetBeans 8.0.2, JSPs and Servlets.