本文目錄一覽:
java保存圖片到本地伺服器共享
1、調用第一個介面時,先將多張圖片存到本地。再調用第二個介面,將圖片統一上傳到圖片伺服器上。
2、根據類別,上傳多張圖片。
java 中怎麼存儲圖的
java將byte數組轉換成圖片,可以File和IO操作來完成,實例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
//byte數組到圖片到硬碟上
public void byte2image(byte[] data,String path){
if(data.length3||path.equals(“”)) return;//判斷輸入的byte是否為空
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));//打開輸入流
imageOutput.write(data, 0, data.length);//將byte寫入硬碟
imageOutput.close();
System.out.println(“Make Picture success,Please find image in ” + path);
} catch(Exception ex) {
System.out.println(“Exception: ” + ex);
ex.printStackTrace();
}
}
java 保存圖片
ImageIO.write(BufferedImage, “JPG”, File);
================================
傳入Component保存圖像的方法,你試試看還有沒有變色。
public void cutScreen(Component com) {
Rectangle rect = com.getBounds();
BufferedImage bi = (BufferedImage) com.createImage(rect.width,
rect.height);
Graphics g = bi.getGraphics();
com.paint(g);
g.dispose();
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory()
|| f.getName().toLowerCase().endsWith(“.jpg”);
}
public String getDescription() {
return “*.jpg”;
}
});
int type = jfc.showSaveDialog(null);
if (type == 0) {
File file = jfc.getSelectedFile();
name = file.getName().toLowerCase();
if (!name.endsWith(“jpg”)) {
String path = file.getAbsolutePath();
file = new File(path + “.jpg”);
for (int i = 0; file.exists(); i++) {
file = new File(path + “(” + i + “).jpg”);
}
}
try {
if (!file.exists()) {
file.createNewFile();
}
ImageIO.write(bi, “JPG”, file);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
在java中如何將圖片保存到資料庫?
存取圖片就是二進位數據的存取問題
把圖片以文件的時候讀入到程序中
轉換成byte
以byte顯示保存到資料庫中
另外,access保存文件~~不合適~
—————————
顯示和存儲沒關係,看你要怎麼顯示了~顯示到瀏覽器?
java如何將圖片保存在資料庫中
一般都是這樣的,就是在你伺服器有一個專門放置圖片的文件夾,然後資料庫保存的是你伺服器圖片的路徑。需要用的時候就去資料庫裡面取路徑。得到路徑以後你想怎麼處理圖片是你的事情了。
至於如何去資料庫取路徑這個就是簡單的db操作。
載入驅動類:
Class.forName(DBDriver);
獲取連接:
Connection
conn
=
DriverManager.getConnection(url,username,password);
創建操作對象:
PreparedStatement
stmt
=
con.prepareStatement(sql);
執行操作:
ResultSet
rs
=
stmt.executeQuery();
遍歷結果:
List
list
=
new
ArrayList();
while(rs.next()){
//具體操作,通常用rs.getString(name)取值
Image
img
=
new
Image();//圖片類對應你資料庫中圖片表格
img.setSrc(rs.getString(“src”));//假設你資料庫中image表中圖片地址欄位是src
list.add(img);
}
記得關閉資源:
rs.close();
stmt.close();
con.close();
看你的意思是已經取出來了不知道怎麼顯示:
你取出來之後可以把圖片放在一個list裡面然後去頁面上遍歷這個list
c:forEach
var=”chakan1″
items=”list”
tr
td
img
src=”${chakan1.src}”/
/td
/tr
/c:forEach
大致應該是這樣
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/248603.html