日期:2014-05-16 浏览次数:20585 次
转的啊
?
jsp上传图片到Oracle数据库????
? 第一步:建立数据库????
? create?? table?? test_img(id?? number(4),name?? varchar(20),pic?? long?? raw);????
???
? 第二步:(NewImg.html)????
???
? <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.setBinaryStream(3,fis,fis.available());????
? ps.executeUpdate();????
? //ps.execute();????
? ps.close();????
? fis.close();????
? out.println("ok!!!");????
? %>????
???
? 第四步:显示图片(ShowImg.jsp)????
???
? <%@?? page?? language="java"?? import="java.sql.*"%>????
? <%@?? page?? import="java.io.*"%>????
? <%@?? page?? contentType="text/html;charset=gb2312"%>????
???
? <%????
? Class.forName("oracle.jdbc.driver.OracleDriver");????
? String?? url="jdbc:oracle:thin:@lubin:1521:b2bdb";????
? String?? image_id?? =?? (String)?? request.getParameter("ID");????
???
? Connection?? con=DriverManager.getConnection(url,"demo","demo");????
? PreparedStatement?? sql=con.prepareStatement("select?? *?? from?? test_img?? WHERE?? id?? =?? "?? +?? image_id);????
? ResultSet?? rs?? =?? sql.executeQuery();????
???
? rs.next();????
? //输入img字段内容到in????
? InputStream?? in?? =?? rs.getBinaryStream("img");????
???
? //以下可是任何处理,比如向页面输出:????
???
? response.reset();????
? //此处的image/jpeg似乎与显示的图片格式无关????
? //因为我显示jsp和gif都没有错误????
? response.setContentType("image/jpeg");????
? byte[]?? b?? =?? new?? byte[1024];????
? int?? len;????
? while((len=in.read(b))?? >0)????
? response.getOutputStream().write(b,0,len);????
???
? in.close();????
? rs.close();????
? %>??
本篇文章来自<A href='http://www.soidc.net'>IDC专家网</a> 原文链接:http://www.soidc.net/discuss/1/031008/11/339889_1.html