Java+MySql图片数据保存与读取的具体实例

创建表:

create table img_table(
img_id int not null  auto_increment  primary key,
img_name varchar(10) comment'名字',
img_data mediumblob):
图片流存取工具:

package com.lijianrong.Mysql.cn;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class imgUtil {
    private static FileInputStream fis=null;
    //获得图片的输入流并返回
    public static FileInputStream getImageByte(String str){
    	try {
			fis=new FileInputStream(new File(str));
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
		return fis;
    	
    }
    //根据inputstream将图片保存
    public static void readBlob(InputStream inputStream, String path) {
    	 try {
			FileOutputStream fileOutputStream = new FileOutputStream(path);
			byte[]  buffer=new byte[1024];
			int len=0;
			while((len=inputStream.read(buffer))!=-1){
				fileOutputStream.write(buffer,0,len);
			}
			 inputStream.close();
	         fileOutputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
将图片保存到数据库中相应的表格。

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ImageInsert {

	public static void main(String[] args) {
		String str="jdbc:mysql://localhost:3306/test";
		String name="root";
		String password="768696";
		InputStream inputstream=null;
		
		
		try {
			inputstream=imgUtil.getImageByte("C:\\Users\\lijianrong\\Desktop\\1.jpg");
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn=DriverManager.getConnection(str, name, password);
			PreparedStatement pst=conn.prepareStatement("insert into img_table values(?,?,?)");
			pst.setInt(1, 001);
			pst.setString(2, "李建荣");
			pst.setBinaryStream(3,inputstream,inputstream.available());
			pst.executeUpdate();
			System.out.println("图片插入成功");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

}
从数据库中读取并保存图片

package com.lijianrong.Mysql.cn;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Statement;

public class ImageLoad {

	public static void main(String[] args) {
		String str="jdbc:mysql://localhost:3306/test";
		String name="root";
		String password="768696";
		InputStream inputstream=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn=DriverManager.getConnection(str, name, password);
			java.sql.Statement pst=conn.createStatement();
			ResultSet result=pst.executeQuery("select p.img_data from img_table p");
			result.next();
			InputStream inputstream1=result.getBinaryStream("img_data");
			imgUtil.readBlob(inputstream1,"E:\\pig.jpg");
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}


展开阅读全文

没有更多推荐了,返回首页