求struts和hibernate实现图片上传保存到数据库中,并可以在页面显示出来,并显示上传人的名字的代码啊?
2个回答
展开全部
struts.xml中
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="sruts.il8n.encoding" value="UTF-8" />
<!--<constant name="struts.multipart.maxSize" value="1024102400" />
<constant name="struts.multipart.saveDir" value="d:/upload" /> -->
<constant name="struts.ui.theme" value="simple"></constant>
<package name="struts" extends="struts-default" namespace="/">
<action name="add2" class="DomeWeb2">
<!-- 限制图片的格式和图片的大小 -->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/pjpeg
</param>
</interceptor-ref>
<!-- 默认的拦截器,必须要写 -->
<interceptor-ref name="defaultStack" />
<result name="input">add2.jsp</result>
<result name="success">showUpload1.jsp</result>
</action> </package>
</struts>
applicationContext.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=bookshop">
</property>
<property name="username" value="sa"></property>
<property name="password" value="chen"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>entity/Dome.hbm.xml</value></list>
</property></bean>
<bean id="DomeDAO" class="dao.impl.DomeDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="DomeWeb2" class="web.UploadOneAction">
<property name="domeBiz" ref="DomeBiz"></property>
</bean>
<!-- 配置事务 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED"
read-only="true" />
<tx:method name="find*" propagation="REQUIRED"
read-only="true" />
<tx:method name="search*" propagation="REQUIRED"
read-only="true" />
<tx:method name="query*" propagation="REQUIRED"
read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="check*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceMethod"
expression="execution(* biz.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
</beans>
UploadOneAction.java类中
package web;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import biz.DomeBiz;
import com.opensymphony.xwork2.ActionSupport;
import entity.Dome;
public class UploadOneAction extends ActionSupport {
private static final long serialVersionUID = 572146812454l;
private static final int BUFFER_SIZE = 16 * 1024;
private File myFile; //文件流
private String contentType; //内容类型
private String fileName; //文件名
private String imageFileName; //新文件名
private String caption; //标题
DomeBiz domeBiz ;
public void setDomeBiz(DomeBiz domeBiz) {
this.domeBiz = domeBiz;
}
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
public String execute() {
if (myFile == null)
return INPUT;
System.out.println(this.getMyFileFileName()+"123");
imageFileName=new Date().getTime()+ getExtention(this.getMyFileFileName());
//得到图片保存的位置(根据root来得到图片保存的路径在tomcat下的该工程里)
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("images")+ "/" + imageFileName);
copy(myFile, imageFile); //把图片写入到上面设置的路径里
Dome dome = new Dome();
dome.setPicture(this.getImageFileName());
domeBiz.save(dome);
return SUCCESS;
}
//得到原文件名称
public String getMyFileFileName() {
return fileName;
}
public void setMyFileFileName(String fileName) {
this.fileName = fileName;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
}
add2.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>上传</title>
</head>
<body>
<s:form action="add2.action" method="POST"
enctype="multipart/form-data">
<s:fielderror />
<s:file name="myFile" label="Image File1" />
<s:textfield name="caption" label="Caption" />
<s:submit />
</s:form>
</body>
</html>
showUpload1.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>显示图片</title>
</head>
<body>
<div
style="padding: 3px; border: solid 1px #cccccc; text-align: center">
<img src='images/<s:property value ="imageFileName" /> ' />
<br />
<s:property value="caption" />
</div>
<s:property value="caption" />
</body>
</html>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="sruts.il8n.encoding" value="UTF-8" />
<!--<constant name="struts.multipart.maxSize" value="1024102400" />
<constant name="struts.multipart.saveDir" value="d:/upload" /> -->
<constant name="struts.ui.theme" value="simple"></constant>
<package name="struts" extends="struts-default" namespace="/">
<action name="add2" class="DomeWeb2">
<!-- 限制图片的格式和图片的大小 -->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/pjpeg
</param>
</interceptor-ref>
<!-- 默认的拦截器,必须要写 -->
<interceptor-ref name="defaultStack" />
<result name="input">add2.jsp</result>
<result name="success">showUpload1.jsp</result>
</action> </package>
</struts>
applicationContext.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=bookshop">
</property>
<property name="username" value="sa"></property>
<property name="password" value="chen"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>entity/Dome.hbm.xml</value></list>
</property></bean>
<bean id="DomeDAO" class="dao.impl.DomeDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="DomeWeb2" class="web.UploadOneAction">
<property name="domeBiz" ref="DomeBiz"></property>
</bean>
<!-- 配置事务 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED"
read-only="true" />
<tx:method name="find*" propagation="REQUIRED"
read-only="true" />
<tx:method name="search*" propagation="REQUIRED"
read-only="true" />
<tx:method name="query*" propagation="REQUIRED"
read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="check*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceMethod"
expression="execution(* biz.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
</beans>
UploadOneAction.java类中
package web;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import biz.DomeBiz;
import com.opensymphony.xwork2.ActionSupport;
import entity.Dome;
public class UploadOneAction extends ActionSupport {
private static final long serialVersionUID = 572146812454l;
private static final int BUFFER_SIZE = 16 * 1024;
private File myFile; //文件流
private String contentType; //内容类型
private String fileName; //文件名
private String imageFileName; //新文件名
private String caption; //标题
DomeBiz domeBiz ;
public void setDomeBiz(DomeBiz domeBiz) {
this.domeBiz = domeBiz;
}
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
public String execute() {
if (myFile == null)
return INPUT;
System.out.println(this.getMyFileFileName()+"123");
imageFileName=new Date().getTime()+ getExtention(this.getMyFileFileName());
//得到图片保存的位置(根据root来得到图片保存的路径在tomcat下的该工程里)
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("images")+ "/" + imageFileName);
copy(myFile, imageFile); //把图片写入到上面设置的路径里
Dome dome = new Dome();
dome.setPicture(this.getImageFileName());
domeBiz.save(dome);
return SUCCESS;
}
//得到原文件名称
public String getMyFileFileName() {
return fileName;
}
public void setMyFileFileName(String fileName) {
this.fileName = fileName;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
}
add2.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>上传</title>
</head>
<body>
<s:form action="add2.action" method="POST"
enctype="multipart/form-data">
<s:fielderror />
<s:file name="myFile" label="Image File1" />
<s:textfield name="caption" label="Caption" />
<s:submit />
</s:form>
</body>
</html>
showUpload1.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>显示图片</title>
</head>
<body>
<div
style="padding: 3px; border: solid 1px #cccccc; text-align: center">
<img src='images/<s:property value ="imageFileName" /> ' />
<br />
<s:property value="caption" />
</div>
<s:property value="caption" />
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询