Java怎样读取数据库中的二进制图片并下载到本地磁盘 10

 我来答
yvee_lin
2010-08-25
知道答主
回答量:32
采纳率:0%
帮助的人:18.5万
展开全部
/**
* Created by IntelliJ IDEA.
* User: ljt
* Date: 2003-3-31
* Time: 18:51:38
* To change this template use Options | File Templates.
*/
import oracle.jdbc.driver.OraclePreparedStatement;
import oracle.jdbc.driver.OracleResultSet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Clob;

public class TestOpenDoc {
public OracleResultSet ors = null; //**这里rs一定要用Oracle提供的
public OraclePreparedStatement opst = null; //**PreparedStatement用
public Connection conn = null;
public Statement stmt = null;

public TestOpenDoc() {
}

public boolean getConnect() {
//这是我的数据库所在
String serverName = "prosrv";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@" + serverName + ":1521:BOHDATA";
conn = DriverManager.getConnection(url, "appuser", "appuser");
}
catch (Exception e) {
System.out.println(e);
return false;
}
return true;
}

public static void main(String[] args) {
TestOpenDoc test = new TestOpenDoc();
if (!test.getConnect()) {
System.out.println("数据库连结错误");
return ;
}
try{

test.conn.setAutoCommit(false);
byte a[] = null; //**将测试文件test.doc读入此字节数组
java.io.FileInputStream fin = null;
java.io.FileOutputStream fout = null;

//Oracle提供的
try {
java.io.File f1 = new java.io.File("c:/test.doc");
java.io.File f2 = new java.io.File("d:/testout.doc"); //**从BLOB读出的信息写

//入该文 件,和源文件对比测试用
fin = new java.io.FileInputStream(f1);
fout = new java.io.FileOutputStream(f2);

int flength = (int) f1.length(); //**读入文件的字节长度
System.out.println("file length::" + flength);
a = new byte[flength];

int i = 0;
int itotal = 0;
//* 将文件读入字节数组
for (; itotal < flength; itotal = i + itotal) {
i = fin.read(a, itotal, flength - itotal);
}
fin.close();

System.out.println("read itotal::" + itotal);
//**注意Oracle的 BLOB一定要用EMPTY_BLOB()初始化
String mysql =
"insert into filelist (FileName,FileSize,FileBody) values (?,?,EMPTY_BLOB())";
OraclePreparedStatement opst = (OraclePreparedStatement) test.conn.
prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
opst.setInt(2, flength);
opst.executeUpdate();
opst.clearParameters();
// /**插入其它数据后,定位BLOB字段
mysql = "select filebody from filelist where filename=?";
opst = (OraclePreparedStatement) test.conn.prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
OracleResultSet ors = (OracleResultSet) opst.executeQuery();

if (ors.next()) {
oracle.sql.BLOB blob = ors.getBLOB(1); //**得到BLOB字段
int j = blob.putBytes(1, a); //**将字节数组写入BLOB字段
System.out.println("j:" + j);
test.conn.commit();
ors.close();
Clob clob;
clob = ors.getClob("");
String str;
str = clob.toString();
str = clob.getSubString(0L,(int)clob.length());
System.out.println(str);
}

System.out.println("insert into ok");

byte b[] = null; //**保存从BLOB读出的字节
opst.clearParameters();
mysql = "select filebody from filelist where filename=?";
opst = (OraclePreparedStatement) test.conn.
prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
ors = (OracleResultSet) opst.executeQuery();
if (ors.next()) {
oracle.sql.BLOB blob2 = ors.getBLOB(1);

System.out.println("blob2 length:" + blob2.length());
b = blob2.getBytes(1, flength); //**从BLOB取出字节流数据
System.out.println("b length::" + b.length);
test.conn.commit();
}
ors.close();
// 将从BLOB读出的字节写入文件
fout.write(b, 0, b.length);
fout.close();

System.out.println("write itotal::" + b.length);

}
catch (Exception e) {
System.out.println("errror :" + e.toString());
e.printStackTrace();

}
finally { //**关闭所有数据联接
test.conn.commit();
}
}
catch(Exception e){
System.out.println(e);

}
}

}
yuanquanfeng
2010-08-18 · 超过48用户采纳过TA的回答
知道小有建树答主
回答量:195
采纳率:0%
帮助的人:130万
展开全部
流读取,然后保存为图片
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式