java

位置:IT落伍者 >> java >> 浏览文章

使用Struts+Hibernate上传大对象(BLOB)


发布日期:2024年08月17日
 
使用Struts+Hibernate上传大对象(BLOB)

流程:首先在addjsp中选择要上传的文件或图片点击确定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

)

addjsp页面代码

Html代码

<%@ page language=java pageEncoding=ISO%>

<%@ taglib uri=bean prefix=bean%>

<%@ taglib uri=html prefix=html%>

<html>

<head>

<title>JSP for AddForm form</title>

</head>

<body>

<html:form action=/add enctype=multipart/formdata>

name : <html:text property=name/><html:errors property=name/><br/>

photo : <html:file property=photo/><html:errors property=photo/><br/>

<html:submit/><html:cancel/>

</html:form>

</body>

</html>

view plaincopy to clipboardprint?

<%@ page language=java pageEncoding=ISO%>

<%@ taglib uri=bean prefix=bean%>

<%@ taglib uri=html prefix=html%>

<html>

<head>

<title>JSP for AddForm form</title>

</head>

<body>

<html:form action=/add enctype=multipart/formdata>

name : <html:text property=name/><html:errors property=name/><br/>

photo : <html:file property=photo/><html:errors property=photo/><br/>

<html:submit/><html:cancel/>

</html:form>

</body>

</html>

<%@ page language=java pageEncoding=ISO%>

<%@ taglib uri=bean prefix=bean%>

<%@ taglib uri=html prefix=html%>

<html>

<head>

<title>JSP for AddForm form</title>

</head>

<body>

<html:form action=/add enctype=multipart/formdata>

name : <html:text property=name/><html:errors property=name/><br/>

photo : <html:file property=photo/><html:errors property=photo/><br/>

<html:submit/><html:cancel/>

</html:form>

</body>

</html> :photo(javabean)代码如下

Java代码

package comhpentity;

import javasqlBlob;

public class Photo implements javaioSerializable {

private Integer id;

private String name;

private Blob photo;

public Photo() {

}

public Photo(String name) {

thisname = name;

}

public Photo(String name Blob photo) {

thisname = name;

thisphoto = photo;

}

public Integer getId() {

return thisid;

}

public void setId(Integer id) {

thisid = id;

}

public String getName() {

return thisname;

}

public void setName(String name) {

thisname = name;

}

public Blob getPhoto() {

return photo;

}

public void setPhoto(Blob photo) {

thisphoto = photo;

}

}

view plaincopy to clipboardprint?

package comhpentity;

import javasqlBlob;

public class Photo implements javaioSerializable {

private Integer id;

private String name;

private Blob photo;

public Photo() {

}

public Photo(String name) {

thisname = name;

}

public Photo(String name Blob photo) {

thisname = name;

thisphoto = photo;

}

public Integer getId() {

return thisid;

}

public void setId(Integer id) {

thisid = id;

}

public String getName() {

return thisname;

}

public void setName(String name) {

thisname = name;

}

public Blob getPhoto() {

return photo;

}

public void setPhoto(Blob photo) {

thisphoto = photo;

}

}

package comhpentity;

import javasqlBlob;

public class Photo implements javaioSerializable {

private Integer id;

private String name;

private Blob photo;

public Photo() {

}

public Photo(String name) {

thisname = name;

}

public Photo(String name Blob photo) {

thisname = name;

thisphoto = photo;

}

public Integer getId() {

return thisid;

}

public void setId(Integer id) {

thisid = id;

}

public String getName() {

return thisname;

}

public void setName(String name) {

thisname = name;

}

public Blob getPhoto() {

return photo;

}

public void setPhoto(Blob photo) {

thisphoto = photo;

}

}

Xml代码

<?xml version= encoding=utf?>

<!DOCTYPE hibernatemapping PUBLIC //Hibernate/Hibernate Mapping DTD //EN

mappingdtd>

<!

Mapping file autogenerated by MyEclipse Persistence Tools

>

<Hibernatemapping>

<class name=comhpentityPhoto table=photo catalog=test>

<id name=id type=javalangInteger>

<column name=id />

<generator class=native />

</id>

<property name=name type=javalangString>

<column name=name length= notnull=true />

</property>

<!注意下面的类型>

<property name=photo type=javasqlBlob>

<column name=photo />

</property>

</class>

</Hibernatemapping>

view plaincopy to clipboardprint?

<?xml version= encoding=utf?>

<!DOCTYPE <A title=Hibernate 基金&amp;amp;q=基金&amp;amp;sbb=搜索&amp;amp;sa=搜索&amp;amp;client=pub&amp;amp;forid=&amp;amp;prog=aff&amp;amp;ie=GB&amp;amp;oe=GB&amp;amp;hl=zhCN target=_blank>Hibernate</A>mapping PUBLIC //Hibernate/Hibernate Mapping DTD //EN

mappingdtd>

<!

Mapping file autogenerated by MyEclipse Persistence Tools

>

<hibernatemapping>

<class name=comhpentityPhoto <A title=table  target=_blank>table</A>=photo catalog=test>

<id name=id type=javalangInteger>

<column name=id />

<generator class=native />

</id>

<property name=name type=javalangString>

<column name=name length= notnull=true />

</property>

<!注意下面的类型>

<property name=photo type=javasqlBlob>

<column name=photo />

</property>

</class>

</hibernatemapping>

<?xml version= encoding=utf?>

<!DOCTYPE Hibernatemapping PUBLIC //Hibernate/Hibernate Mapping DTD //EN

mappingdtd>

<!

Mapping file autogenerated by MyEclipse Persistence Tools

>

<hibernatemapping>

<class name=comhpentityPhoto table=photo catalog=test>

<id name=id type=javalangInteger>

<column name=id />

<generator class=native />

</id>

<property name=name type=javalangString>

<column name=name length= notnull=true />

</property>

<!注意下面的类型>

<property name=photo type=javasqlBlob>

<column name=photo />

</property>

</class>

</hibernatemapping> :PhotoDao代码如下

Java代码

import orghibernateQuery;

import orghibernateSession;

import orghibernateTransaction;

import comhpentityPhoto;

import comhputil*;

public class PhotoDao {

private Session session=null;

public PhotoDao(){

thissession = HibernateSessionFactorygetSession();

}

public void addPhoto(Photo photo){

Transaction tx=sessionbeginTransaction();

sessionsaveOrUpdate(photo);

mit();

}

public Photo getByName(String name){

Query query=sessioncreateQuery(from Photo p where pname = ?);

querysetString(name);

return (Photo) queryuniqueResult();

}

public void close(){

if(sessionisOpen()){

sessionclose();

session=null;

}

}

}

import orghibernateQuery;

import orghibernateSession;

import orghibernateTransaction;

import comhpentityPhoto;

import comhputil*;

public class PhotoDao {

private Session session=null;

public PhotoDao(){

thissession = HibernateSessionFactorygetSession();

}

public void addPhoto(Photo photo){

Transaction tx=sessionbeginTransaction();

sessionsaveOrUpdate(photo);

mit();

}

public Photo getByName(String name){

Query query=sessioncreateQuery(from Photo p where pname = ?);

querysetString(name);

return (Photo) queryuniqueResult();

}

public void close(){

if(sessionisOpen()){

sessionclose();

session=null;

}

}

}

import orghibernateQuery;

import orghibernateSession;

import orghibernateTransaction;

import comhpentityPhoto;

import comhputil*;

public class PhotoDao {

private Session session=null;

public PhotoDao(){

thissession = HibernateSessionFactorygetSession();

}

public void addPhoto(Photo photo){

Transaction tx=sessionbeginTransaction();

sessionsaveOrUpdate(photo);

mit();

}

public Photo getByName(String name){

Query query=sessioncreateQuery(from Photo p where pname = ?);

querysetString(name);

return (Photo) queryuniqueResult();

}

public void close(){

if(sessionisOpen()){

sessionclose();

session=null;

}

}

}

:struts的ActionForm代码

Java代码

import javaxservlethttpHttpServletRequest;

import orgapachestrutsactionActionErrors;

import orgapachestrutsactionActionForm;

import orgapachestrutsactionActionMapping;

import orgapachestrutsuploadFormFile;

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) {

thisphoto = photo;

}

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

}

import javaxservlethttpHttpServletRequest;

import orgapachestrutsactionActionErrors;

import orgapachestrutsactionActionForm;

import orgapachestrutsactionActionMapping;

import orgapachestrutsuploadFormFile;

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) {

thisphoto = photo;

}

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

}

import javaxservlethttpHttpServletRequest;

import orgapachestrutsactionActionErrors;

import orgapachestrutsactionActionForm;

import orgapachestrutsactionActionMapping;

import orgapachestrutsuploadFormFile;

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) {

thisphoto = photo;

}

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

}

:struts的Action代码

Java代码

import javaioFile;

import javaioFileInputStream;

import javaioFileNotFoundException;

import javaioFileOutputStream;

import javaioIOException;

import javaioInputStream;

import javaioOutputStream;

import javasqlBlob;

import javaxservlethttpHttpServletRequest;

import javaxservlethttpHttpServletResponse;

import orgapachestrutsactionAction;

import orgapachestrutsactionActionForm;

import orgapachestrutsactionActionForward;

import orgapachestrutsactionActionMapping;

import orgapachestrutsuploadFormFile;

import orghibernateHibernate;

import comdao*;

import comhpentityPhoto;

import comhpstrutsformAddForm;

public class AddAction extends Action {

public ActionForward execute(ActionMapping mapping ActionForm form

HttpServletRequest request HttpServletResponse response) {

AddForm addForm = (AddForm) form;

String name = addFormgetName();

// 在Struts中上传文件需要使用FormFile

FormFile formFile = addFormgetPhoto();

Systemoutprintln(name = + name);

Systemoutprintln(size = + formFilegetFileSize());

Photo photo= new Photo();

//将FormFile类型的图片信息转换为字节数组类型

try {

byte[] buf= formFilegetFileData();

//使用Hibernate的工具将字节数组转换成Blob对象

Blob ph= HibernatecreateBlob(buf);

photosetName(name);

photosetPhoto(ph);

PhotoDao photoDao = new PhotoDao();

photoDaoaddPhoto(photo);

//以上为插入图片下面为读取刚才插入的图片按name查找图片

Photo temp = photoDaogetByName(name);

//将BLOB对象转化为输入流的形式

InputStream in=tempgetPhoto()getBinaryStream();

//先在C盘生成temp文件用于保存图片

OutputStream os = new FileOutputStream(new File(c://tempjpg));

//把输入流中的数据读入buf数组中

inread(buf);

//从buf数组中读出数据

oswrite(buf);

inclose();

osclose();

requestgetSession()setAttribute(photoc://tempjpg);

photoDaoclose();

} catch (Exception e) {

eprintStackTrace();

}

return mappingfindForward(success);

}

public static void main(String[] args) {

Systemoutprintln(SystemgetProperty(userdir));

}

}

import javaioFile;

import javaioFileInputStream;

import javaioFileNotFoundException;

import javaioFileOutputStream;

import javaioIOException;

import javaioInputStream;

import javaioOutputStream;

import javasqlBlob;

import javaxservlethttpHttpServletRequest;

import javaxservlethttpHttpServletResponse;

import orgapachestrutsactionAction;

import orgapachestrutsactionActionForm;

import orgapachestrutsactionActionForward;

import orgapachestrutsactionActionMapping;

import orgapachestrutsuploadFormFile;

import orghibernateHibernate;

import comdao*;

import comhpentityPhoto;

import comhpstrutsformAddForm;

public class AddAction extends Action {

public ActionForward execute(ActionMapping mapping ActionForm form

HttpServletRequest request HttpServletResponse response) {

AddForm addForm = (AddForm) form;

String name = addFormgetName();

// 在Struts中上传文件需要使用FormFile

FormFile formFile = addFormgetPhoto();

<A title=system  target=_blank>system</A>outprintln(name = + name);

<A title=system  target=_blank>system</A>outprintln(size = + formFilegetFileSize());

Photo photo= new Photo();

//将FormFile类型的图片信息转换为字节数组类型

try {

byte[] buf= formFilegetFileData();

//使用Hibernate的工具将字节数组转换成Blob对象

Blob ph= <A title=Hibernate 基金&amp;amp;q=基金&amp;amp;sbb=搜索&amp;amp;sa=搜索&amp;amp;client=pub&amp;amp;forid=&amp;amp;prog=aff&amp;amp;ie=GB&amp;amp;oe=GB&amp;amp;hl=zhCN target=_blank>Hibernate</A>createBlob(buf);

photosetName(name);

photosetPhoto(ph);

PhotoDao photoDao = new PhotoDao();

photoDaoaddPhoto(photo);

//以上为插入图片下面为读取刚才插入的图片按name查找图片

Photo temp = photoDaogetByName(name);

//将BLOB对象转化为输入流的形式

InputStream in=tempgetPhoto()getBinaryStream();

//先在C盘生成temp文件用于保存图片

OutputStream os = new FileOutputStream(new File(c://tempjpg));

//把输入流中的数据读入buf数组中

inread(buf);

//从buf数组中读出数据

oswrite(buf);

inclose();

osclose();

requestgetSession()setAttribute(photoc://tempjpg);

photoDaoclose();

} catch (Exception e) {

eprintStackTrace();

}

return mappingfindForward(success);

}

public static void main(String[] args) {

<A title=system  target=_blank>system</A>outprintln(SystemgetProperty(userdir));

}

}

import javaioFile;

import javaioFileInputStream;

import javaioFileNotFoundException;

import javaioFileOutputStream;

import javaioIOException;

import javaioInputStream;

import javaioOutputStream;

import javasqlBlob;

import javaxservlethttpHttpServletRequest;

import javaxservlethttpHttpServletResponse;

import orgapachestrutsactionAction;

import orgapachestrutsactionActionForm;

import orgapachestrutsactionActionForward;

import orgapachestrutsactionActionMapping;

import orgapachestrutsuploadFormFile;

import orghibernateHibernate;

import comdao*;

import comhpentityPhoto;

import comhpstrutsformAddForm;

public class AddAction extends Action {

public ActionForward execute(ActionMapping mapping ActionForm form

HttpServletRequest request HttpServletResponse response) {

AddForm addForm = (AddForm) form;

String name = addFormgetName();

// 在Struts中上传文件需要使用FormFile

FormFile formFile = addFormgetPhoto();

systemoutprintln(name = + name);

systemoutprintln(size = + formFilegetFileSize());

Photo photo= new Photo();

//将FormFile类型的图片信息转换为字节数组类型

try {

byte[] buf= formFilegetFileData();

//使用Hibernate的工具将字节数组转换成Blob对象

Blob ph= HibernatecreateBlob(buf);

photosetName(name);

photosetPhoto(ph);

PhotoDao photoDao = new PhotoDao();

photoDaoaddPhoto(photo);

//以上为插入图片下面为读取刚才插入的图片按name查找图片

Photo temp = photoDaogetByName(name);

//将BLOB对象转化为输入流的形式

InputStream in=tempgetPhoto()getBinaryStream();

//先在C盘生成temp文件用于保存图片

OutputStream os = new FileOutputStream(new File(c://tempjpg));

//把输入流中的数据读入buf数组中

inread(buf);

//从buf数组中读出数据

oswrite(buf);

inclose();

osclose();

requestgetSession()setAttribute(photoc://tempjpg);

photoDaoclose();

} catch (Exception e) {

eprintStackTrace();

}

return mappingfindForward(success);

}

public static void main(String[] args) {

systemoutprintln(SystemgetProperty(userdir));

}

}

:用于显示插入图片的页面showjsp

Html代码

<%@ page language=java pageEncoding=ISO%>

<%@ taglib uri=bean prefix=bean %>

<%@ taglib uri=html prefix=html %>

<%@ taglib uri=logic prefix=logic %>

<%@ taglib uri=tiles prefix=tiles %>

<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>

<html:html lang=true>

<head>

<html:base />

<title>showjsp</title>

</head>

<body>

<html:img src=${photo}/>

</body>

</html:html>

view plaincopy to clipboardprint?

<%@ page language=java pageEncoding=ISO%>

<%@ taglib uri=bean prefix=bean %>

<%@ taglib uri=html prefix=html %>

<%@ taglib uri=logic prefix=logic %>

<%@ taglib uri=tiles prefix=tiles %>

<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>

<html:html lang=true>

<head>

<html:base />

<title>showjsp</title>

</head>

<body>

<html:img src=${photo}/>

</body>

</html:html>

               

上一篇:Hibernate Annotation中BLOB、CLOB注解写法

下一篇:Spring MVC框架的高级配置(下)