導航:首頁 > 編程語言 > javaoracle圖片

javaoracle圖片

發布時間:2022-04-28 22:56:50

java刪除上傳到oracle資料庫的圖片,刪除記錄的同時刪除資料庫本地圖片,如何刪除記錄的同時定位到圖片

1、明白一些概覽:
1)上傳的圖片並沒有真正的存儲在oracle資料庫里,orcale資料庫里其實存儲的是圖片的路徑。
2)從瀏覽器上傳圖片到後端Action,action調用相應文件存儲邏輯(文件存儲系統)由文件存儲系統完成圖片的存儲,並返迴文件的路徑。最後將這個路徑保存到oracle資料庫(這個部分可以很簡單也可以很復雜視實際情況而定)。
2、相關刪除邏輯,通過資料庫里的文件路徑找到實際的存儲位置執行刪除。刪除成功後再刪除資料庫里相關記錄。

❷ 通過java將圖片存儲到oracle中

其實這個更簡單,你可以試試

❸ java中oracle多個blob類型如何在jsp中顯示成圖片

使用WSH(webwork,spring,hibernate)技術,在頁面中載入10-20個圖片時,因為數據都是在oracle 10g中以blob類型存儲,頁面代碼採用webwork標簽iterator遍歷集合,iterator標簽里寫了一個<img>

前台代碼如下:

<ww:iteratorvalue="bloblist"id="it"status="rowstatus">
<%--顯示圖片--%>
<imgid='<ww:propertyvalue="id"/>'name="imgId"width="120"height="120"src="/blobUtil/getImg.action?id=<ww:propertyvalue='bloblist[#rowstatus.index].id'/>"/>
</ww:iterator>

後台代碼:

publicStringgetImg()throwsException{
BufferedInputStreamins;//取得BLOB的IO流
OutputStreamops;
byte[]bt=null;
getResponse().setContentType("image/JPEG");
Blobbo=bjqeDtRelationService.getBjqeDtRelationChildBlob(imgId);
if(bo==null)returnnull;
InputStreamis=bo.getBinaryStream();
ins=newBufferedInputStream(is);
intbufferSize=(int)bo.length();//取得BLOB的長度
bt=newbyte[bufferSize];
ins.read(bt,0,bufferSize);
getResponse().getOutputStream().write(bt);
ops=getResponse().getOutputStream();
ops.flush();
ops.close();
ins.close();
returnnull;
}

❹ JavaEE使用Oracle資料庫怎麼存放和提取圖片、音頻文件

使用io將圖片音頻之類的二進制文件轉化為數據流,然後存入資料庫類型為BLOB。jsp調用就反過來。

❺ java如何把圖片轉換成二進制並存到oracle的blob中,求代碼

importjavax.imageio.ImageIO;
importjava.awt.image.BufferedImage;
importjava.io.ByteArrayInputStream;
importjava.io.ByteArrayOutputStream;
importjava.io.File;
importjava.io.IOException;


publicclassImageUtils{

publicstaticvoidmain(String[]args){
Stringstr=img2Binary("C:\Users\hny\Desktop\favicon.jpg");
System.out.println(str);
binary2Img("C:\Users\hny\Desktop\favicon2.jpg",str);
}

/**
*圖片轉二進制字元串
*
*@parampath圖片路徑
*@return
*/
publicstaticStringimg2Binary(Stringpath){
Filefile=newFile(path);
if(!file.exists()){
returnnull;
}

try{
BufferedImagebi=ImageIO.read(file);
ByteArrayOutputStreambaos=newByteArrayOutputStream();
Stringsuffix=getSuffix(path);
ImageIO.write(bi,suffix,baos);
byte[]bytes=baos.toByteArray();

returnnewsun.misc.BASE64Encoder().encodeBuffer(bytes).trim();
}catch(IOExceptione){
e.printStackTrace();
}
returnnull;
}

/**
*字元串轉圖片文件
*
*@parampath圖片路徑
*@paramimgBinary圖片字元串
*/
publicstaticvoidbinary2Img(Stringpath,StringimgBinary){
try{
Filefile=newFile(path);

byte[]bytes1=newsun.misc.BASE64Decoder().decodeBuffer(imgBinary);
ByteArrayInputStreams=newByteArrayInputStream(bytes1);
BufferedImagebi1=ImageIO.read(s);
Stringsuffix=getSuffix(path);
ImageIO.write(bi1,suffix,file);
}catch(IOExceptione){
e.printStackTrace();
}
}

/**
*獲取圖片後綴名
*
*@parampath
*@return
*/
privatestaticStringgetSuffix(Stringpath){
intindex=path.contains(".")?path.lastIndexOf("."):-1;
if(index>-1){
returnpath.substring(index+1);
}
returnnull;
}
}

❻ 關於JAVA~~~~ 如何將圖片等大對象存入ORACLE中~~求詳解!(代碼)

插入圖片/文本(blob /clob)到oracle資料庫(引用http://www.java-asp.net/java/200512/t_48888.html)
我們在寫OA的時候經常遇到的問題就是員工圖片檔案的儲存問題,解決這個問題有兩個方法,
1.JSP/html頁面裡面讀取web伺服器上的圖片,也就是把圖片放到(上傳)到web 伺服器上,然後用html 語句讀取:

<img src=" 絕對或相對路徑 " border="0" />

2.就是上傳到資料庫裡面(oracle).關於oracle 資料庫,它支持blob, 和clob, 分別對應著圖片和文本(長字元串)操作

由於性能原因,我們還是要採用第二種方法,而且存到資料庫裡面比較容易管理,是吧?

首先,我們要解決上傳問題,這里採用普遍使用的apache commons 組件裡面的FileUpload class.

具體步驟如:

DiskFileUpload dfu=new DiskFileUpload();

dfu.setSizeMax(100000000);

dfu.setSizeThreshold(100000);

dfu.setRepositoryPath("f:\\public");

try{

List fileItems=dfu.parseRequest(request);

Iterator i=fileItems.iterator();

while(i.hasNext()){

FileItem fi=(FileItem)i.next();

if(!fi.isFormField()){

name=fi.getName();

size=fi.getSize();

if((name==null||name.equals(""))&&size==0)

continue;

}

name=fi.getName();

size=fi.getSize();

(InputStream)is=fi.getInputStream();

}

上面的代碼是web伺服器接受上傳的代碼,參考文件已經在我上篇寫的上傳文本文件里給出,今天,終於想明白了:

dfu.setRepositoryPath("f:\\public"); 的意思

原來是轉義字元也就是說\n\t等而要列印反斜杠要用\\,其實這個問題原先已經知道,可是由於經驗沒有寫過圖片上傳處理什麼的,覺得很高深,也很可怕,哈哈,心裡有點畏懼.看來基礎的東西,那怕一點點小細節也很重要,接著還有下面的java IO 問題.剛才讀core java 的時候突然發現在講io的時候特意提醒了這個問題,可是我沒有注意!

通過上面的代碼已經實現文件上傳了.然後,我們要實現JDBC數據源鏈接,目的是要把數據插入到oracle.

Context ctx=new InitialContext();

DataSource ds=(DataSource)ctx.lookup("jdbc/asdbCoreDS");

conn=ds.getConnection();

conn.setAutoCommit(false);

關於要import java.sql.* javax.sql.* java.naming.* 不再詳細敘述了

接著根據很有用的一篇文章的提示,插入blob類型一定要先1.插入一個空的

String insert=" insert into uploadpicture "+

" values(?, empty_blob()) " ;

2.然後找到這個blob的oracle 裡面的游標:

String findCursor=" select content "+

" from uploadpicture "+

" where name=? for update ";

注意這個for update(注意!!!必須加for update,這將鎖定該行,直至該行被修改完畢,保證不產生並發沖突。這里還是難以理解,先記下來吧)

3.然後再修改

String update=" update uploadpicture "+

" set content=? "+

" where name=? ";

這里的問號是為PreparedStatement參數處理而寫的!

寫這個程序用到了oracle.sql.BLOB class ,這個類是用來操作BLOB數據類型的

當我們通過ResultSet 對象得到

blob=(BLOB)rs.getBlob(1);

的時候我不知道如何處理了,Blob 是什麼?String, int ,long? 我現在也不明白!估計CSDN上的人也不明白,否則我發個帖子半天沒有人回答,也許是很爛,也許是太簡單了,大家不屑一顧,看來我還要繼續追趕!

不發牢騷了,回到程序里(總覺得自己的發散思維很強,看來寫程序的時候不能這樣,多虧java 是純面向對象語言,如果是過程就麻煩了)

我們如何處理這個blob 呢?回答是,不管它是什麼,直接寫入 BufferedOutputStream out1 =new BufferedOutputStream(blob.getBinaryOutputStream());

這里是建立了緩沖寫如blob 的流(注意getBinaryOutputStream()已經不被贊成使用了,一定有更優秀的方法替代!),說到流,我到現在還有點暈,類很多,不知道究竟用哪個好!

基礎的東西非常重要,這曾經是我的口頭禪,這里用到了流的讀入寫和寫入,有些流是從文件或其它位置上讀取位元組(如, FileInputStream),有寫流是把位元組組合成有用的數據(如, DataInputStream).我們讀取數字的時候,需要首先建議一個FileInpuStream, 然後, 再把該類的對象傳遞給DataInputStream

FileInputStream fin=new FileInputStream(「emp.dat」);

DataInputStream din=new DataInputStream(fin);//把fin傳遞給din

double s=din.readDouble();

默認情況下,流是沒有緩沖的, 如果使用緩沖就是

DataInputStream din=new DataInputStream(

new BufferedInputStream(new FileINputStream(「emp.dat」)));

有了這點理解也很管用,

BufferedOutputStream out1 =new BufferedOutputStream(blob.getBinaryOutputStream());

就是建立一個緩沖寫的對象到blob.注意這里的out1 不是out,否則程序運行的時候不能列印了temp 數據了!

已經准備好如何寫了, 可是如何讀呢?

BufferedInputStream in=new BufferedInputStream(is);

在我們上傳的時候 (InputStream)is=fi.getInputStream();

讀取圖片為輸入的流.保存為is 對象,然後就用到這里了,准備好了讀和寫了,我們開始幹活:

int c;

while((c=in.read())!=-1) {out1.write(c);}

in.close();

out1.close();

通過緩沖一個個讀數據,然後一個個寫數據.-1 為文件的末尾,

最後當讀寫完成後我們要關閉讀寫對象!

程序分析就是這樣,以後還要對此問題進行研究,最後還要注意,

<%@ page contentType="image/jpeg;charset=GBK"%>

不是

<%@ page contentType="text/html;charset=GBK"%>

否則是以文字顯示圖片---亂碼.

這里研究了上傳圖片到oralce 裡面的程序,關於顯示還要麻煩一點,藉助資料我實現了,明天再研究一下.

//插入上傳圖片到資料庫
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.apache.commons.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="oracle.sql.*"%>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>getPicture.jsp</title>
</head>

<body>

<%
request.setCharacterEncoding("GBK");

String name=null;
long size=0;

Connection conn=null;
String insert=" insert into uploadpicture "+
" values(?, empty_blob()) " ;

String findCursor=" select content "+
" from uploadpicture "+
" where name=? for update ";

String update=" update uploadpicture "+
" set content=? "+
" where name=? ";
BLOB blob=null;
InputStream is=null;

DiskFileUpload dfu=new DiskFileUpload();
dfu.setSizeMax(100000000);
dfu.setSizeThreshold(100000);
dfu.setRepositoryPath("f:\\public");

try{
List fileItems=dfu.parseRequest(request);
Iterator i=fileItems.iterator();

while(i.hasNext()){
FileItem fi=(FileItem)i.next();
if(!fi.isFormField()){
name=fi.getName();
size=fi.getSize();
if((name==null||name.equals(""))&&size==0)
continue;
}
name=fi.getName();
size=fi.getSize();
is=fi.getInputStream();

}

Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("jdbc/asdbCoreDS");
conn=ds.getConnection();
conn.setAutoCommit(false);

//step 1
PreparedStatement ps=conn.prepareStatement(insert);
ps.setString(1, name);
int a=ps.executeUpdate();
if(a>0)
out.println("insert success!"+"<br>");

//step 2
ps=conn.prepareStatement(findCursor);
ps.setString(1, name);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
blob=(BLOB)rs.getBlob(1);

out.println("find cursor success!"+"<br>");
out.println("cursor :"+blob+"<br>");
//step 3
ps=conn.prepareStatement(update);
ps.setBlob(1, blob);
ps.setString(2, name);
ps.executeUpdate();
ps.close();
BufferedOutputStream out1 =new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in=new BufferedInputStream(is);
int c;
while((c=in.read())!=-1) {out1.write(c);}
in.close();
out1.close();
out.println("update success!"+"<br>");}
conn.commit();
}

catch(SQLException se)
{se.printStackTrace();}
catch(FileUploadException fue)
{fue.printStackTrace();}
%>

</body>

</html>

//顯示資料庫裡面的圖片

<%@ page contentType="image/jpeg;charset=GBK"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="java.io.*"%>
<%@ page import="com.sun.image.codec.jpeg.*"%>
<%@ page import="javax.imageio.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.awt.image.*"%>

<html>

<head>
<meta http-equiv="Content-Type" content="image/jpeg; charset=GBK">
<title>showDBImage.jsp</title>
</head>

<body>
<%
String showImage=" select * "+
" from uploadpicture "+
" where name=´TXC with snow.JPG´ " ;
Connection conn=null;
BufferedInputStream inputImage=null;

try{
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("jdbc/asdbCoreDS");
conn=ds.getConnection();
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery(showImage);
while(rs.next())
{
oracle.sql.BLOB blob=(oracle.sql.BLOB)rs.getBlob("content");
inputImage =new BufferedInputStream(blob.getBinaryStream());
/*String name=rs.getString(1);
String content=rs.getString(2);
out.println(name+"<br>");*/}

BufferedImage image=null;
image=ImageIO.read(inputImage);

ServletOutputStream sos=response.getOutputStream();
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
inputImage.close();
conn.commit();

}
catch(SQLException se)
{se.printStackTrace();
conn.rollback(); }
catch(IOException ie)
{ie.printStackTrace();}
%>

</body>

</html>

❼ Java中如何使用Struts2實現將圖片以二進制流的形式存儲到Oracle資料庫,不使用JDBC

我寫了個例子,你可以參考下,保存圖片形式是二進制import java.io.*;
import java.sql.*;
public class FileImage1{
public void insert(String name,File file)
{
Connection con=null;
try {
con=DBhelper.getCon();
String sql="insert into addImage values(?,?)";
PreparedStatement ps=con.prepareStatement(sql);
BufferedInputStream input=new BufferedInputStream(new FileInputStream(file));//可緩沖位元組流
//InputStream input=new FileInputStream(file);
int k=input.available();
ps.setString(1,name);
ps.setBinaryStream(2, input, k);
int result=ps.executeUpdate();
if(result>0)
System.out.println("保存成功");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}public static void main(String[] args) {
new FileImage1().insert("張遠",new File("D:\\圖片\\lin.jpg"));
}
}

❽ 如何用Java把圖片插入到oracle資料庫

用oracle的blob數據類型存儲,jdbc調用setBinaryStream(index,inputstream,inputstream.available
())存進去即可

❾ Java操作oracle存入圖片,為什麼存不進,進入SQL/PLUS,select count(*) from imgic;結果是0,郁悶

你的程序應該是沒問題的,看看資料庫是不是有問題?

閱讀全文

與javaoracle圖片相關的資料

熱點內容
數學奇跡神奇運演算法 瀏覽:359
大廠的程序員的水平如何 瀏覽:700
遺傳演算法入門經典書籍 瀏覽:878
源碼炮台腳本 瀏覽:620
在位編輯命令 瀏覽:347
曲式分析基礎教程pdf 瀏覽:14
php生成靜態html頁面 瀏覽:964
怎麼分割pdf 瀏覽:812
壓縮垃圾報警器 瀏覽:629
小公司一般都用什麼伺服器 瀏覽:968
java獲取時間gmt時間 瀏覽:820
為什麼csgo一直連接不到伺服器 瀏覽:504
安卓登ins需要什麼 瀏覽:836
機器人演算法的難點 瀏覽:226
全自動化編程 瀏覽:727
程序員高薪限制 瀏覽:693
壓縮圖片壓縮 瀏覽:75
美國發明解壓魔方 瀏覽:302
電腦怎麼備案網上伺服器 瀏覽:515
旅行商問題Python寫法 瀏覽:953