testblob.jsp

来自「初学者 在mysql数据库中保存一张图片的方法 使用jsp+mysql h」· JSP 代码 · 共 44 行

JSP
44
字号
<%@ page contentType="text/html;charset=gb2312"%> 
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>将提交的图片插入数据库的页面</title>
</head>
<body>
<%
 request.setCharacterEncoding("gbk");
 String id=request.getParameter("id");
 String file=request.getParameter("file");
 System.out.print(id);
 System.out.print(file);
 File fileName = new File(file);
 long l = (int)fileName.length();
 
 FileInputStream str=new FileInputStream(fileName);
 
 

 Class.forName("com.mysql.jdbc.Driver").newInstance();
 Connection  conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/testblob","root","1234");
 
 
 
 System.out.println("测试执行位置");
 
 
 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();
 System.out.println("Success,You Have Insert an Image Successfully");
 pstmt.close();
%>
<a href="readblob.jsp">查看图片</a>
<a href="postblob.html">返回</a>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?