① java怎么做到把图片转换成流存入数据库,然后怎么再把图片显示出来。
首先创建一个空 Blob/Clob 字段,再从这个空 Blob/Clob字段获取游标,例如下面的代码:
PreparedStatement ps = conn.prepareStatement( " insert into PICTURE(image,resume) values(?,?) " );
// 通过oralce.sql.BLOB/CLOB.empty_lob()构造空Blob/Clob对象
ps.setBlob( 1 ,oracle.sql.BLOB.empty_lob());
ps.setClob( 2 ,oracle.sql.CLOB.empty_lob());
ps.excuteUpdate();
ps.close();
// 再次对读出Blob/Clob句柄
ps = conn.prepareStatement( " select image,resume from PICTURE where id=? for update " );
ps.setInt( 1 , 100 );
ResultSet rs = ps.executeQuery();
rs.next();
oracle.sql.BLOB imgBlob = (oracle.sql.BLOB)rs.getBlob( 1 );
oracle.sql.CLOB resClob = (oracle.sql.CLOB)rs.getClob( 2 );
// 将二进制数据写入Blob
FileInputStream inStream = new FileInputStream( " c://image.jpg " );
OutputStream outStream = imgBlob.getBinaryOutputStream();
byte [] buf = new byte [ 10240 ];
int len;
while (len = inStream.read(buf) > 0 ) {
outStream.write(buf, 0 ,len);
}
inStream.close();
outStream.cloese();
// 将字符串写入Clob
resClob.putString( 1 , " this is a clob " );
// 再将Blob/Clob字段更新到数据库
ps = conn.prepareStatement( " update PICTURE set image=? and resume=? where id=? " );
ps.setBlob( 1 ,imgBlob);
ps.setClob( 2 ,resClob);
ps.setInt( 3 , 100 );
ps.executeUpdate();
ps.close();
② java如何读取文件夹中的图片并在界面显示
下面给你提供一个实现,该实现采用了代理模式。这个实现包含两个文件,分别是Client.java和ImageIcoProxy.java,ImageIcoProxy.java负责了图片的延迟加载,你可以修改为不延迟即可。
Client.java的代码为:
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.Icon;
import javax.swing.JFrame;
public class Client extends JFrame {
private static int IMG_WIDTH = 510;
private static int IMG_HEIGHT = 317;
private Icon imgProxy = null;
public static void main(String[] args) {
Client app = new Client();
app.setVisible(true);
}
public Client() {
super("Virture Proxy Client");
imgProxy = new ImageIcoProxy("D:/test.jpg", IMG_WIDTH, IMG_HEIGHT);
this.setBounds(100, 100, IMG_WIDTH + 10, IMG_HEIGHT + 30);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
Insets insets = getInsets();
imgProxy.paintIcon(this, g, insets.left, insets.top);
}
}
ImageIcoProxy.java的代码为:
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
public class ImageIcoProxy implements Icon {
private ImageIcon realIcon = null;
private String imgName;
private int width;
private int height;
boolean isIconCreated = false;
public ImageIcoProxy(String imgName, int width, int height) {
this.imgName = imgName;
this.width = width;
this.height = height;
}
public int getIconHeight() {
return realIcon.getIconHeight();
}
public int getIconWidth() {
return realIcon.getIconWidth();
}
public void paintIcon(final Component c, Graphics g, int x, int y) {
if (isIconCreated) {
//已经加载了图片,直接显示
realIcon.paintIcon(c, g, x, y);
g.drawString("Just Test", x + 20, y + 370);
} else {
g.drawRect(x, y, width-1, height-1);
g.drawString("Loading photo...", x+20, y+20);
synchronized(this) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Thread.currentThread().sleep(2000);
realIcon = new ImageIcon(imgName);
isIconCreated = true;
} catch (Exception e) {
e.printStackTrace();
}
c.repaint();
}
}
);
}
}
}
}
③ JAVA 读取 数据库中的图片显示到页面
你说你只想存ID到数据库中,那你就专门用个文件夹存放图片,ID值就是图片文件名字!从数据库得到ID后,就在<IMG SRE="这里写上绝对路径"+ID+".jpg"/>
④ 求 Java 图形界面化 读取并显示一张本地图片 代码
界面,使用javax.swing.JFrame 比较多
读取图片,有多种方法,简单地,这样也可以
JLabel.setIcon(new ImageIcon("Path to image file"));
⑤ JAVA中怎样获取文件夹里的图片 并把图片在Panel里显示出来
这是第一个程序文件:
import java.awt.*;
import javax.swing.*;
public class ImagePanel extends JPanel {
private Image img;
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null),
img.getHeight(null));
setSize(size);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img,0,0,null);
}
}
这是第二个:
import java.awt.*;
import javax.swing.*;
public class ImageTest {
public static void main(String[] args) {
ImagePanel panel = new ImagePanel(new
ImageIcon("images/01.jpg").getImage());
JFrame frame = new JFrame("多问自己,会使你少犯错误!");
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
⑥ java里怎么把从数据库里读取的图片类型数据 显示到页面上啊
楼上挺全了!!
不过说些题外让搜话
就是
数据库中存取图片文件的做法是卖派不大好的!!
建议图片文件存放在硬盘中,数据库存放文件的相对路径。
显示的时候直接读取路径信坦配历息。