JSP:上传图片到数据库并从数据库调用图片

实现将图片放入数据库,并从数据库调用图片

准备:tomcat-9.0.01      jdk9     eclipse-ide     mysql8.0      mysql-connector-java-8.0.11.jar(jdbc驱动包)   一个图片

JSP:上传图片到数据库并从数据库调用图片_第1张图片

1,建库/建表

create database bin_db;        //建库

use bin_db;

create table bindata(        //建表

filename char(255) not null,

binfile longblob,

primary key (filename)

);

2,上传图片首页面

<%@ page language="java" contentType="text/html; charset=GB2312"
	pageEncoding="GB2312"%>




Insert title here


	

请选择图片的URL:

JSP:上传图片到数据库并从数据库调用图片_第2张图片

3,上传处理页面

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %> 
<%@ page import="java.util.*"%> 
<%@ page import="java.text.*"%> 
<%@ page import="java.io.*"%>
<%@ page import="java.nio.*"%>




    
     <%
        Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); //加载JDBC驱动程序   
        String  url="jdbc:mysql:"+ "//127.0.0.1:3306/bin_dbuseSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT"; //bin_db为你的数据库的名称   
        String  user="root";    
        String  password="123456";       //密码为自己数据库的密码   
        String filename=request.getParameter("image");
         File file = new File(filename); //获取表单传过来的图片的url           
        try {
			//打开文件
			FileInputStream fin = new FileInputStream(file);
			//建一个缓冲保存数据
			ByteBuffer nbf = ByteBuffer.allocate((int) file.length());
			byte[] array = new byte[1024];
			int offset = 0, length = 0;
			//读存数据
			while ((length = fin.read(array)) > 0) {
				if (length != 1024)
					nbf.put(array, 0, length);
				else
					nbf.put(array);
				offset += length;
			}
		  //新建一个数组保存要写的内容
			byte[] content = nbf.array();
			//创建数据库连接
			Connection  conn=  DriverManager.getConnectio(url,user,password);
			//保存数据
			Statement stmt =conn.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			String sqlstr = "select * from bindata where filename='01'";
			ResultSet rs = stmt.executeQuery(sqlstr);
			if (rs.next()) 
			{
				rs.updateBytes(2, content);
				rs.updateRow();
			} else {
				rs.moveToInsertRow();
				rs.updateString(1, "01");
				rs.updateBytes(2, content);
				rs.insertRow();
			}

			rs.close();
//			关闭文件
			fin.close();
			out.println("恭喜,已经将新的记录成功地添加到数据库中!");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
   %> 

    




JSP:上传图片到数据库并从数据库调用图片_第3张图片

4,从数据库调用图片

<%@page import="java.io.*"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=GB2312"
	pageEncoding="GB2312"%>




Insert title here


	<%
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			Connection connection = DriverManager.getConnection("jdbc:mysql:"
					+ "//127.0.0.1:3306/bin_db?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT",
					"root", "123456");
			String sql = "select binfile from bindata where filename='01'";
			out.clearBuffer();
			out = pageContext.pushBody();
			Statement stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				response.setContentType("image/jpeg");
				Blob b = rs.getBlob("binfile");
				long size = b.length();
				byte bs[] = b.getBytes(1, (int) size);
				OutputStream outs = response.getOutputStream();
				outs.write(bs);
				outs.flush();
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	%>

JSP:上传图片到数据库并从数据库调用图片_第4张图片

 

你可能感兴趣的:(JAVA/JSP)