获取输入文件的路径

时间:2018-08-01 07:26:42

标签: java file http servlets

你好吗?

在我正在执行的项目中,我需要获取计算机的图像并将其保存在数据库中,为此,我使用了字段输入类型= file。

要保存它,我使用以下功能:

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;
    }

我的问题是,此函数仅允许我保存指定目录内的图像,在本例中为C:\,因为如果在函数中我将File file = new File ("C: \\" + image);替换为File file = new File (image);,似乎试图在项目内部而不是在计算机目录中查找路由,因此希望将静态目录替换为动态目录,以免将客户端限制为必须将映像移动到指定的路由,以便保存时不标记错误。

为此,我正在使用NetBeans 8.0.2,JSP和Servlet进行Web项目。

0 个答案:

没有答案