java - 如何从MySQL读取并保存图像

标签 java mysql image blob

我要做的就是将图像插入MySQL表中,然后获取图像数据(保存为blob类型)并将其写入某个文件位置的文件中。

到目前为止,我有以下代码来从表中读取图像数据并将其写入图像文件:

package imageReadWrite;

import dbConnection.testConnection;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.*;

public class BlobImageReadWeite {
    testConnection db = new testConnection();

    public String GetImage() {
        String result = "";
        try {
            db.connect();
            System.out.println("this is for test connection");
            String Banner_chk = "Select * from banner_file where image_name is not null and status='Active' order by upload_dt DESC LIMIT 1";
            ResultSet rs_Banner_chk = db.execSQL(Banner_chk);
            if (rs_Banner_chk.next()) {
                System.out.println(rs_Banner_chk.getString("image_name"));

                Blob blob = rs_Banner_chk.getBlob("image");
                File image = new File("test.png");
                FileOutputStream fos = new FileOutputStream(image);

                byte[] buffer = new byte[1024];

                // Get the binary stream of our BLOB data
                InputStream is = rs_Banner_chk.getBinaryStream("image");
                while (is.read(buffer) > 0) {
                    fos.write(buffer);
                }

                fos.close();
            }

            db.close();
        } catch (Exception e) {
            System.out.println(e);
        }

        return result;
    }

    public static void main(String[] args) {
        BlobImageReadWeite obj1 = new BlobImageReadWeite();
        obj1.GetImage();
    }
}

问题是生成的图像文件无法打开。

我使用以下代码将图像插入数据库

 package promocode;
import java.awt.Toolkit;
import java.io.*;
import java.util.*;
import java.util.Date;
import java.text.*;
import java.sql.*;
import java.util.Vector;
import javax.imageio.*;
import javax.swing.ImageIcon;
import java.awt.*;


import db.connection;



public class ImageReader {
connection db=new connection();
public String fileupload(String flname,String user,String promo_rad)throws      IOException, SQLException,ParseException{

String line=null;

 String line1 = null;
 String fval = "";
 String fval_temp = "";
 String flog,fileflag;
 String sqlst1 = "";
 String sqlst2 = "";
 String res = "false";
 int er_count=0;
 int cor_count=0;
 String error_message="";
 String chk1="";
 String chk2="";
 String chk3="";
 String chk4="";

 System.out.println("the path is"+flname);
 System.out.println(promo_rad);
 String filename1="";
 filename1=flname.substring(flname.lastIndexOf("\\")+1) ;
 System.out.println(filename1);

try {

Image img=Toolkit.getDefaultToolkit().getImage(flname);
ImageIcon icon=new ImageIcon(img);
int height=icon.getIconHeight();
int  width=icon.getIconWidth();
System.out.println("the image height is "+height+" and the image width is "+width);
File file = new File(flname);
FileInputStream fs = new FileInputStream(file);

db.connect();
if(width == 260 && height == 187){

sqlst2="insert into banner_file (image_name,image, FileType,Status, userid,image_flag,upload_dt,height,width) values ('"+filename1+"','"+fs+"','jpg','Active','"+user+"','"+promo_rad+"',now(),'"+height+"','"+width+"')";
System.out.println(sqlst2);
db.updateSQL(sqlst2);
res="true";
}

    }catch(Exception e){
        System.out.println(e);

    }
finally{

db.close();
}

return res;


}
    public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

        ImageReader obj1=new ImageReader();
        String fn = "C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\webapps\\aircel-billpay\\online_images\\Ganesha13.jpg";
        String result = obj1.fileupload(fn,"internalpromo","yes");
        System.out.println(result); 
}

}

最佳答案

尝试交换

InputStream is = rs_Banner_chk.getBinaryStream("image");

InputStream is = blob.getBinaryStream("image");

关于java - 如何从MySQL读取并保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16033056/

相关文章:

mysql - 数据库类模型 - 电影

r - 在R中调整图像大小

java - 为什么我在图像函数中查找图像匹配不完全匹配?

java - 正则表达式分配不正确

java - 如何在 Android 中渲染 PDF

java - 为什么我的 for 循环会出现此错误?

php - 如何更改查询中的 Mysql 变量?

mysql - 在 MySql 中获取具有两列最大值的行

python - 如何在背景 OpenCV 和 Python 上调整和转换蒙版图像的大小

java - 空指针异常