把图片以二进制写入mysql或DB2数据库,再读出来。

//把图片以二进制写入数据库
//一次性只能写入或读取确定目录下的单个文件
package write_read;

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

public class PutImg {
public void putimg() {
try {

String url = "jdbc:mysql://localhost/img?useUnicode=true&characterEncoding=gb2312";
Class.forName("com.mysql.jdbc.Driver");
String username="root";
String password="root";
// String url = "jdbc:db2://localhost:50000/SJPT";
// Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
// String username="DB_SJPT";
// String password="sjpt";
Connection conn = DriverManager.getConnection(url,username,password);
System.out.println("测试连接成功");
Statement stmt = conn.createStatement();
//stmt.execute("insert into imgt (id) values (5)");

PreparedStatement pstmt = null;
String sql = "";

// LinkedList<String> folderList = new LinkedList<String>();
// folderList.add("1");
// while(folderList.size()>0){
// File file=new File((String)folderList.poll());
// File[] files=file.listFiles();
// List<File> fileList=new ArrayList<File>();
// for(int i=0;i<files.length;i++){
// if(files[i].isDirectory())
// folderList.add(files[i].getPath());
// else{
// fileList.add(files[i]);
// InputStream photoStream = new FileInputStream(fileList.get(i));
// sql = "INSERT INTO imgtable (img) VALUES (?)";
//
// pstmt = conn.prepareStatement(sql);
// pstmt.setBinaryStream(i, photoStream, (int) fileList.size());
// pstmt.executeUpdate();
// if(pstmt.executeUpdate()==1){
// System.out.println("图片写入成功!");
// }else{
// System.out.println("图片写入失败!");
// }
// }
// }
for(Flie f:fileList){
f.getAbsoluteFile();

}
// }
File files = new File("d:\\blog.jpg");
InputStream photoStream = new FileInputStream(files);
//sql = " UPDATE imgt SET img = ? ";
sql = "INSERT INTO imgtable (img) VALUES (?)";

pstmt = conn.prepareStatement(sql);
pstmt.setBinaryStream(1, photoStream, (int) files.length());
pstmt.executeUpdate();

if(pstmt.executeUpdate()==1){
System.out.println("图片写入成功!");
}else{
System.out.println("图片写入失败!");
}
stmt.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]){
PutImg pi=new PutImg();
pi.putimg();
}
}


//从数据库读取图片
package write_read;

import java.io.*;
import java.sql.*;
public class GetImg {

// private static final String URL="jdbc:db2://localhost:50000/SJPT";
// String username="DB_SJPT";
// String password="sjpt";
private static final String URL= "jdbc:mysql://localhost/img?useUnicode=true&characterEncoding=gb2312";
String username="root";
String password="root";
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private File file = null;

public void blobRead(String outfile, int picID) throws Exception {
FileOutputStream fos = null;
InputStream is = null;
byte[] Buffer = new byte[4096];
try {
// Class.forName("com.ibm.db2.jcc.DB2Driver");
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL,username,password);
System.out.println("测试连接成功");
pstmt = conn.prepareStatement("select img from imgtable where id=?");
pstmt.setInt(1, picID); // 传入要取的图片的ID
rs = pstmt.executeQuery();

System.out.println(rs);
rs.next();


file = new File(outfile);
if (!file.exists()) {
file.createNewFile(); // 如果文件不存在,则创建
}
fos = new FileOutputStream(file);
is = rs.getBinaryStream("img");
System.out.println("1000000"+is);
int size = 0;

while ((size = is.read(Buffer)) != -1) {

// System.out.println(size);
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) {
try {
GetImg gi=new GetImg();
gi.blobRead("E:/getimgs/liyanli.jpg", 5);
System.out.println(gi);
if(gi!=null){
System.out.println("图片读取成功");
}else{
System.out.println("图片读取失败!");
}
} catch (Exception e) {
System.out.println("[Main func error: ]" + e.getMessage());
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值