JSP Oracle BLOB 圖檔儲存
- <%@?page?contentType="text/html;image/jpg;charset=gb2312"?language="java"?import="java.sql.*"?errorPage=""?%>
- <html>
- <head>
- <meta?http-equiv="Content-Type"?content="text/html;?charset=gb2312">
- <title>瀵煎?ュ?劇??</title>
- </head>
- <body>
- <h2>瀵煎?ュ?劇??锛?</h2>
- <%@?page?import="java.io.*"%>?
- <%@?page?import="oracle.sql.BLOB"%>?
- <%
- try
- {
- ????Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
- ????int?imgId=1;
- ??????
- ????Statement?st?=?null;
- ????
- ????BLOB?blob?=?null;?//?劇??绫誨??
- ????OutputStream?outputStream?=?null;?//杈??烘?
- ????File?file?=?null;?//??浠?
- ????FileInputStream?inputStream?=null;
- ????file?=?new?File("i://tomcat.gif");?//??浠?
- ????inputStream?=?new?FileInputStream(file);?//杈??ユ?
- ????ResultSet?rs?=?null;
- ????
- ????String?url="jdbc:oracle:thin:@localhost:1521:ORCL";
- ????Connection?conn?=DriverManager.getConnection(url,"system","oracle");
- ????conn.setAutoCommit(false);?//浜??╃?辯?搴?????浣?
- ????st?=?conn.createStatement();
- ????rs?=?st.executeQuery("select?nvl(max(id),0)?as?maxid?from?hr.psn");
- ????if?(rs.next())
- ????{
- ????????imgId+=rs.getInt("maxid");
- ????}
- ????rs.close();
- ????st.executeQuery("insert?into?hr.psn?values("+?imgId?+",'"+file.length()+"',empty_blob())");
- ????rs?=?st.executeQuery("select?imag?from?hr.psn?where?id="+?imgId?+"?for?update");
- ????if?(rs.next())?
- ????{
- ????????
- ????????blob?=?(oracle.sql.BLOB)rs.getBlob("imag");
- ????????
- ????????OutputStream?outt?=?blob.getBinaryOutputStream();
- ????????BufferedInputStream?in?=?new?BufferedInputStream(new?FileInputStream(file));
- ????????int?c;
- ????????while?((c=in.read())!=-1)
- ????????{
- ???????????outt.write(c);
- ????????}
- ????????in.close();
- ????????outt.close();
- ????}
- ????
- ????conn.commit();??????????????
- ????
- ????rs.close();
- ????st.close();
- ????conn.close();
- }
- catch?(Exception?e)
- {
- ????e.printStackTrace();
- }
- %>
- </body>
- </html>
100) ? false:true" x-data="topBtn" @click="scrolltoTop" x-cloak>