流程:首先在add
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
)
Html代碼
<%@ page language=
<%@ taglib uri=
<%@ taglib uri=
<html>
<head>
<title>JSP for AddForm form</title>
</head>
<body>
<html:form action=
name : <html:text property=
photo : <html:file property=
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
view plaincopy to clipboardprint?
<%@ page language=
<%@ taglib uri=
<%@ taglib uri=
<html>
<head>
<title>JSP for AddForm form</title>
</head>
<body>
<html:form action=
name : <html:text property=
photo : <html:file property=
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
<%@ page language=
<%@ taglib uri=
<%@ taglib uri=
<html>
<head>
<title>JSP for AddForm form</title>
</head>
<body>
<html:form action=
name : <html:text property=
photo : <html:file property=
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
Java代碼
package com
import java
public class Photo implements java
private Integer id;
private String name;
private Blob photo;
public Photo() {
}
public Photo(String name) {
this
}
public Photo(String name
this
this
}
public Integer getId() {
return this
}
public void setId(Integer id) {
this
}
public String getName() {
return this
}
public void setName(String name) {
this
}
public Blob getPhoto() {
return photo;
}
public void setPhoto(Blob photo) {
this
}
}
view plaincopy to clipboardprint?
package com
import java
public class Photo implements java
private Integer id;
private String name;
private Blob photo;
public Photo() {
}
public Photo(String name) {
this
}
public Photo(String name
this
this
}
public Integer getId() {
return this
}
public void setId(Integer id) {
this
}
public String getName() {
return this
}
public void setName(String name) {
this
}
public Blob getPhoto() {
return photo;
}
public void setPhoto(Blob photo) {
this
}
}
package com
import java
public class Photo implements java
private Integer id;
private String name;
private Blob photo;
public Photo() {
}
public Photo(String name) {
this
}
public Photo(String name
this
this
}
public Integer getId() {
return this
}
public void setId(Integer id) {
this
}
public String getName() {
return this
}
public void setName(String name) {
this
}
public Blob getPhoto() {
return photo;
}
public void setPhoto(Blob photo) {
this
}
}
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<!
Mapping file autogenerated by MyEclipse Persistence Tools
<Hibernate
<class name=
<id name=
<column name=
<generator class=
</id>
<property name=
<column name=
</property>
<!
<property name=
<column name=
</property>
</class>
</Hibernate
view plaincopy to clipboardprint?
<?xml version=
<!DOCTYPE <A title=Hibernate 基金&amp;q=基金&amp;sbb=搜索&amp;sa=搜索&amp;client=pub
<!
Mapping file autogenerated by MyEclipse Persistence Tools
<hibernate
<class name=
<id name=
<column name=
<generator class=
</id>
<property name=
<column name=
</property>
<!
<property name=
<column name=
</property>
</class>
</hibernate
<?xml version=
<!DOCTYPE Hibernate
<!
Mapping file autogenerated by MyEclipse Persistence Tools
<hibernate
<class name=
<id name=
<column name=
<generator class=
</id>
<property name=
<column name=
</property>
<!
<property name=
<column name=
</property>
</class>
</hibernate
Java代碼
import org
import org
import org
import com
import com
public class PhotoDao {
private Session session=null;
public PhotoDao(){
this
}
public void addPhoto(Photo photo){
Transaction tx=session
session
mit();
}
public Photo getByName(String name){
Query query=session
query
return (Photo) query
}
public void close(){
if(session
session
session=null;
}
}
}
import org
import org
import org
import com
import com
public class PhotoDao {
private Session session=null;
public PhotoDao(){
this
}
public void addPhoto(Photo photo){
Transaction tx=session
session
mit();
}
public Photo getByName(String name){
Query query=session
query
return (Photo) query
}
public void close(){
if(session
session
session=null;
}
}
}
import org
import org
import org
import com
import com
public class PhotoDao {
private Session session=null;
public PhotoDao(){
this
}
public void addPhoto(Photo photo){
Transaction tx=session
session
mit();
}
public Photo getByName(String name){
Query query=session
query
return (Photo) query
}
public void close(){
if(session
session
session=null;
}
}
}
Java代碼
import javax
import org
import org
import org
import org
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
}
public FormFile getPhoto() {
return photo;
}
public void setPhoto(FormFile photo) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
}
import javax
import org
import org
import org
import org
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
}
public FormFile getPhoto() {
return photo;
}
public void setPhoto(FormFile photo) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
}
import javax
import org
import org
import org
import org
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
}
public FormFile getPhoto() {
return photo;
}
public void setPhoto(FormFile photo) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
}
Java代碼
import java
import java
import java
import java
import java
import java
import java
import java
import javax
import javax
import org
import org
import org
import org
import org
import org
import com
import com
import com
public class AddAction extends Action {
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
AddForm addForm = (AddForm) form;
String name = addForm
// 在Struts中上傳文件需要使用FormFile
FormFile formFile = addForm
System
System
Photo photo= new Photo();
//將FormFile類型的圖片信息轉換為字節數組類型
try {
byte[] buf= formFile
//使用Hibernate的工具將字節數組轉換成Blob對象
Blob ph= Hibernate
photo
photo
PhotoDao photoDao = new PhotoDao();
photoDao
//以上為插入圖片
Photo temp = photoDao
//將BLOB對象轉化為輸入流的形式
InputStream in=temp
//先在C盤生成temp文件
OutputStream os = new FileOutputStream(new File(
//把輸入流中的數據讀入buf數組中
in
//從buf數組中讀出數據
os
in
os
request
photoDao
} catch (Exception e) {
e
}
return mapping
}
public static void main(String[] args) {
System
}
}
import java
import java
import java
import java
import java
import java
import java
import java
import javax
import javax
import org
import org
import org
import org
import org
import org
import com
import com
import com
public class AddAction extends Action {
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
AddForm addForm = (AddForm) form;
String name = addForm
// 在Struts中上傳文件需要使用FormFile
FormFile formFile = addForm
<A title=system target=_blank>system</A>
<A title=system target=_blank>system</A>
Photo photo= new Photo();
//將FormFile類型的圖片信息轉換為字節數組類型
try {
byte[] buf= formFile
//使用Hibernate的工具將字節數組轉換成Blob對象
Blob ph= <A title=Hibernate 基金&amp;q=基金&amp;sbb=搜索&amp;sa=搜索&amp;client=pub
photo
photo
PhotoDao photoDao = new PhotoDao();
photoDao
//以上為插入圖片
Photo temp = photoDao
//將BLOB對象轉化為輸入流的形式
InputStream in=temp
//先在C盤生成temp文件
OutputStream os = new FileOutputStream(new File(
//把輸入流中的數據讀入buf數組中
in
//從buf數組中讀出數據
os
in
os
request
photoDao
} catch (Exception e) {
e
}
return mapping
}
public static void main(String[] args) {
<A title=system target=_blank>system</A>
}
}
import java
import java
import java
import java
import java
import java
import java
import java
import javax
import javax
import org
import org
import org
import org
import org
import org
import com
import com
import com
public class AddAction extends Action {
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
AddForm addForm = (AddForm) form;
String name = addForm
// 在Struts中上傳文件需要使用FormFile
FormFile formFile = addForm
system
system
Photo photo= new Photo();
//將FormFile類型的圖片信息轉換為字節數組類型
try {
byte[] buf= formFile
//使用Hibernate的工具將字節數組轉換成Blob對象
Blob ph= Hibernate
photo
photo
PhotoDao photoDao = new PhotoDao();
photoDao
//以上為插入圖片
Photo temp = photoDao
//將BLOB對象轉化為輸入流的形式
InputStream in=temp
//先在C盤生成temp文件
OutputStream os = new FileOutputStream(new File(
//把輸入流中的數據讀入buf數組中
in
//從buf數組中讀出數據
os
in
os
request
photoDao
} catch (Exception e) {
e
}
return mapping
}
public static void main(String[] args) {
system
}
}
Html代碼
<%@ page language=
<%@ taglib uri=
<%@ taglib uri=
<%@ taglib uri=
<%@ taglib uri=
<!DOCTYPE HTML PUBLIC
<html:html lang=
<head>
<html:base />
<title>show
</head>
<body>
<html:img src=
</body>
</html:html>
view plaincopy to clipboardprint?
<%@ page language=
<%@ taglib uri=
<%@ taglib uri=
<%@ taglib uri=
<%@ taglib uri=
<!DOCTYPE HTML PUBLIC
<html:html lang=
<head>
<html:base />
<title>show
</head>
<body>
<html:img src=
</body>
</html:html>
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27966.html