mysql保存图片和读取图片

基于字节流和sql的图片存储和读取操作
这种方法只能适合字节数小的照片,大的话mysql会暴

DbUtil
package cn.edu.zzti.photo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @Classname DBUtil
 * @Author jdq8576
 * @Date 2019/4/11 23:05
 **/
public class DBUtil {
    private static final String url = "jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT%2b8&useSSL=false";
    private static final String username = "root";
    private static final String password = "889977";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,username,password);
    }
    public static void closeConnection(Connection connection){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

ImageDemo
package cn.edu.zzti.photo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Classname ImageDemo
 * @Author jdq8576
 * @Date 2019/4/11 23:17
 **/
public class ImageDemo {
    public static void saveImage(){
        String path = "C:\\Users\\jdq8576\\Desktop\\啥都有\\img\\2.jpg";
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = ImageUtil.getFileInputStream(path);
            connection = DBUtil.getConnection();
            String sql = "INSERT into photo(id,name,photo) values(?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,1);
            preparedStatement.setString(2,"DQ");
            preparedStatement.setBinaryStream(3,fileInputStream,fileInputStream.available());
            int res = preparedStatement.executeUpdate();
            if(res>0){
                System.out.println("Success");
            }else{
                System.out.println("Failed");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeConnection(connection);
            if(null != preparedStatement){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void readImage(){
        String path = "F:\\2.jpg";
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from photo where id = 1";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                InputStream inputStream = resultSet.getBinaryStream("photo");
                ImageUtil.saveImage(inputStream,path);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeConnection(connection);
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(preparedStatement!=null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

ImageUtil
package cn.edu.zzti.photo;

import java.io.*;

/**
 * @Classname ImageUtil
 * @Author jdq8576
 * @Date 2019/4/11 23:10
 **/
public class ImageUtil {
    public static FileInputStream getFileInputStream(String path) throws FileNotFoundException {
        return new FileInputStream(new File(path));
    }
    public static void saveImage(InputStream inputStream,String path){
        File file = new File(path);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = inputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes);
            }
            fileOutputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

MyTest
package cn.edu.zzti.photo;

import org.junit.Test;

/**
 * @Classname Test
 * @Author jdq8576
 * @Date 2019/4/11 23:35
 **/
public class MyTest {

    @Test
    public void test(){
        ImageDemo.saveImage();
        ImageDemo.readImage();
    }
}

数据库sql
create table photo
(
	id int auto_increment,
	name varchar(20) null,
	photo blob null,
	constraint photo_pk
		primary key (id)
);

鸣谢 原作者

你可能感兴趣的:(数据库)