File file = new File("D:/JavaEE/zsck/WebRoot/ma.jpg"); InputStream is = new FileInputStream(file); byte[] buf=new byte[(int)file.length()]; is.read(buf);//图片数据 PreparedStatement psm = con.prepareStatement("update tip set picture=? where id='1'"); psm.setBytes(1, buf);//插入图片 psm.executeUpdate(); is.close(); <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*" %> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> <%@ page import="java.io.*"%> <html> <body> <% String sql = "select picture from tip where id='1'"; String driverName = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/shujuku"; String username = "root"; String password = "1234"; ResultSet rs; try{ Class.forName(driverName); Connection conn = DriverManager.getConnection(url,username,password); PreparedStatement stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while(rs.next()) { response.setContentType("image/jpg"); ServletOutputStream sout = response.getOutputStream(); InputStream in = rs.getBinaryStream(1); byte b[] = new byte[0x70120]; int i; while((i = in.read(b))!=-1) { sout.write(b); in.read(b); } sout.flush(); sout.close(); in.close(); } }catch(SQLException e){} %> </body> </html>