从数据库读取图片问题

stckiss 2006-03-16 12:56:29
package com.demo.Test.images;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;

public class ImageDemo {
public static Connection con = null;

public static Statement stat = null;

public static FileInputStream fs = null;

public static PreparedStatement ps = null;

public static File file = null;

public static void main(String args[]) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
.newInstance();
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MyDataBase";
con = DriverManager.getConnection(url, "sa", "");
stat = con.createStatement();

file = new File("D:/struts-power.gif");
fs = new FileInputStream(file);
ps = con.prepareStatement("insert into images values(?,?)");
ps.setString(1, file.getName());
ps.setBinaryStream(2, fs, fs.available());
ps.executeUpdate();
System.out.println("成功加入图片......");
/*
* 从数据库里取得图片
*/
InputStream input = null;
Image myimages = null;
ResultSet rs = stat.executeQuery("select * from images");
while (rs.next()) {
input = rs.getBinaryStream(2);
}

ByteArrayOutputStream boutput = new ByteArrayOutputStream();
int image = input.available();
for (int i = 0; i < image; i++) {
boutput.write((char) input.read());
}
myimages = Toolkit.getDefaultToolkit().createImage(
boutput.toByteArray());

boutput.close();
input.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}
提示说:int image = input.available();这一句有问题..实在不知道问题在什么地方.
(国际惯例:解决给分)
表就两个字段 name image
...全文
437 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
TheUnforgiven 2006-03-22
package per.WBF.actions;
import javax.servlet.*;
import javax.servlet.http.*;

import java.awt.*;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.*;

import per.WBF.classes.ActionException;
import per.WBF.classes.DataBaseCon;

import java.io.*;
import java.sql.*;

public class ShowPhotoAction{
private Connection con;
public void excute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException,ActionException,Exception{
int id = Integer.parseInt(request.getParameter("id"));
this.con=DataBaseCon.getCon();
String sql = "select * from info_xx_personnel where id="+id;
try {
con.setAutoCommit(false);//在显示图片的时候不允许对数据库的数据进行修改
PreparedStatement psmt = con.prepareStatement(sql);
ResultSet rs = psmt.executeQuery();
if (rs.next()){
Blob bb = null;
bb = rs.getBlob("photo");
response.setContentType("image/*");
OutputStream outStream = response.getOutputStream();
if (bb!=null) {//有照片信息则显示这张图片
InputStream instream = bb.getBinaryStream();
byte[] bytes = new byte[1024];
int i=0;
while ( (i = instream.read(bytes)) != -1){
outStream.write(bytes, 0, i);
}
instream.close();
}
else {//没有就画下面这张图
BufferedImage image = new BufferedImage (150,180,BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
Color backColor = new Color(222,222,222);//底色为浅灰
g.setColor(backColor);
g.fillRect(0,0,150,180);
g.setFont(new Font("仿宋", 0 , 27));
g.setColor(Color.black);
g.drawString("暂 无",40,45);//X坐标是字左与左边界的距离,Y坐标是字底与上边界的距离
g.drawString("此 人",40,90);
g.drawString("照 片",40,135);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outStream);
encoder.encode(image);
}
outStream.close();
con.commit();
rs.close();
psmt.close();
}//if
}//try
catch (SQLException sqle) {
System.out.println("类ShowPhotoAction读取数据库时出现异常,可能导致数据不完整!");
}
catch (IOException ioe) {
System.out.println("类ShowPhotoAction出现IO异常!");
}
catch (Exception e) {
System.out.println("类ShowPhotoAction出现异常!");
}
finally {
try {
if(con!=null && !con.isClosed())
con.close();
}
catch (Exception e) {
System.out.println("类ShowPhotoAction在关闭数据库连接时出现异常!");
}
}
}
}
回复
diggywang 2006-03-22
int 肯定是不行的,图片一般用long
回复
huantianxidi 2006-03-16
int image = input.available(); 不太明白 为什么要加这句
回复
doway 2006-03-16
应该算是标准写法啊,怎么会死循环呢?

是不是楼主放进去的东西太大了,得读老半天?楼主可以加一句 System.out.println(rc); 看一看。
回复
stckiss 2006-03-16
楼上的方法我也试过了.但是在while ((rc = input.read(buf)) != -1) {
boutput.write(buf, 0, rc);
}就不动了.死循环啊!!
回复
doway 2006-03-16
if (rs.next()) {
input = rs.getBinaryStream(2);
}

ByteArrayOutputStream boutput = new ByteArrayOutputStream();
int image = input.available();
byte[] buf = new byte[1024];
int rc = 0;
while ((rc = input.read(buf)) != -1) {
boutput.write(buf, 0, rc);
}

问题出在 while(next())。当退出 while 时,最后一次对 next 的调用,一方面使指针移动到 eof,另一方面自动关闭了 input = rs.getBinaryStream(2); 中打开的流。所以,available 的调用就报 IOException 了。

另外读取的方法明显不合理,应该改成按字节读取,即我给出的代码。
回复
stckiss 2006-03-16
com.microsoft.jdbc.base.BaseInputStreamWrapper 不知道这个类是搞什么的!!自己先up
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2006-03-16 12:56
社区公告
暂无公告