zoukankan      html  css  js  c++  java
  • MyEclipse------从MySQL取出图片

    showImage.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
    %>
    <%@page import="java.sql.*" %>
    <%@page import="java.util.*" %>
    <%@page import="java.text.*" %>
    <%@page import="java.io.*" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>">
    <title>My JSP 'showImage.jsp' starting page</title>
    </head>
    
    <body>
        <%
            String url="jdbc:mysql://localhost:3306/student?useSSL=true";
            String useName="root";
            String password="2277092";
            String sql="select binfile from bindata where name='01'";
            
            try{
                Class.forName("com.mysql.jdbc.Driver");
            }
            catch(Exception e){
                out.print(e);
            }
            
            Connection conn=null;//连接数据库
            Statement stmt=null;//执行SQL语句
            ResultSet rs=null;//取得结果集
            
            conn=DriverManager.getConnection(url,useName,password);
            
            try{
                stmt=conn.createStatement();
                rs=stmt.executeQuery(sql);
            }catch(SQLException e){
                out.print(e);
            }
            
            try{
                while(rs.next()){
                    response.setContentType("image/jpeg");//设置返回给客户端的内容的类型
                    //给客户端提供一个输出二进制的输出流
                    ServletOutputStream sout=response.getOutputStream();
                    
                    InputStream in=rs.getBinaryStream(1);
                    byte b[]=new byte[0x7a120];
                    for(int i=in.read(b);i!=-1;){
                        sout.write(b);
                        in.read();
                    }
                    sout.flush();
                    sout.close();
                }
                out.clear();
                out=pageContext.pushBody();
            }
            catch(Exception e){
                out.print(e);
            }
         %>
    </body>
    </html>

    MySQL上传图片类型设置

  • 相关阅读:
    七. 多线程编程3.主线程
    七. 多线程编程1.线程的概念
    六. 异常处理12.断言
    liunx 安装 mysql 5.6
    idea Unable to open debugger port (127.0.0.1:58006) Address already in use: JVM_Bind 的解决办法
    liunx 安装redis 4.0
    liunx 安装jdk1.8
    idea 去除xml文件sql语句背景色
    改变数据库和表编码
    mybatis 按in 函数参数顺序排序
  • 原文地址:https://www.cnblogs.com/tianhengblogs/p/5334350.html
Copyright © 2011-2022 走看看