ImageVerifierCode 换一换
格式:DOCX , 页数:10 ,大小:19KB ,
资源ID:16958514      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/16958514.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(MYSQL存取图片文件Word格式.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

MYSQL存取图片文件Word格式.docx

1、mysql:/localhost/img?user=root&password=root&useUnicode=true&characterEncoding=gbk;Connection conn = DriverManager.getConnection(url);PreparedStatement pstmt = INSERT INTO myTable(image) VALUES (?)pstmt = conn.prepareStatement(sql);File file = new File(file的URLInputStream imageStream = new FileInput

2、Stream(file);pstmt.setBinaryStream(1, imageStream, (int) file.length();pstmt.executeUpdate();pstmt.close();conn.close();取:PreparedStatement pstmt = conn.prepareStatement(select image from myTable where id=?/设置IDbyte buffer = new byte4096;File file = new File(outfile);/读取后要保存的位置FileOutputStream fos =

3、 new FileOutputStream(file);ResultSet rs = pstmt.executeQuery();InputStream in = rs.getBinaryStream(imageint size = -1;while (size = is.read(buffer) != -1) fos.write(buffer, 0, size);/如果把fos改成是servlet中的response.getOutputStream(),就可以将图片返回给客户端显示了。/关闭资源二、JSP在MySQL中存取图片2008年04月04日 星期五 23:41为了使用JSP灵活,需要把

4、各种文件储存到数据库中,然后需要的时候把它读取出来显示到客户端。这些文件包括音乐,图片,文本等,人们统称为二进制文件。首先,二进制文件储存到数据库的过程:打开文件,将内容读到缓冲区,然后直接联线创建jdbc语句对象,使用该缓冲区的数据,并执行更新,就完成了储存。例子: 首先在mysql中创建一个表 picture_dbcreate table picture_db(file_name varchar(255) not null,content longblob,primary key (file_name);接下来就是用java写储存文件的代码:假设要储存的图片名称是:01.jpg (放在同一

5、个目录下)import java.sql.*;import java.io.*;import java.nio.*;public class UploadImage protected Connection dbConnection;protected String driverName = com.mysql.jdbc.Driverprotected String dbURL = /localhost:3306/sample_dbprotected String userID = rootprotected String passwd = yourpasswordpublic boolean

6、 storeImage(String sqlstr,File file)tryFileInputStream fin = new FileInputStream(file);ByteBuffer nbf = ByteBuffer.allocate(int)file.length();byte array = new byte1024;int offset =0,length=0;while(length=fin.read(array)0)if(length!=1024) nbf.put(array,0,length);else nbf.put(array); offset+=length; f

7、in.close();byte content = nbf.array();return setImage(sqlstr,content);catch(FileNotFoundException e) e.printStackTrace();catch (IOException e)return false;private boolean setImage(String sqlstr,bytein)boolean flag = false;if(sqlstr=null) sqlstr=select * from picture_db Class.forName(driverName); dbC

8、onnection = DriverManager.getConnection(dbURL,userID,passwd);Statement stmt = dbConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);ResultSet rs = stmt.executeQuery(sqlstr);if(rs.next() rs.updateBytes(2,in); rs.updateRow();else rs.moveToInsertRow(); rs.updateStri

9、ng(1,01 rs.insertRow(); rs.close(); flag=true;catch(Exception e)return flag;public static void main(String args) UploadImage upload = new UploadImage();01.jpgif(upload.storeImage(null, file)System.out.print(tureFalse如果执行成功的话,系统打印“true 否则“false.最后就是将图片度取出来:与储存的过程相反,它是县建立连接,创建数据库查询的jdbc对象,使用该语句来返回二进制结

10、果,保存到文件当中。showImage.jsp% page import=java.sql.*java.io.*com.sun.image.codec.jpeg.*javax.imageio.*java.awt.image.*htmlheadmeta http-equiv=Content-Type content=titleshowDBImage/headbody%String showImage =select * from picture_db where file_name =01Connection conn = null;BufferedInputStream inputImage

11、= null;String driverName = String dbURL = String userID = String passwd = Class.forName(driverName).newInstance(); conn = DriverManager.getConnection(dbURL,userID,passwd); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(showImage);while(rs.next() Blob blob = (Blob)rs.getBlob(co

12、ntent inputImage = new BufferedInputStream(blob.getBinaryStream(); BufferedImage image = null; image = ImageIO.read(inputImage);ServletOutputStream sos = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos); encoder.encode(image); inputImage.close();catch(SQLExcept

13、ion e)catch(IOException e)/body/html三、/*-表结构-表名:student2+-+-+-+-+-+-+| Field | Type | Null | Key | Default | Extra | id | int(4) | NO | PRI | NULL | | name | varchar(20) | YES | | NULL | | stupic | blob | YES | | NULL | |*/package com.ibm.jdbc;public class StoreBLOB public static void main(String ar

14、gs) /连接MySQl数据库 Connection con=DBManager.getConnection(); PreparedStatement ps=null; InputStream in=null; try /从本地硬盘读取一张读片 in=new FileInputStream(d:/111.jpg ps=con.prepareStatement(insert into student2 values(?,? ps.setInt(1,2); ps.setString(2, Tom ps.setBinaryStream(3, in, in.available(); ps.execut

15、eUpdate(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); finally try /关闭流 if(in!=null) in.close(); catch (IOException e) /关闭相关连接 DBManager.close(ps, con); 取出操作public class GetBLOB public stat

16、ic void main(String args) Connection con=DBManager.getConnection(); Statement st=null; ResultSet rs=null; InputStream in=null; OutputStream out=null; try st=con.createStatement(); rs=st.executeQuery(select stupic from student2 where id=2 rs.next(); /将光标指向第一行 /从rs中读取stupic放进InputStream对象中 in=rs.getBi

17、naryStream(stupic /申明byte数组,用来存放图片流 byte b=new byte40000; in.read(b); /从InputStream对象中读取数据放进byte数组中 /实例化OutputStream对象,在D盘创建一个图片文件 out=new FileOutputStream(/222.jpg /将文件输出,内容则为byte数组里面的数据 out.write(b); out.flush(); catch (SQLException e) / TODO Auto-generated catch block catch (IOException e) finally if(in!=null) in.close(); if(out! out.close(); catch (IOException e) DBManager.close(rs, st, con);/关闭相关连接

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1