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代码
- nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-
-
-
-
Insert title here
-
-
-
-
-
-
-
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><center><form action="testblob.jsp" method="post"><table width="291" border="1"> <tr> <td width="107">id </td> <td width="168"><input name="id" type="text"></td> </tr> <tr> <td>file</td> <td><input name="file" type="file"></td> </tr> <tr> <td><input type="submit" value="提交"></td> </tr>
</table></form></center>
**********************************************************************************************
readblob.jsp界面源码
Java代码
-
- pageEncoding="UTF-8"%>
-
-
-
-
- nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-
-
-
-
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();
-
- }
-
-
- %>
-
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title>
**********************************************************************************************
testblob.jsp界面源码
Java代码
-
- pageEncoding="UTF-8"%>
-
-
-
-
- nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-
-
-
-
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();
- %>
-
查看图片
-
返回
-
-
Stellungnahme:Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn