<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的時候經(jīng)常遇到的問題就是員工圖片檔案的儲存問題,解決這個問題有兩個方法,

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

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

    2.就是上傳到數(shù)據(jù)庫里面(oracle).關(guān)于oracle 數(shù)據(jù)庫,它支持blob, 和clob, 分別對應(yīng)著圖片和文本(長字符串)操作

    由于性能原因,我們還是要采用第二種方法,而且存到數(shù)據(jù)庫里面比較容易管理,是吧?

    ?

    首先,我們要解決上傳問題,這里采用普遍使用的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服務(wù)器接受上傳的代碼,參考文件已經(jīng)在我上篇寫的上傳文本文件里給出,今天,終于想明白了:

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

    原來是轉(zhuǎn)義字符也就是說\n\t等而要打印反斜杠要用\\,其實這個問題原先已經(jīng)知道,可是由于經(jīng)驗沒有寫過圖片上傳處理什么的,覺得很高深,也很可怕,哈哈,心里有點(diǎn)畏懼.看來基礎(chǔ)的東西,那怕一點(diǎn)點(diǎn)小細(xì)節(jié)也很重要,接著還有下面的java IO 問題.剛才讀core java 的時候突然發(fā)現(xiàn)在講io的時候特意提醒了這個問題,可是我沒有注意!

    ?

    通過上面的代碼已經(jīng)實現(xiàn)文件上傳了.然后,我們要實現(xiàn)JDBC數(shù)據(jù)源鏈接,目的是要把數(shù)據(jù)插入到oracle.

    ??????????????????

    ? Context ctx=new InitialContext();

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

    ? conn=ds.getConnection();

    ? conn.setAutoCommit(false);

    關(guān)于要import java.sql.* javax.sql.* java.naming.* 不再詳細(xì)敘述了

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

    ? String insert=" insert into uploadpicture "+

    ??????????????? " values(?, empty_blob()) "???? ;

    2.然后找到這個blob的oracle 里面的游標(biāo):

    String findCursor=" select content "+

    ??????????????????? " from uploadpicture "+

    ??????????????????? " where name=?? for update ";

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

    3.然后再修改

    String update=" update uploadpicture? "+

    ??????????????? " set content=? "+

    ??????????????? " where name=? ";

    這里的問號是為PreparedStatement參數(shù)處理而寫的!

    ?

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

    當(dāng)我們通過ResultSet 對象得到

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

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

    不發(fā)牢騷了,回到程序里(總覺得自己的發(fā)散思維很強(qiáng),看來寫程序的時候不能這樣,多虧java 是純面向?qū)ο笳Z言,如果是過程就麻煩了)

    我們?nèi)绾翁幚磉@個blob 呢?回答是,不管它是什么,直接寫入? BufferedOutputStream out1 =new BufferedOutputStream(blob.getBinaryOutputStream());

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

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

    FileInputStream fin=new FileInputStream(“emp.dat”);

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

    double s=din.readDouble();

    默認(rèn)情況下,流是沒有緩沖的, 如果使用緩沖就是

    DataInputStream din=new DataInputStream(

    new BufferedInputStream(new FileINputStream(“emp.dat”)));

    ?

    有了這點(diǎn)理解也很管用,

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

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

    已經(jīng)準(zhǔn)備好如何寫了, 可是如何讀呢?

    BufferedInputStream in=new BufferedInputStream(is);

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

    讀取圖片為輸入的流.保存為is 對象,然后就用到這里了,準(zhǔn)備好了讀和寫了,我們開始干活:

    int c;

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

    ? in.close();

    ? out1.close();

    通過緩沖一個個讀數(shù)據(jù),然后一個個寫數(shù)據(jù).-1 為文件的末尾,

    最后當(dāng)讀寫完成后我們要關(guān)閉讀寫對象!

    程序分析就是這樣,以后還要對此問題進(jìn)行研究,最后還要注意,

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

    不是

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

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

    ?

    這里研究了上傳圖片到oralce 里面的程序,關(guān)于顯示還要麻煩一點(diǎn),借助資料我實現(xiàn)了,明天再研究一下.

    ?

    ?//插入上傳圖片到數(shù)據(jù)庫
    <%@ 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>


    //顯示數(shù)據(jù)庫里面的圖片

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


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产最新凸凹视频免费| 日韩电影免费观看| 成年轻人网站色免费看| 国产免费AV片在线观看播放| 国产精品免费看久久久久| 亚洲影院天堂中文av色| 在线观看免费人成视频| 亚洲福利精品电影在线观看| 亚洲av无码片vr一区二区三区| 我们的2018在线观看免费高清| 亚洲成a人片在线网站| 青娱乐免费在线视频| 亚洲 日韩经典 中文字幕| 性感美女视频在线观看免费精品 | 免费手机在线看片| 亚洲AV无码成H人在线观看 | 浮力影院亚洲国产第一页| 中文字幕永久免费| 亚洲综合在线视频| 国产精品亚洲综合| 亚洲人成电影网站国产精品 | 18禁男女爽爽爽午夜网站免费| 国产大片线上免费看| 一级毛片在线完整免费观看| 国产亚洲美女精品久久久久狼| 日本免费一区二区三区 | a级毛片高清免费视频| 色婷婷亚洲十月十月色天 | 亚洲AⅤ无码一区二区三区在线 | 日韩毛片在线免费观看| 亚洲男人的天堂www| 4hu四虎最新免费地址| 久久久久久亚洲精品| 无码日韩精品一区二区免费| 国产精品亚洲天堂| 久久国产亚洲观看| 国产片免费福利片永久| 久久免费观看国产精品88av| 亚洲av无码有乱码在线观看| 亚洲午夜未满十八勿入网站2| 国产男女爽爽爽爽爽免费视频|