jsp mysql 插入 读取 图片
来源:互联网 发布:拍淘宝女装用什么镜头 编辑:程序博客网 时间:2024/05/16 17:54
建立数据库:
我的数据库images
create table img (
id int primary key auto_increment,
name varchar(80),
pic longblob
)
要保证网站根目录 有个 images 文件夹
插入数据库 从本地文件夹
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.io.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<%
String path=application.getRealPath("/"); //取得网扎根目录
File fr=new File(path+"images"); //得到inages文件夹下面的所有的文件
String []names=fr.list();//得到所有的文件名称
//设置URL和驱动
String url="jdbc:mysql://localhost:3306/images";
String Drivers="com.mysql.jdbc.Driver";
//练级数据库 准备SLQ语句
Class.forName(Drivers);
String SQL="insert into img(name ,pic) values(?,?)";
Connection conn=DriverManager.getConnection(url,"root","root");
PreparedStatement pstmt=conn.prepareStatement(SQL);
try{
for(int k=0;k<names.length;k++)
//生成输入流对象
InputStream fin=new FileInputStream(path+"images//"+names[k].toString());
//赋值
pstmt.setString(1,names[k]);
pstmt.setBinaryStream(2,fin);
//执行
pstmt.execute();
}
}catch(SQLException e)
{
out.println(e.toString());
}
conn.close();
out.println("Success");
%>
</body>
</html>
读取图片 输出到 文件夹里面 images
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.io.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<%
String path=application.getRealPath("/");
String URL="jdbc:mysql://localhost:3306/images";
String drivers="com.mysql.jdbc.Driver";
String sql="select * from img";
Class.forName(drivers);
Connection conn=DriverManager.getConnection(URL,"root","root");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
int i=0;String names;
while(rs.next())
{
byte img[]=new byte[1024];
names=rs.getString(2);
InputStream in=rs.getBinaryStream(3);
OutputStream outS=new FileOutputStream(path+"images//"+names);
while((i=in.read(img))!=-1)
outS.write(img);
}
%>
<% out.println("suc"); %>
</body>
</html>
从数据看读取数据 输出到网页
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.io.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<%
//
if(request.getProtocol().compareTo("HTTP/1.0")==0)
{
response.setHeader("Pragma", "no-cache");
}
else if(request.getProtocol().compareTo("HTTP/1.1")==0)
{
response.setHeader("Cache-Control", "no-cache");
}
String path=application.getRealPath("/");
String URL="jdbc:mysql://localhost:3306/images";
String drivers="com.mysql.jdbc.Driver";
String sql="select * from img";
Class.forName(drivers);
Connection conn=DriverManager.getConnection(URL,"root","root");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
int i=0;String names;
while(rs.next())
{
byte img[]=new byte[1024];
names=rs.getString(2);
InputStream in=rs.getBinaryStream(3);
OutputStream outS=new FileOutputStream(path+"images//"+names);
while((i=in.read(img))>0)
outS.write(img);
%>
<div style="width:80;height:80; overflow:hidden"><img src="images/<%=names%>"/><div>
<%
}
%>
</body>
</html>
- jsp mysql 插入 读取 图片
- 向MySQL中插入读取图片
- 从MySQL数据库读取图片和向数据库插入图片
- 向mysql数据库插入与读取图片文件
- 把mysql读取的图片显示在jsp 页面上
- Jsp读取Mysql数据库
- HBase插入和读取图片
- JSP文件中插入图片
- mysql插入图片
- mysql插入图片数据
- Mysql插入图片
- JSP Oracle BLOB 图片读取
- hibernate+mysql图片读取
- JSP 插入中文 mysql 乱码
- mysql jsp 图片
- 从sql server 2000中读取image字段图片(blob),并插入到jsp中相应的页面
- java读取mysql中保存的图片并显示在jsp上
- MySql:jsp插入mysql数据库中文乱码
- 好久没写东西了.
- 概要设计说明书样本及解释
- SAP 高级应用开发 学习笔记 1 -- RFC NO.2
- SQL Server之分布式事务
- 关于java中的构造方法
- jsp mysql 插入 读取 图片
- 遍历文件夹下所有文件(递归调用)
- 1633 Moo Volume解题报告
- Java手机开发平台 J2ME
- 客户关系管理之会员管理(转)
- SEO中的十大关键词竞争度分析方法
- nfs服务器的搭建
- 游戏拍卖行出现的几点影响
- Reading Notes on NS2(1)