数据库读取、保存图片

java 向数据库中保存图片


Part1 : 保存图片到数据库中

import java.io.*;
import java.sql.*;

public class PutImg {

public void putimg(String filename) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydefault","root","skychen");
PreparedStatement pstmt = null;
String sql = "";
File file = new File(filename);

InputStream photoStream = new FileInputStream(file);
//sql = " UPDATE imgt SET img = ? ";

sql = "INSERT INTO imgtable (id,img) VALUES (1,?)";
//insert into images(PicNum,Content,Image) values(1,?,?)

pstmt = con.prepareStatement(sql);
pstmt.setBinaryStream(1,photoStream,(int)file.length());
//需获取文件输出流及其大小
pstmt.execute();
pstmt.close();
con.close();
} catch (Exception e) {
System.err.println("Error");
e.printStackTrace();
}
}
public static void main(String[] args){
if (!args[0].equals(null))
{
PutImg pi=new PutImg();
pi.putimg(args[0]);
System.out.println("Upload file success");
}
else{
System.out.println(args[0]);
System.out.println("Warnning: Please type your wanted filename!");
}
}
}

Part2 : 保存图片到文件

import java.io.*;
import java.sql.*;


class GetImg {

private String URL = "jdbc:mysql://localhost:3306/mydefault?user=root&password=skychen";
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private File file = null;

public void blobRead(String outfilename, int picID) throws Exception {
FileOutputStream fos = null;
InputStream is = null;
byte[] Buffer = new byte[4096];
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(URL);
pstmt = conn.prepareStatement("select img from imgtable where id=?");
pstmt.setInt(1, picID); // 传入要取的图片的ID
rs = pstmt.executeQuery();
rs.next();
file = new File(outfilename);
if (!file.exists()) {
file.createNewFile(); // 如果文件不存在,则创建
}
fos = new FileOutputStream(file); //得到文件输出流
is = rs.getBinaryStream("img"); //从数据库中得到文件二进制流
int size = 0;

while ((size = is.read(Buffer)) != -1) { //读到buffer 中缓存
//System.out.println(size); //打印buffer大小
fos.write(Buffer, 0, size); //写入到文件输出流中
}
} catch (Exception e) {
System.out.println( e.getMessage());
} finally {
// 关闭用到的资源
fos.close();
rs.close();
pstmt.close();
conn.close();
}
}
public static void main(String[] args) {
if (!args[0].equals(null))
{
try {
GetImg gi=new GetImg();
gi.blobRead(args[0], 1);
} catch (Exception e) {
System.out.println("[Main func error: ]" + e.getMessage());
}
}
else{
System.out.println("Warnning: Please type your store path and filename!");
}
}
}


JAVA从数据库读取图片数据并保存到本地的方法


//从数据表tablepicture中读字段picture

Sql="Select picture From tablepicture";
Rs = Stmt.executeQuery(Sql);
while (Rs.next())
{
java.io.InputStream long_out = Rs.getBinaryStream("Picture");
if (long_out != null)
{
byte[] long_buf=new byte[8192];
int len=0;
java.io.FileOutputStream long_file = new java.io.FileOutputStream("E:/tomcatx/webapps/ROOT/upfile/instrumentimages/"+Rs.getString("pictureid")+".jpg");
while((len = long_out.read(long_buf))>0)
long_file.write(long_buf,0,len);
long_file.close();
long_out.close();
}
}
Rs.close();

你可能感兴趣的:(Java收集)