0

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.

Brian
  • 12,145
  • 20
  • 90
  • 153
Curious
  • 111
  • 1
  • 1
  • 10
  • This might help¿? [Retrieve path from input jsp file](https://stackoverflow.com/questions/14338947/how-do-i-get-the-complete-path-of-the-file) – Ramon jansen gomez Aug 01 '18 at 07:35
  • how are you packaging your project jar or war ? and how are you deploying/ running your project? – pvpkiran Aug 01 '18 at 07:45

0 Answers0