package org.pic;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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 sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class SavePic {
public static Connection conn = null;
public static PreparedStatement pStmt = null;
/**
* 数据库中加入图片等文件
*/
public static void InsertPic() {
String sql = "insert into TEACHER(ID,NAME,PHOTO,BASE64PHOTO) values('1',?,?,?)";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@//192.168.1.183:1521/orcl",
"LWW", "lww");
pStmt = conn.prepareStatement(sql);
pStmt.setString(1, "Lily");// 设置字段NAME值
// 数据库插入图片/word/excel等
File file = new File("src/pic.jpg");
FileInputStream fis = new FileInputStream(file);
//创建一个和文件大小一样的缓冲区
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
pStmt.setBytes(2, buffer);//内容设置到字段PHOTO中
System.out.println("未加密图片长度:"+fis.available());
String base64 = new BASE64Encoder().encode(buffer);
System.out.println("加密后图片长度:" +base64.length());
pStmt.setBytes(3, base64.getBytes());//内容设置到字段BASE64PHOTO中
int n = pStmt.executeUpdate();
System.out.println(n + "条记录插入");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException| IOException e) {
e.printStackTrace();
} finally {
try {
pStmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 从数据库中取出图片等文件
*/
public static void getPic() {
String sql = "select ID,NAME,PHOTO,BASE64PHOTO from TEACHER where ID=?";
try {
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@//192.168.1.183:1521/orcl",
"LWW", "lww");
pStmt = conn.prepareStatement(sql);
pStmt.setString(1, "1");
ResultSet rs = pStmt.executeQuery();
if (rs.next()) {
System.out.println("ssssssssss");
//获取图片字段
// FileOutputStream fos = new FileOutputStream(new File("abc.jpg"));
// InputStream is = rs.getBinaryStream("photo");
//
// byte[] buffer = new byte[4 * 1024];
// int length = 0;
// while ((length = is.read(buffer)) != -1) {
// fos.write(buffer, 0, length);
// }
//获取图片加密字段内容,并进行base64解密
FileOutputStream fos = new FileOutputStream(new File("abcbase64.jpg"));
byte[] basebt = rs.getBytes("base64photo");
System.out.println("加密:" +basebt.length);
byte[] by = new BASE64Decoder().decodeBuffer(new String(basebt));
System.out.println("解密:" +by.length);
fos.write(by, 0, by.length);
fos.flush();
fos.close();
// is.close();
}
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
pStmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
InsertPic();//把图片保存到数据库中
getPic();//获取字段并生成图片
}
}