利用JSP存取图片,数据库采用mysql转载_MySQL
时间:2021-07-01 10:21:17
帮助过:21人阅读
Java代码
- 一、数据库端操作:
- 1 在mysql下建一个数据库名字叫 testpic
- ===>
- mysql>create database testpic;
-
- 2 在testpic库下建一数据表test,只有两字段
- ===>
- mysql>use testpic;
-
- ===>
- 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代码
-
-
-
-
- Insert title here
-
-
-
-
-
-
-
Insert title here
**********************************************************************************************
readblob.jsp界面源码
Java代码
- <%@ 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();
-
- }
-
-
- %>
-
-
<%@ 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代码
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ page import="java.sql.*" %>
- <%@ page import="java.util.*"%>
- <%@ page import="java.text.*"%>
- <%@ page import="java.io.*"%>
-
-
-
-
- Insert title here
-
-
- <%
- String id=request.getParameter("id");
- String file=request.getParameter("file");
- out.print(id);
- out.print(file);
- FileInputStream str=new FileInputStream(file);
- out.print(str.available());
- java.sql.Connection conn;
- java.lang.String strConn;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/testpic","root","root");
- String sql="insert into test(id,pic) values(?,?)";
- PreparedStatement pstmt=conn.prepareStatement(sql);
- pstmt.setString(1,id);
- pstmt.setBinaryStream(2,str,str.available());
- pstmt.execute();
- out.println("Success,You Have Insert an Image Successfully");
- pstmt.close();
- %>
- 查看图片
- 返回
-
-