当前位置:Gxlcms > mysql > 利用JSP存取图片,数据库采用mysql转载_MySQL

利用JSP存取图片,数据库采用mysql转载_MySQL

时间:2021-07-01 10:21:17 帮助过:21人阅读

Java代码 收藏代码
  1. 一、数据库端操作:
  2. 1 在mysql下建一个数据库名字叫 testpic
  3. ===>
  4. mysql>create database testpic;
  5. 2 在testpic库下建一数据表test,只有两字段
  6. ===>
  7. mysql>use testpic;
  8. ===>
  9. mysql>create table test (id int, pic blob);
一、数据库端操作:1 在mysql下建一个数据库名字叫 testpic     ===> mysql>create database testpic;2 在testpic库下建一数据表test,只有两字段 ===> mysql>use testpic;                                          ===> mysql>create table test (id int, pic blob);

二、相关的html jsp文件
**********************************************************************************************
登录界面 postblob.html
Java代码 收藏代码
  1. Insert title here
Insert title here

**********************************************************************************************
readblob.jsp界面源码
Java代码 收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ page import="java.sql.*, javax.sql.*" %>
  4. <%@ page import="java.util.*"%>
  5. <%@ page import="java.text.*"%>
  6. <%@ page import="java.io.*"%> 
  7. Insert title here
  8. <%
  9. java.sql.Connection conn;
  10. ResultSet rs=null;
  11. Class.forName("com.mysql.jdbc.Driver").newInstance();
  12. conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/testpic","root","root");
  13. Statement stmt=conn.createStatement();
  14. rs=stmt.executeQuery("select * from test where id=1");
  15. if(rs.next())
  16. {
  17. Blob b = rs.getBlob("pic");
  18. int size =(int)b.length();
  19. out.print(size);
  20. InputStream in=b.getBinaryStream();
  21. byte[] by= new byte[size];
  22. response.setContentType("image/jpeg");
  23. ServletOutputStream sos = response.getOutputStream();
  24. int bytesRead = 0;
  25. while ((bytesRead = in.read(by)) != -1) {
  26. sos.write(by, 0, bytesRead);
  27. }
  28. in.close();
  29. sos.flush();
  30. }
  31. %>
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>    <%@ page import="java.sql.*, javax.sql.*" %><%@ page import="java.util.*"%><%@ page import="java.text.*"%><%@ page import="java.io.*"%> Insert title here<%  java.sql.Connection conn; ResultSet rs=null;  Class.forName("com.mysql.jdbc.Driver").newInstance();    conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/testpic","root","root");    Statement stmt=conn.createStatement();    rs=stmt.executeQuery("select * from test where id=1");  if(rs.next())  {    Blob b = rs.getBlob("pic");    int size =(int)b.length();      out.print(size);  InputStream in=b.getBinaryStream();  byte[] by= new byte[size];  response.setContentType("image/jpeg");   ServletOutputStream sos = response.getOutputStream();     int bytesRead = 0;       while ((bytesRead = in.read(by)) != -1) {             sos.write(by, 0, bytesRead);          }         in.close();         sos.flush();      }   %>


**********************************************************************************************
testblob.jsp界面源码
Java代码 收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ page import="java.sql.*" %>
  4. <%@ page import="java.util.*"%>
  5. <%@ page import="java.text.*"%>
  6. <%@ page import="java.io.*"%> 
  7. Insert title here
  8. <%
  9. String id=request.getParameter("id");
  10. String file=request.getParameter("file");
  11. out.print(id);
  12. out.print(file);
  13. FileInputStream str=new FileInputStream(file);
  14. out.print(str.available());
  15. java.sql.Connection conn;
  16. java.lang.String strConn;
  17. Class.forName("com.mysql.jdbc.Driver").newInstance();
  18. conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/testpic","root","root");
  19. String sql="insert into test(id,pic) values(?,?)";
  20. PreparedStatement pstmt=conn.prepareStatement(sql);
  21. pstmt.setString(1,id);
  22. pstmt.setBinaryStream(2,str,str.available());
  23. pstmt.execute();
  24. out.println("Success,You Have Insert an Image Successfully");
  25. pstmt.close();
  26. %>
  27. 查看图片
  28. 返回

人气教程排行