<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 119, comments - 62, trackbacks - 0, articles - 0
    我們在寫OA的時候經常遇到的問題就是員工圖片檔案的儲存問題,解決這個問題有兩個方法,

    1.JSP/html頁面里面讀取web服務器上的圖片,也就是把圖片放到(上傳)到web 服務器上,然后用html 語句讀?。?o:p>

    <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>


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 免费无遮挡无码永久视频| 777亚洲精品乱码久久久久久| 国产99视频精品免费视频76| 成人免费无毒在线观看网站| 日韩一区二区在线免费观看| 亚洲精品二三区伊人久久| 亚洲最大免费视频网| 亚洲精品国产啊女成拍色拍| 99久久精品免费视频| 亚洲欧洲高清有无| 久久久久久国产a免费观看黄色大片 | 成人国产精品免费视频| 国产精品亚洲美女久久久| 曰韩无码AV片免费播放不卡| 亚洲老妈激情一区二区三区| 国精产品一区一区三区免费视频| 亚洲成色999久久网站| 亚洲爆乳精品无码一区二区| 四虎永久在线精品免费观看地址| 边摸边脱吃奶边高潮视频免费| 亚洲日韩精品无码专区网站 | 久久亚洲精品无码VA大香大香| 88xx成人永久免费观看| 亚洲AV无码乱码在线观看代蜜桃 | 亚洲国产成人无码av在线播放| 男人的好看免费观看在线视频| 亚洲AV无码国产精品永久一区| 亚洲中文字幕无码专区| 久久99精品国产免费观看| 亚洲激情电影在线| 男人的天堂亚洲一区二区三区 | 一区二区三区免费电影| 亚洲码国产精品高潮在线| 99久久99久久免费精品小说| 亚洲中文字幕乱码AV波多JI| 亚洲熟女乱综合一区二区| 国产高清不卡免费在线| 成人免费网站久久久| 亚洲啪啪免费视频| 中文字幕不卡亚洲| 免费无码午夜福利片69|