0

0

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

php中文网

php中文网

发布时间:2016-06-01 13:51:29

|

1181人浏览过

|

来源于php中文网

原创

Java代码 复制代码 收藏代码利用JSP存取图片,数据库采用mysql转载_MySQL

  1. 一、数据库端操作:   
  2. 1 在mysql下建一个数据库名字叫 testpic        
  3. ===>    
  4. mysql>create database testpic;   
  5.   
  6. 2 在testpic库下建一数据表test,只有两字段    
  7. ===>    
  8. mysql>use testpic;   
  9.                                              
  10. ===>    
  11. 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代码 复制代码 收藏代码利用JSP存取图片,数据库采用mysql转载_MySQL
  1. nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  2.   
  3.   
  4.   
  5. Insert title here   
  6.   
  7.   
  8.   
  9.   
  10.   
  11.   
  12.   
  13.     
  14.   
  15.     
  16.   
  17.   
  18.   
  19.   
  20.   
  21.     
  22.   
  23.     
  24.   
  25.   
  26.   
  27.   
  28.   
  29.     
  30.   
  31.       
  32.   
  33.   
  34. id 
    file
      
  35.   
  36.   
  37.   
  38.   
Insert title here
id
file

**********************************************************************************************
  readblob.jsp界面源码
Java代码 复制代码 收藏代码利用JSP存取图片,数据库采用mysql转载_MySQL
  1. java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>   
  3.        
  4.   
  5.   
  6.     
  7. nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  8.   
  9.   
  10.   
  11. Insert title here   
  12.   
  13.   
  14.     
  15.  java.sql.Connection conn;   
  16.  ResultSet rs=null;   
  17.   Class.forName("com.mysql.jdbc.Driver").newInstance();    
  18.    conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/testpic","root","root");    
  19.    Statement stmt=conn.createStatement();    
  20.    rs=stmt.executeQuery("select * from test where id=1");   
  21.   if(rs.next())   
  22.   {   
  23.     Blob b = rs.getBlob("pic");   
  24.       
  25.  int size =(int)b.length();   
  26.       out.print(size);   
  27.   InputStream in=b.getBinaryStream();   
  28.   byte[] by= new byte[size];   
  29.   response.setContentType("image/jpeg");    
  30.   ServletOutputStream sos = response.getOutputStream();   
  31.      int bytesRead = 0;   
  32.        while ((bytesRead = in.read(by)) != -1) {   
  33.              sos.write(by, 0, bytesRead);   
  34.           }   
  35.          in.close();   
  36.          sos.flush();   
  37.        
  38.   }   
  39.      
  40.     
  41. %>   
  42.   
  43.   
<%@ 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代码 复制代码 收藏代码利用JSP存取图片,数据库采用mysql转载_MySQL
  1.     pageEncoding="UTF-8"%>   
  2.        
  3.   
  4.   
  5.     
  6. nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  7.   
  8.   
  9.   
  10. Insert title here   
  11.   
  12.   
  13.  String id=request.getParameter("id");   
  14.  String file=request.getParameter("file");   
  15.  out.print(id);   
  16.  out.print(file);   
  17.  FileInputStream str=new FileInputStream(file);   
  18.  out.print(str.available());   
  19.    java.sql.Connection conn;    
  20.    java.lang.String strConn;    
  21.    Class.forName("com.mysql.jdbc.Driver").newInstance();    
  22.    conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/testpic","root","root");    
  23.  String sql="insert into test(id,pic) values(?,?)";    
  24.  PreparedStatement pstmt=conn.prepareStatement(sql);    
  25.  pstmt.setString(1,id);   
  26.  pstmt.setBinaryStream(2,str,str.available());    
  27. pstmt.execute();    
  28. out.println("Success,You Have Insert an Image Successfully");   
  29.  pstmt.close();   
  30. %>    
  31. 查看图片   
  32. 返回   
  33.   
  34.  

相关专题

更多
虚拟号码教程汇总
虚拟号码教程汇总

本专题整合了虚拟号码接收验证码相关教程,阅读下面的文章了解更多详细操作。

29

2025.12.25

错误代码dns_probe_possible
错误代码dns_probe_possible

本专题整合了电脑无法打开网页显示错误代码dns_probe_possible解决方法,阅读专题下面的文章了解更多处理方案。

20

2025.12.25

网页undefined啥意思
网页undefined啥意思

本专题整合了undefined相关内容,阅读下面的文章了解更多详细内容。后续继续更新。

37

2025.12.25

word转换成ppt教程大全
word转换成ppt教程大全

本专题整合了word转换成ppt教程,阅读专题下面的文章了解更多详细操作。

6

2025.12.25

msvcp140.dll丢失相关教程
msvcp140.dll丢失相关教程

本专题整合了msvcp140.dll丢失相关解决方法,阅读专题下面的文章了解更多详细操作。

2

2025.12.25

笔记本电脑卡反应很慢处理方法汇总
笔记本电脑卡反应很慢处理方法汇总

本专题整合了笔记本电脑卡反应慢解决方法,阅读专题下面的文章了解更多详细内容。

6

2025.12.25

微信调黑色模式教程
微信调黑色模式教程

本专题整合了微信调黑色模式教程,阅读下面的文章了解更多详细内容。

5

2025.12.25

ps入门教程
ps入门教程

本专题整合了ps相关教程,阅读下面的文章了解更多详细内容。

4

2025.12.25

苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

218

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 6万人学习

Django 教程
Django 教程

共28课时 | 2.5万人学习

Excel 教程
Excel 教程

共162课时 | 9.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号