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>