利用JSP存取图片,数据库采用mysql转载_MySQL
程序员文章站
2022-06-09 17:56:14
...
Java代码
二、相关的html jsp文件
**********************************************************************************************
登录界面 postblob.html
Java代码
**********************************************************************************************
readblob.jsp界面源码
Java代码
**********************************************************************************************
testblob.jsp界面源码
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 id file
Insert title here
**********************************************************************************************
readblob.jsp界面源码
Java代码
- pageEncoding="UTF-8"%>
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();
- }
- %>
Insert title here
**********************************************************************************************
testblob.jsp界面源码
Java代码
- pageEncoding="UTF-8"%>
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();
- %>
- 查看图片
- 返回
-