//一次性只能写入或读取确定目录下的单个文件
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
// folderList.add("1");
// while(folderList.size()>0){
// File file=new File((String)folderList.poll());
// File[] files=file.listFiles();
// List
// for(int i=0;i
// 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());
}
}
}