① 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里怎麼把從資料庫里讀取的圖片類型數據 顯示到頁面上啊
樓上挺全了!!
不過說些題外讓搜話
就是
資料庫中存取圖片文件的做法是賣派不大好的!!
建議圖片文件存放在硬碟中,資料庫存放文件的相對路徑。
顯示的時候直接讀取路徑信坦配歷息。