如何使用java从数据库中检索图像文件

如何使用java从数据库中检索图像文件,java,Java,我可以将图像文件存储到数据库中,但如何将图像作为文件获取并使用java进行预览,就像quikr图像上载一样,将图像转换为字节,保存到db或从db还原为字节 选中以转换为字节 还是你的问题 检查字节到图像操作以下代码将存储图像并将其检索到MySql数据库 将图像存储到数据库中 public static void main(String args[]){ try{ // DB connection Class.forName("com.mysql.jdbc.Dri

我可以将图像文件存储到数据库中,但如何将图像作为文件获取并使用java进行预览,就像quikr图像上载一样,将
图像

转换为
字节
,保存到db或从db还原为字节

选中以转换为字节 还是你的问题


检查字节到图像操作

以下代码将存储图像并将其检索到MySql数据库

将图像存储到数据库中

 public static void main(String args[]){
    try{
    // DB connection
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost/your db name","root","root");

        File file=new File("E:\\sample_image.png");
        FileInputStream fis=new FileInputStream(file);

    //Insert image into db
        PreparedStatement ps=con.prepareStatement("insert into image_table (name,image) values(?,?)"); 
        ps.setString(1,"image 1");
        ps.setBinaryStream(2,fis,(int)file.length());
        ps.executeUpdate();

        ps.close();
        fis.close();
        con.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}
将图像检索到数据库中

 public static void main(String args[]){
    try{
    // DB connection
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost/your db name","root","root");

        File file=new File("E:\\sample_image.png");
        FileInputStream fis=new FileInputStream(file);

    //Insert image into db
        PreparedStatement ps=con.prepareStatement("insert into image_table (name,image) values(?,?)"); 
        ps.setString(1,"image 1");
        ps.setBinaryStream(2,fis,(int)file.length());
        ps.executeUpdate();

        ps.close();
        fis.close();
        con.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}
下面的代码将从数据库中检索图像,并将其保存在位置“E:\sample\u image.png”


你试过什么?你得到了什么?如何将图像保存在数据库中,以base 64格式保存?图像保存为二进制,我可以将其转换为图像文件吗?它是web应用程序,我不想保存在本地内存中,我想还原字节并将其转换为图像文件。但不保存在本地内存如何将字节转换为图像和预览?