如何读取保存在SQL数据库中图片,我搜索了很多贴子,都没解决!

oolin 2004-10-14 01:26:18
我的使用sql 2000数据库,数据表就两个字段,id及image

我的servlet代码。

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

String id = request.getParameter("id");
if (id==null) id="1";
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String URL="jdbc:microsoft:sqlserver://127.0.0.1:1433;User=sa;Password=123;DatabaseName=jsp";
Connection conn=DriverManager.getConnection(URL);

String cmd="select image from jsp where id="+id;
PreparedStatement sql=conn.prepareStatement(cmd);
ResultSet rs = sql.executeQuery();
if (rs.next()){
response.reset();
response.setContentType("image/*");
ServletOutputStream out = response.getOutputStream();
InputStream inStream = rs.getBinaryStream("image");
byte[] b = new byte[1024];
int len;
while((len=inStream.read(b)) >0){
out.write(b,0,len);
}
out.close();
inStream.close();
}
}catch (Exception e){
System.out.println(e);
}

}

可是运行后的结果确是:

-----------------------------7d465a1d0544 Content-Disposition: form-data; name="filename"; filename="D:\web\logo.jpg" Content-Type: image/pjpeg ?JFIFHH?逧..............(还有很多)....


请各位帮忙一下了,我找不到解决的办法,搜索了很多贴子,都没解决。

...全文
177 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Drate 2004-10-15
  • 打赏
  • 举报
回复
呵呵,这个操作很简单的,像楼上的就可以了
ningIII 2004-10-15
  • 打赏
  • 举报
回复
直接操作BLOB字段就是了

把图片文件直接写入或者先读取byte[],然后写BLOB

strSQL = "insert into inform(FILEATT) values(empty_blob())";
pstmt = conn.prepareStatement(strSQL);
pstmt.executeUpdate();

strSQL = "select FILEATT from inform ";
rs = pstmt.executeQuery();

if (rs.next()) {
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("INFILE_DATA");
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(file.getInputStream());

int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
HFeiLin 2004-10-15
  • 打赏
  • 举报
回复
wypenguin(WangYong) ,楼主要的是JAVA呀!
wypenguin 2004-10-15
  • 打赏
  • 举报
回复
‘======img.asp=============
'---------------------------------------------------
'检测函数把数据库的值赋给session变量。
Function PicList(F_list, F_type)
F_type = LCase(trim(F_type))'如果有问题就屏蔽这一句
nFieldSize =F_list.ActualSize
bytes =F_list.GetChunk(nFieldSize)
session("Bytes") = bytes
session("Type") = F_type
End Function
'====================================================
%>
<% '按照数据流的格式和类型写出Bytes的原内容。
strsql="select * from * where id="&request("id")
rs.open strsql,conn
piclist rs("picture"),rs("imag")
'显示文件的图片格式picture为图片数据imag为图片类别
response.Expires = 0
response.Buffer = True
response.Clear
response.contentType=session("Type")
'Response.Write(Session("ImageBytes"))
response.BinaryWrite session("Bytes")
session("Type")=""
session("Bytes")=""
response.End
'==========程序结束===
在其它的网页上调用<img srg=img.asp?id=<%=*%> name ="pic" >就可以了
debug158 2004-10-15
  • 打赏
  • 举报
回复
ding
oolin 2004-10-14
  • 打赏
  • 举报
回复
litf(study) ,谢谢你能找到.

cm4ever(小P),我已经找很多久了,实在是没信心在找了.
禽兽v5 2004-10-14
  • 打赏
  • 举报
回复
到sql server 2000 论坛搜索,答案多多。

http://community.csdn.net/expert/deeptree/RoomForum.asp?bigroomid=57
litf 2004-10-14
  • 打赏
  • 举报
回复
我写过的,我找到了给你回。
SQLSERVER2000
bkm2 2004-10-14
  • 打赏
  • 举报
回复
oolin 2004-10-14
  • 打赏
  • 举报
回复
难道是这问题太简单了? 请写过相关代码的帮忙一下!

81,094

社区成员

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

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