怎样修改下这个程序?里面有详细说明
就是把getFileFromBytes方法中file = new File("c:/tmp.jpg"); 的"c:/tmp.jpg"去掉,我想直接把二进制byte流变成图片文件显示,不想保存临时文件"c:/tmp.jpg"
怎么实现呢?
package picToBin;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
class ImagePanel extends JPanel {
private BufferedImage image;
public ImagePanel() {
try {
byte[] b = fileT0Byte();
File file=getFileFromBytes(b);
//File file = (File) getObject(b);
image = ImageIO.read(file);
// image = ImageIO.read(new File("c:\\rose.jpg"));
} catch (IOException ex) {
// handle exception...
}
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
public static byte[] fileT0Byte() throws IOException {
String path = "c://2013-08.jpg";
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[fis.available()];
fis.read(b);// 写入到byte数组
return b;
}
//public static Object getObject(byte[] b) {
//Object o = null;
//try {
//ByteArrayInputStream bais = new ByteArrayInputStream(b);
//ObjectInputStream ois;
//ois = new ObjectInputStream(bais);
//o = ois.readObject();
//// ArrayList str = (ArrayList) ois.readObject(); //读取类
//} catch (Exception e) {
//// TODO Auto-generated catch block
//e.printStackTrace();
//}
//return o;
//}
public static File getFileFromBytes(byte[] b) {
BufferedOutputStream stream = null;
File file = null;
try {
file = new File("c:/tmp.jpg");
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return file;
}
}
Class.forName(driverName);
con = DriverManager.getConnection(url,userName,password);
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT photo FROM t_kk_applicant Where kk_applicant_id='N7V9FS3005421993' and photo is not null");
while (rs.next()) {
Blob test=rs.getBlob("photo");
int blobLength = (int) test.length();
byte[] blobAsBytes = test.getBytes(1, blobLength);
System.out.println("Length"+blobLength);
System.out.println("testlen"+blobAsBytes.length);
BufferedImage image = ImageIO.read( new ByteArrayInputStream( blobAsBytes ) );