日期:2014-05-16 浏览次数:20551 次
?
注:
转载注明原文地址:?http://thetopofqingshan.iteye.com/blog/1566499
DatabasesConnect 类:http://thetopofqingshan.iteye.com/blog/1504004
建议:使用在数据库中储存文件路径与文件名
?
建表sql:
?
CREATE TABLE `file` ( `id` INT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'id标识', `image` LONGBLOB NULL COMMENT '图片', `name` VARCHAR(50) NULL DEFAULT NULL COMMENT '图片名称', `createTime` DATETIME NULL DEFAULT NULL COMMENT '创建时间', `updateTime` DATETIME NULL DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`) ) COMMENT='储存文件' COLLATE='utf8_general_ci' ENGINE=InnoDB;?
用于mysql数据库中存取文件:
package com.qingshan.jdbc;
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.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * <pre>
 * 用于mysql数据库中存取文件
 * </pre>
 * <hr Color="green" ></hr> 
 * 2012 admin Group 版权所有
 * <hr Color="green" ></hr> 
 * @author  thetopofadmin
 * @version 1.0.0
 * @since   JDK 1.5
 * @date    2012-6-23
 */
public class FileFecthDB {
	private static Connection c  = null;
	static{
		DatabasesConnect.name="root";
		DatabasesConnect.password="admin";
		DatabasesConnect.databaseName="admin";
		try {
			c  = DatabasesConnect.getConnection(DatabasesConnect.MYSQL);//得到数据连接
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * <pre>
	 * 用于插入文件
	 * </pre>
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 * @throws IOException
	 *
	 */
	public static void insert() throws ClassNotFoundException, SQLException, IOException{
		PreparedStatement ps =  c.prepareStatement("insert into file(image,name, createTime, updateTime) values(?,?,now(),now())");
		File file = new File("C:/Users/Administrator/Pictures/【IThome.com】【桌面壁纸】这天空,我想飞/0.jpg");
		if(!file.exists()){
			throw new FileNotFoundException(file.getName().toString());
		}
		InputStream is =  new FileInputStream(file);
		ps.setBinaryStream(1, is);
		ps.setString(2,  file.getName().toString());
		ps.execute();
		is.close();
		ps.close();  
        c.close(); 
	}
	
	/**
	 * <pre>
	 * 用于取出文件
	 * </pre>
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 * @throws IOException
	 *
	 */
	public static void select() throws ClassNotFoundException, SQLException, IOException{
		Statement s = c.createStatement();
		ResultSet rs =  s.executeQuery("select *from file");
		while(rs.next()){
			InputStream is  = rs.getBinaryStream("image");//取出文件后
			File file = new File("A:/"+rs.getString("name"));//储存文件的路径
			OutputStream os =  new FileOutputStream(file);
			byte b[] = new byte[1024*20];
			while(is.read(b) != -1){
				os.write(b);
			}
			is.close();
			os.close();
		}
        rs.close();
        s.close();
        c.close(); 
	}
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
		insert();
//		select();
	}
}
?