- <%@ 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对象 */
- blob = (oracle.sql.BLOB)rs.getBlob("imag");
- /* 向BLOB对象中写入数据 */
- 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>
JSP Oracle BLOB 图片保存
最新推荐文章于 2021-04-03 04:17:38 发布