【问题标题】:How to read and save image from MySQL如何从 MySQL 读取和保存图像
【发布时间】:2013-04-17 18:56:08
【问题描述】:

我要做的是在 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); 
}

}

【问题讨论】:

  • 不确定这是否是一个问题,但是当读取的字节数小于缓冲区大小时,您会将零写入文件。我建议使用ByteBuffer 和FileChannel,但您也可以通过记录读取的字节并使用其他write 方法来解决问题。
  • 嗨,你对你的博客对象做了什么,我认为你没有把从数据库中提取的对象写入你的新文件对象,即博客对象
  • 嗨 Brijesh,我正在读取从数据库中获取的输入流中的数据并将其写入文件中。

标签: java mysql image blob


【解决方案1】:

尝试交换

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

与

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

【讨论】:

    【解决方案2】:

    is.read(buffer)返回的值表示读取了多少字节,可能不是buffer的全部长度。您将忽略该返回值,并且在每次循环迭代中写出 buffer 的全部内容。如果您检查文件的大小,您可能会发现它大于数据库中二进制数据的大小。

    考虑使用 java.nio.file 包代替 File 和 FileOutputStream:

    Path image = Paths.get("test.png");
    Files.copy(rs_Banner_chk.getBinaryStream("image"), image);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      相关资源
      最近更新 更多