怎样把一幅图像存到数据库里面去,然后读出来显示?

207 2003-10-09 05:06:53
最好能有详细代码,谢谢!
...全文
37 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
marlang 2003-10-14
  • 打赏
  • 举报
回复
up
seaman0916 2003-10-14
  • 打赏
  • 举报
回复
关注!
207 2003-10-10
  • 打赏
  • 举报
回复
请问有相关资料的文章吗?
favourl 2003-10-10
  • 打赏
  • 举报
回复
//////////////////////////////////////////////////////////
//插入文件
import java.sql.*;

public class InsertFileTest
{
String strDriverClass = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String strUrl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
String strUser = "sa";
String strPsw = "";

private Connection conn;

public InsertFileTest()
{
try
{ //引入要SqlServer的Jdbc数据库驱动
Class.forName(strDriverClass).newInstance();
//连接数据

conn = DriverManager.getConnection(strUrl,strUser,strPsw);
String filePath = "C:\\dict\\index_dict.xml";
java.io.File file = new java.io.File(filePath);
java.io.FileInputStream ofis = new java.io.FileInputStream(filePath);

// String sql = "BULK INSERT bigfile(id,myxml,mytext) FROM ('1','C:\\dict\\index_dict.xml','C:\\dict\\index_dict.xml'";
String sql = "SELECT TOP 0 code,myfile,size FROM filetest";
// CallableStatement statement = conn.prepareCall(sql);

Statement objStm = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);

ResultSet ofRS = objStm.executeQuery(sql);
// ofRS.insertRow();
ofRS.moveToInsertRow();
// ofRS.next();
ofRS.updateString("code","index");
ofRS.updateBinaryStream("myfile", ofis, (int)file.length());
ofRS.updateInt("size",(int)file.length());
//Clob clob = ofRS.getClob("myxml");
//byte[] buf = new byte[(int)file.length()];
//java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
//fos.write(buf);
//clob.getAsciiStream().read(buf);

//ofRS.updateClob("myxml",clob);

ofRS.insertRow();
ofRS.close();
objStm.close();
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}

public static void main(String[] args)
{
new InsertFileTest();
}
}

//////////////////////////////////////////////////////////////
//读取文件
import java.sql.*;
import java.io.*;

public class ReadFile
{

public ReadFile()
{
}

public static void main(String[] args)
{
String strDriverClass = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String strUrl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
String strUser = "sa";
String strPsw = "";
Connection conn;
try
{ //引入要SqlServer的Jdbc数据库驱动
Class.forName(strDriverClass).newInstance();
//连接数据
conn = DriverManager.getConnection(strUrl,strUser,strPsw);
String filePath = "C:\\dict\\index_dict_temp.xml";
java.io.File file = new java.io.File(filePath);

if(!file.exists())
{
// String sql = "BULK INSERT bigfile(id,myxml,mytext) FROM ('1','C:\\dict\\index_dict.xml','C:\\dict\\index_dict.xml'";
String sql = "SELECT code, myfile,size FROM filetest WHERE code='index'";
// CallableStatement statement = conn.prepareCall(sql);

Statement objStm = conn.createStatement();

ResultSet ofRS = objStm.executeQuery(sql);
String code = null;
int size = 0;
if(ofRS.next()) {
code = ofRS.getString("code");
size = ofRS.getInt("size");
System.out.println(code+" "+size);
}

ofRS = objStm.executeQuery(sql);
if(ofRS.next())
{
java.io.InputStream is = ofRS.getBinaryStream("myfile");
byte[] buf = new byte[size];
is.read(buf);

FileOutputStream fos = new java.io.FileOutputStream(filePath);
fos.write(buf);

System.out.println(new String(buf));
}

ofRS.close();
objStm.close();
conn.close();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
hzm1029 2003-10-10
  • 打赏
  • 举报
回复
oracle有一种字段类型BLOB,可以存储图象,使用时需要以流的形式写入和读出,具体代码手头没有
207 2003-10-10
  • 打赏
  • 举报
回复
oracle数据库,各位兄弟有没有好网址介绍一下,让我看看相关文章阿
或者列出个程序DEMO阿,谢谢!
hzm1029 2003-10-10
  • 打赏
  • 举报
回复
http://www.csdn.net/Develop/article/20%5C20484.shtm
这里有一篇文章(不是我写的),大概对你有所帮助
loveknife 2003-10-10
  • 打赏
  • 举报
回复
把图片放到一个特定的文件夹里
然后在数据库里存储文件的名称和后缀名
207 2003-10-10
  • 打赏
  • 举报
回复
up
Yssss1980 2003-10-09
  • 打赏
  • 举报
回复
建议你不要把图片存到数据库

存个url就可以了吧
yangjuanli 2003-10-09
  • 打赏
  • 举报
回复
GOOGLE 上搜呀
大把这样的例子。
onizuka5211 2003-10-09
  • 打赏
  • 举报
回复
jspsmartupload组建上传具体代码在jspsmartupload得例子里就有,看看吧
etre 2003-10-09
  • 打赏
  • 举报
回复
什么数据库啊

62,614

社区成员

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

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