Topic: jsp如何把图片和媒体文件存入oracle中 |
Print this page |
1.jsp如何把图片和媒体文件存入oracle中 | Copy to clipboard |
Posted by: chengbd Posted on: 2004-06-21 10:54 第一步:建立数据库 create table test_img( id number(4), name varchar(20), img long raw); 第二步: <html><head><title>添加图片</title></head> <body> 添加图片<br> <form method="post" action="insertNews.jsp"> 图像ID:<input name="id" size="10"> <br> 选择图像:<input type="file" name="image"> <br> <input type="submit" value="上传" name="submit" size="25"> <input type="reset" value="清除" name="clear" size="25"> <br> </form> </body></html> 第三步:插入数据库(InsertImg.jsp) <%@ page language="java"%> <%@ page contentType="text/html;charset=gb2312" %> <%@ page import="java.util.*" %> <%@ page import="java.sql.*"%> <%@ page import="java.text.*"%> <%@ page import="java.io.*"%> <% Class.forName("oracle.jdbc.driver.OracleDriver"); String url="jdbc:oracle:thin:@lubin:1521:b2bdb"; Connection con=DriverManager.getConnection(url,"demo","demo"); //插入数据库 String sql="insert into test_img values (?,?,?)"; //获取传值ID String id=request.getParameter("id"); //获取image的路径 String kk=request.getParameter("image"); //转换成file格式 File filename=new File(kk); //将文件的长度读出,并转换成Long型 long l1=filename.length(); int l2=(int)l1; //以流的格式赋值 FileInputStream fis=new FileInputStream(filename); PreparedStatement ps =con.prepareStatement(sql); ps.setString(1,id); ps.setString(2,filename.getName()); ps.setBinaryStream(3,fis,l2); ps.executeUpdate(); ps.close(); fis.close(); 看不懂请提出来 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |