日期:2014-05-16 浏览次数:20356 次
create table image(id int,content varchar(200),image blob);
<HTML> <HEAD> <TITLE>Image File </TITLE> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </HEAD> <FORM METHOD=POST ACTION="testimage.jsp"> <INPUT TYPE="text" NAME="content"><BR> <INPUT TYPE="file" NAME="image"><BR> <INPUT TYPE="submit"></FORM> <BODY> </BODY> </HTML>
<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*" %> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> <%@ page import="java.io.*"%> <html> <body> <%Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url="jdbc:mysql://localhost/mysql?user=root&password=&useUnicode=true&characterEncoding=8859_1"; //其中mysql为你数据库的名字,user为你连接数据库的用户,password为你连接数据库用户的密码,可自己改 Connection conn= DriverManager.getConnection(url); String content=request.getParameter("content"); String filename=request.getParameter("image"); FileInputStream str=new FileInputStream(filename); String sql="insert into test(id,content,image) values(1,?,?)"; PreparedStatement pstmt=dbconn.conn.prepareStatement(sql); pstmt.setString(1,content); pstmt.setBinaryStream(2,str,str.available()); pstmt.execute(); out.println("Success,You Have Insert an Image Successfully"); %>
<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*" %> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> <%@ page import="java.io.*"%> <html> <body> <%Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url="jdbc:mysql://localhost/mysql?user=root&password=&useUnicode=true&characterEncoding=8859_1"; //其中mysql为你数据库的名字,user为你连接数据库的用户,password为你连接数据库用户的密码,可自己改 Connection conn= DriverManager.getConnection(url); String sql = "select image from test where id=1"; Statement stmt=null; ResultSet rs=null; try{ stmt=conn.createStatement(); rs=stmt.executeQuery(sql); }catch(SQLException e){} try { while(rs.next()) { res.setContentType("image/jpeg"); ServletOutputStream sout = response.getOutputStream(); InputStream in = rs.getBinaryStream(1); byte b[] = new byte[0x7a120]; for(int i = in.read(b); i != -1;) { sout.write(b); in.read(b); } sout.flush(); sout.close(); } } catch(Exception e){System.out.println(e);} %> </body> </html>