java如何读取二进制图片

HuangIs 2009-04-06 10:06:15
请教如题 麻烦大家帮帮忙 怎么用java读取二进制形式的图片 要显示出来 有源代码的麻烦给个 多谢了。。。。。
...全文
3493 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzm_keivn 2011-08-24
  • 打赏
  • 举报
回复
對啊完全不對題啊...
阁楼上的伟哥 2009-11-24
  • 打赏
  • 举报
回复
啥玩意啊 回答的都不对题目
renli2152 2009-07-13
  • 打赏
  • 举报
回复
是不是还得将二进制转成原来的啊
winnernoom 2009-04-10
  • 打赏
  • 举报
回复
学习了
zhizhuo89 2009-04-08
  • 打赏
  • 举报
回复
最好放到servlet里。呵呵。
yinglu206 2009-04-08
  • 打赏
  • 举报
回复
学习了
yinglu206 2009-04-08
  • 打赏
  • 举报
回复
学习了
Sodino 2009-04-08
  • 打赏
  • 举报
回复
好麻烦啊...
ameyume 2009-04-07
  • 打赏
  • 举报
回复
/**  
* 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);

}
}

}

顶下。
YL_Show 2009-04-07
  • 打赏
  • 举报
回复
Mark
kungfucop1 2009-04-07
  • 打赏
  • 举报
回复
mark
alen1985 2009-04-06
  • 打赏
  • 举报
回复
/**
* 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);

}
}

}

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧