知识大全 使用Struts+Hibernate上传大对象(BLOB)
Posted 类型
篇首语:科学的自负比起无知的自负来还只能算是谦虚。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 使用Struts+Hibernate上传大对象(BLOB)相关的知识,希望对你有一定的参考价值。
使用Struts+Hibernate上传大对象(BLOB) 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
流程:首先在add jsp中选择要上传的文件或图片 点击确定 Struts中的ActionForm验证 验证无误就到Action类中继续操作 Action类调用PhotoDao类的addPhoto方法保存图片
:建表
Sql代码 create table( id int primary key auto_increment name varchar( ) photo blob ) create <A title=table target=_blank>table</A>( id int primary key auto_increment name varchar( ) photo blob )
create table( id int primary key auto_increment name varchar( ) photo blob )
add jsp页面代码Html代码 <%@ page language= java pageEncoding= ISO %> <%@ taglib uri= bean prefix= bean %> <%@ taglib uri= prefix= %>
<> <head> <title>JSP for AddForm form</title> </head> <body> <:form action= /add enctype= multipart/form data > name : <:text property= name /><:errors property= name /><br/> photo : <:file property= photo /><:errors property= photo /><br/> <:submit/><:cancel/> </:form> </body> </> view plaincopy to clipboardprint? <%@ page language= java pageEncoding= ISO %> <%@ taglib uri= bean prefix= bean %> <%@ taglib uri= prefix= %>
<> <head> <title>JSP for AddForm form</title> </head> <body> <:form action= /add enctype= multipart/form data > name : <:text property= name /><:errors property= name /><br/> photo : <:file property= photo /><:errors property= photo /><br/> <:submit/><:cancel/> </:form> </body> </>
<%@ page language= java pageEncoding= ISO %> <%@ taglib uri= bean prefix= bean %> <%@ taglib uri= prefix= %>
<> <head> <title>JSP for AddForm form</title> </head> <body> <:form action= /add enctype= multipart/form data > name : <:text property= name /><:errors property= name /><br/> photo : <:file property= photo /><:errors property= photo /><br/> <:submit/><:cancel/> </:form> </body> </> :photo(javabean)代码如下
Java代码 package hp entity;
import java sql Blob;
public class Photo implements java io Serializable
private Integer id; private String name; private Blob photo;
public Photo()
public Photo(String name) this name = name;
public Photo(String name Blob photo) this name = name; this photo = photo;
public Integer getId() return this id;
public void setId(Integer id) this id = id;
public String getName() return this name;
public void setName(String name) this name = name;
public Blob getPhoto() return photo;
public void setPhoto(Blob photo) this photo = photo;
view plaincopy to clipboardprint? package hp entity;
import java sql Blob;
public class Photo implements java io Serializable
private Integer id; private String name; private Blob photo;
public Photo()
public Photo(String name) this name = name;
public Photo(String name Blob photo) this name = name; this photo = photo;
public Integer getId() return this id;
public void setId(Integer id) this id = id;
public String getName() return this name;
public void setName(String name) this name = name;
public Blob getPhoto() return photo;
public void setPhoto(Blob photo) this photo = photo;
package hp entity;
import java sql Blob;
public class Photo implements java io Serializable
private Integer id; private String name; private Blob photo;
public Photo()
public Photo(String name) this name = name;
public Photo(String name Blob photo) this name = name; this photo = photo;
public Integer getId() return this id;
public void setId(Integer id) this id = id;
public String getName() return this name;
public void setName(String name) this name = name;
public Blob getPhoto() return photo;
public void setPhoto(Blob photo) this photo = photo;
Xml代码 <?xml version= encoding= utf ?>
<!DOCTYPE hibernate mapping PUBLIC //Hibernate/Hibernate Mapping DTD //EN
mapping dtd >
<!
Mapping file autogenerated by MyEclipse Persistence Tools
>
<Hibernate mapping>
<class name= hp entity Photo table= photo catalog= test >
<id name= id type= java lang Integer >
<column name= id />
<generator class= native />
</id>
<property name= name type= java lang String >
<column name= name length= not null= true />
</property>
<! 注意下面的类型 >
<property name= photo type= java sql Blob >
<column name= photo />
</property>
</class>
</Hibernate mapping> view plaincopy to clipboardprint? <?xml version= encoding= utf ?>
<!DOCTYPE <A title=Hibernate 基金&amp;q=基金&amp;sbb=搜索&amp;sa=搜索&amp;client=pub &amp;forid= &amp;prog=aff&amp;ie=GB &amp;oe=GB &amp;hl=zh CN target=_blank>Hibernate</A> mapping PUBLIC //Hibernate/Hibernate Mapping DTD //EN
mapping dtd >
<!
Mapping file autogenerated by MyEclipse Persistence Tools
>
<hibernate mapping>
<class name= hp entity Photo <A title=table target=_blank>table</A>= photo catalog= test >
<id name= id type= java lang Integer >
<column name= id />
<generator class= native />
</id>
<property name= name type= java lang String >
<column name= name length= not null= true />
</property>
<! 注意下面的类型 >
<property name= photo type= java sql Blob >
<column name= photo />
</property>
</class>
</hibernate mapping>
<?xml version= encoding= utf ?>
<!DOCTYPE Hibernate mapping PUBLIC //Hibernate/Hibernate Mapping DTD //EN
mapping dtd >
<!
Mapping file autogenerated by MyEclipse Persistence Tools
>
<hibernate mapping>
<class name= hp entity Photo table= photo catalog= test >
<id name= id type= java lang Integer >
<column name= id />
<generator class= native />
</id>
<property name= name type= java lang String >
<column name= name length= not null= true />
</property>
<! 注意下面的类型 >
<property name= photo type= java sql Blob >
<column name= photo />
</property>
</class>
</hibernate mapping> :PhotoDao代码如下
Java代码 import hibernate Query;
import hibernate Session;
import hibernate Transaction;
import hp entity Photo;
import hp util *;
public class PhotoDao
private Session session=null;
public PhotoDao()
this session = HibernateSessionFactory getSession();
public void addPhoto(Photo photo)
Transaction tx=session beginTransaction();
session saveOrUpdate(photo);
mit();
public Photo getByName(String name)
Query query=session createQuery( from Photo p where p name = ? );
query setString( name);
return (Photo) query uniqueResult();
public void close()
if(session isOpen())
session close();
session=null;
import hibernate Query;
import hibernate Session;
import hibernate Transaction;
import hp entity Photo;
import hp util *;
public class PhotoDao
private Session session=null;
public PhotoDao()
this session = HibernateSessionFactory getSession();
public void addPhoto(Photo photo)
Transaction tx=session beginTransaction();
session saveOrUpdate(photo);
mit();
public Photo getByName(String name)
Query query=session createQuery( from Photo p where p name = ? );
query setString( name);
return (Photo) query uniqueResult();
public void close()
if(session isOpen())
session close();
session=null;
import hibernate Query;
import hibernate Session;
import hibernate Transaction;
import hp entity Photo;
import hp util *;
public class PhotoDao
private Session session=null;
public PhotoDao()
this session = HibernateSessionFactory getSession();
public void addPhoto(Photo photo)
Transaction tx=session beginTransaction();
session saveOrUpdate(photo);
mit();
public Photo getByName(String name)
Query query=session createQuery( from Photo p where p name = ? );
query setString( name);
return (Photo) query uniqueResult();
public void close()
if(session isOpen())
session close();
session=null;
:struts的ActionForm代码
Java代码 import javax servlet HttpServletRequest;
import apache struts action ActionErrors;
import apache struts action ActionForm;
import apache struts action ActionMapping;
import apache struts upload FormFile;
public class AddForm extends ActionForm
//在Struts中上传文件需要使用FormFile类型
private FormFile photo; private String name;
public ActionErrors validate(ActionMapping mapping
HttpServletRequest request)
return null;
public void reset(ActionMapping mapping HttpServletRequest request)
public FormFile getPhoto()
return photo;
public void setPhoto(FormFile photo)
this photo = photo;
public String getName()
return name;
public void setName(String name)
this name = name;
import javax servlet HttpServletRequest;
import apache struts action ActionErrors;
import apache struts action ActionForm;
import apache struts action ActionMapping;
import apache struts upload FormFile;
public class AddForm extends ActionForm
//在Struts中上传文件需要使用FormFile类型
private FormFile photo; private String name;
public ActionErrors validate(ActionMapping mapping
HttpServletRequest request)
return null;
public void reset(ActionMapping mapping HttpServletRequest request)
public FormFile getPhoto()
return photo;
public void setPhoto(FormFile photo)
this photo = photo;
public String getName()
return name;
public void setName(String name)
this name = name;
import javax servlet HttpServletRequest;
import apache struts action ActionErrors;
import apache struts action ActionForm;
import apache struts action ActionMapping;
import apache struts upload FormFile;
public class AddForm extends ActionForm
//在Struts中上传文件需要使用FormFile类型
private FormFile photo; private String name;
public ActionErrors validate(ActionMapping mapping
HttpServletRequest request)
return null;
public void reset(ActionMapping mapping HttpServletRequest request)
public FormFile getPhoto()
return photo;
public void setPhoto(FormFile photo)
this photo = photo;
public String getName()
return name;
public void setName(String name)
this name = name;
:struts的Action代码
Java代码 import java io File;
import java io FileInputStream;
import java io FileNotFoundException;
import java io FileOutputStream;
import java io IOException;
import java io InputStream;
import java io OutputStream;
import java sql Blob;
import javax servlet HttpServletRequest;
import javax servlet HttpServletResponse;
import apache struts action Action;
import apache struts action ActionForm;
import apache struts action ActionForward;
import apache struts action ActionMapping;
import apache struts upload FormFile;
import hibernate Hibernate;
import dao *;
import hp entity Photo;
import hp struts form AddForm;
public class AddAction extends Action
public ActionForward execute(ActionMapping mapping ActionForm form
HttpServletRequest request HttpServletResponse response)
AddForm addForm = (AddForm) form;
String name = addForm getName();
// 在Struts中上传文件需要使用FormFile
FormFile formFile = addForm getPhoto();
System out println( name = + name);
System out println( size = + formFile getFileSize());
Photo photo= new Photo();
//将FormFile类型的图片信息转换为字节数组类型
try
byte[] buf= formFile getFileData();
//使用Hibernate的工具将字节数组转换成Blob对象
Blob ph= Hibernate createBlob(buf);
photo setName(name);
photo setPhoto(ph);
PhotoDao photoDao = new PhotoDao();
photoDao addPhoto(photo);
//以上为插入图片 下面为读取刚才插入的图片 按name查找图片
Photo temp = photoDao getByName(name);
//将BLOB对象转化为输入流的形式
InputStream in=temp getPhoto() getBinaryStream();
//先在C盘生成temp文件 用于保存图片
OutputStream os = new FileOutputStream(new File( c://temp jpg ));
//把输入流中的数据读入buf数组中
in read(buf);
//从buf数组中读出数据
os write(buf);
in close();
os close();
request getSession() setAttribute( photo c://temp jpg );
photoDao close();
catch (Exception e)
e printStackTrace();
return mapping findForward( success );
public static void main(String[] args)
System out println(System getProperty( user dir ));
import java io File;
import java io FileInputStream;
import java io FileNotFoundException;
import java io FileOutputStream;
import java io IOException;
import java io InputStream;
import java io OutputStream;
import java sql Blob;
import javax servlet HttpServletRequest;
import javax servlet HttpServletResponse;
import apache struts action Action;
import apache struts action ActionForm;
import apache struts action ActionForward;
import apache struts action ActionMapping;
import apache struts upload FormFile;
import hibernate Hibernate;
import dao *;
import hp entity Photo;
import hp struts form AddForm;
public class AddAction extends Action
public ActionForward execute(ActionMapping mapping ActionForm form
HttpServletRequest request HttpServletResponse response)
AddForm addForm = (AddForm) form;
String name = addForm getName();
// 在Struts中上传文件需要使用FormFile
FormFile formFile = addForm getPhoto();
<A title=system target=_blank>system</A> out println( name = + name);
<A title=system target=_blank>system</A> out println( size = + formFile getFileSize());
Photo photo= new Photo();
//将FormFile类型的图片信息转换为字节数组类型
try
byte[] buf= formFile getFileData();
//使用Hibernate的工具将字节数组转换成Blob对象
Blob ph= <A title=Hibernate 基金&amp;q=基金&amp;sbb=搜索&amp;sa=搜索&amp;client=pub &amp;forid= &amp;prog=aff&amp;ie=GB &amp;oe=GB &amp;hl=zh CN target=_blank>Hibernate</A> createBlob(buf);
photo setName(name);
photo setPhoto(ph);
PhotoDao photoDao = new PhotoDao();
photoDao addPhoto(photo);
//以上为插入图片 下面为读取刚才插入的图片 按name查找图片
Photo temp = photoDao getByName(name);
//将BLOB对象转化为输入流的形式
InputStream in=temp getPhoto() getBinaryStream();
//先在C盘生成temp文件 用于保存图片
OutputStream os = new FileOutputStream(new File( c://temp jpg ));
//把输入流中的数据读入buf数组中
in read(buf);
//从buf数组中读出数据
os write(buf);
in close();
os close();
request getSession() setAttribute( photo c://temp jpg );
photoDao close();
catch (Exception e)
e printStackTrace();
return mapping findForward( success );
public static void main(String[] args)
<A title=system target=_blank>system</A> out println(System getProperty( user dir ));
import java io File;
import java io FileInputStream;
import java io FileNotFoundException;
import java io FileOutputStream;
import java io IOException;
import java io InputStream;
import java io OutputStream;
import java sql Blob;
import javax servlet HttpServletRequest;
import javax servlet HttpServletResponse;
import apache struts action Action;
import apache struts action ActionForm;
import apache struts action ActionForward;
import apache struts action ActionMapping;
import apache struts upload FormFile;
import hibernate Hibernate;
import dao *;
import hp entity Photo;
import hp struts form AddForm;
public class AddAction extends Action
public ActionForward execute(ActionMapping mapping ActionForm form
HttpServletRequest request HttpServletResponse response)
AddForm addForm = (AddForm) form;
String name = addForm getName();
// 在Struts中上传文件需要使用FormFile
FormFile formFile = addForm getPhoto();
system out println( name = + name);
system out println( size = + formFile getFileSize());
Photo photo= new Photo();
//将FormFile类型的图片信息转换为字节数组类型
try
byte[] buf= formFile getFileData();
//使用Hibernate的工具将字节数组转换成Blob对象
Blob ph= Hibernate createBlob(buf);
photo setName(name);
photo setPhoto(ph);
PhotoDao photoDao = new PhotoDao();
photoDao addPhoto(photo);
//以上为插入图片 下面为读取刚才插入的图片 按name查找图片
Photo temp = photoDao getByName(name);
//将BLOB对象转化为输入流的形式
InputStream in=temp getPhoto() getBinaryStream();
//先在C盘生成temp文件 用于保存图片
OutputStream os = new FileOutputStream(new File( c://temp jpg ));
//把输入流中的数据读入buf数组中
in read(buf);
//从buf数组中读出数据
os write(buf);
in close();
os close();
request getSession() setAttribute( photo c://temp jpg );
photoDao close();
catch (Exception e)
e printStackTrace();
return mapping findForward( success );
public static void main(String[] args)
system out println(System getProperty( user dir ));
:用于显示插入图片的页面show jsp
Html代码 <%@ page language= java pageEncoding= ISO %>
<%@ taglib uri= bean prefix= bean %>
<%@ taglib uri= prefix= %>
<%@ taglib uri= logic prefix= logic %>
<%@ taglib uri= tiles prefix= tiles %>
<!DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN >
<: lang= true >
<head>
<:base />
<title>show jsp</title>
</head>
<body>
<:img src= $photo />
</body>
</:> view plaincopy to clipboardprint? <%@ page language= java pageEncoding= ISO %>
<%@ taglib uri= bean prefix= bean %>
<%@ taglib uri= prefix= %>
<%@ taglib uri= logic prefix= logic %>
<%@ taglib uri= tiles prefix= tiles %>
<!DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN >
<: lang= true >
<head>
<:base />
<title>show jsp</title>
</head>
<body>
<:img src= $photo />
</body>
</:>
cha138/Article/program/Java/ky/201311/27966相关参考
在Struts2中实现文件上传 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Struts是通过
知识大全 Struts+Spring+Hibernate快速入门
Struts+Spring+Hibernate快速入门 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧
知识大全 Struts&Spring&Hibernate面试总结
Struts&Spring&Hibernate面试总结 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧
知识大全 Struts 2, spring 2, hibernate
Struts2,spring2,hibernate 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
Struts+hibernate投票系统代码实例 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!&n
知识大全 MyEclipse+struts+Hibernate配置开发手册
MyEclipse+struts+Hibernate配置开发手册 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起
知识大全 struts2 + spring + hibernate&
struts释出已经很久了虽然自己现在作GUI开发不过有时间还是学习下web开发现在就将我使用myeclipse工具应用struts+spring+hibernat
Struts轻松搞定多文件上传 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 在前面的《Apac
让Struts与Hibernate顺利协同工作 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 安
J2EE综合:Struts上传多个文件的例子 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!&nbs