主要用于上传图片或文件:
流程:首先在add.jsp中选择要上传的文件或图片,点击确定.Struts中的ActionForm验证,验证无误就到Action类中继续操作,Action类调用PhotoDao类的addPhoto方法保存图片
1,数据库表
create table( id int primary key auto_increment, name varchar(20), photo blob ) 2,add.jsp页面代码<%@ page language="java" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <html> <head> <title>JSP for AddForm form</title> </head> <body> <html:form action="/add" enctype="multipart/form-data"> 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>
3,JAVABEAN:photo代码如下
package com.hp.entity; import java.sql.Blob; public class Photo implements java.io.Serializable { private Integer id; private String name;
//注意photo的类型 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; } }
//PhotoDao代码如下
package com.dao; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import com.hp.entity.Photo; import com.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); tx.commit(); } public Photo getByName(String name){ Query query=session.createQuery("from Photo p where p.name = ?"); query.setString(0,name); return (Photo) query.uniqueResult(); } public void close(){ if(session.isOpen()){ session.close(); session=null; } } }
5,Photo的Hiberante表映射文件
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.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="10" not-null="true" /> </property> <!--注意下面的类型--> <property name="photo" type="java.sql.Blob"> <column name="photo" /> </property> </class> </hibernate-mapping> 6,struts的ActionForm代码
package com.hp.struts.form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.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; } }
7,struts的Action代码package com.hp.struts.action; 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.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; import org.hibernate.Hibernate; import com.dao.*; import com.hp.entity.Photo; import com.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")); } } 8,用于显示插入图片的页面show.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html:html lang="true"> <head> <html:base /> <title>show.jsp</title> </head> <body> <html:img src="${photo}"/> </body> </html:html>