保存图片、word等到数据库,并读出

数据库中保存图片等的字段类型设置为blob

界面,上传窗口

<div id="uploadWin" class="easyui-window" title="My Window" closed="true">
					<br>
					<strong>注意事项:</strong>
					<br>
					<div>1.请上传大小适合的图片<br>2.只能上传gif或者jpg类型的图片。</div>
					<br>
					    上传文件:<input type="file" name="file" id="theFile"/>  
					        <br/>  
					        <input type="button" value="确认选择" onClick="return close_upload();"/> 
					        <input type="button" value="取消" οnclick="return canclePhoto();"/> 
					        <div id="upMessage" style="displan:hidden"></div>
					</div>
				</div>

 

action层

@Resource
private SystemService systemService;
private File file;
public File getFile() {
		return file;
}

public void setFile(File file) {
	this.file = file;
}

systemService.saveCommanInfo(this.getFile());

 

service层

@Resource
private SystemDao systemDao;
public String saveCommanInfo(List<Object> list,File file) throws Exception {
		return systemDao.saveCommanInfo(file);
}

 

dao层

Connection con = DB.getConnection();
		PreparedStatement  psta=con.prepareStatement("insert into zxtagl_tb_psrole_user_comman(sessions,username,name,photo,isconvener) values(?,?,?,?,?)");
		System.out.println("上传的文件"+file);
		if(null!=file) {
			InputStream in=new BufferedInputStream(new FileInputStream(file));
			psta.setInt(1, 1104);
			psta.setString(2, "gzry");
			psta.setString(3, "工作人员");
			psta.setBinaryStream(4, in, (int)file.length());
			psta.setString(5, "N" );
			
			
			psta.execute();
			in.close();

 

就这样很简单就可以把图片、word等以二进制的形式存到数据库了

下面说说如何读取

Blob blob=null;
while(rs.next()) {
	blob=rs.getBlob(1);
	........
}
if(null!=blob) {
	//获取流对象后就好处理了
	InputStream in=blob.getBinaryStream();
}

 

 

要将图片写入数据库,你需要将其转换为二进制格式并将其存储在数据库的BLOB字段中。可以使用Python的Pillow库来打开、读取和转换图像文件。以下是将一张名为“image.png”的图片写入数据库的示例代码: ```python import mysql.connector from mysql.connector import Error from PIL import Image # 读取要写入的图片 img = Image.open('image.png') # 将图片转换为二进制格式 with open("image.bin", "wb") as f: f.write(img.tobytes()) # 连接数据库 try: conn = mysql.connector.connect(host='localhost', database='my_database', user='my_username', password='my_password') if conn.is_connected(): print('Connected to MySQL database') # 将二进制数据写入数据库 cursor = conn.cursor() sql_insert_blob_query = """ INSERT INTO my_table (id, image_data) VALUES (%s,%s)""" with open("image.bin", "rb") as f: image_blob = f.read() # 执行 SQL 语句并提交到数据库 cursor.execute(sql_insert_blob_query, (1, image_blob)) conn.commit() print("Image and file inserted successfully as a BLOB into MySQL table") except Error as e: print(e) finally: cursor.close() conn.close() ``` 要从数据库中读取图像,可以使用以下代码: ```python # 连接数据库 try: conn = mysql.connector.connect(host='localhost', database='my_database', user='my_username', password='my_password') if conn.is_connected(): print('Connected to MySQL database') # 从数据库中读取二进制数据 cursor = conn.cursor() sql_fetch_blob_query = """SELECT image_data FROM my_table WHERE id = %s""" cursor.execute(sql_fetch_blob_query, (1,)) record = cursor.fetchone() # 将数据转换为图像文件并保存到磁盘上 with open("image_out.png", "wb") as f: f.write(record[0]) except Error as e: print(e) finally: cursor.close() conn.close() ``` 这将从数据库中读取id为1的记录中的图像数据,并将其保存为名为“image_out.png”的文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值