概述
开发环境:MyEclipse6.0
1、第一步创建一个web工程,配置好SSH环境。
2、建立实体类 Tfile.java
package model;
public class Tfile ...{
private String fileId;
private String fileName;
private byte[] fileContent;
private String remark;
public String getFileId() ...{
return fileId;
}
public void setFileId(String fileId) ...{
this.fileId = fileId;
}
public String getFileName() ...{
return fileName;
}
public void setFileName(String fileName) ...{
this.fileName = fileName;
}
public byte[] getFileContent() ...{
return fileContent;
}
public void setFileContent(byte[] fileContent) ...{
this.fileContent = fileContent;
}
public String getRemark() ...{
return remark;
}
public void setRemark(String remark) ...{
this.remark = remark;
}
}
3、完成OR映射文件:Tfile.hbm.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class table="t_file" name="model.Tfile">
<id name="fileId" column="FILE_ID">
<generator class="uuid"/>
</id>
<property name="fileName" column="FILE_NAME"/>
<property name="remark" column="FILE_REMARK"/>
<property name="fileContent"
type="org.springframework.orm.hibernate3.support.BlobByteArrayType"
column="FILE_CONTENT" lazy="true"
>
</property>
</class>
</hibernate-mapping>
4、完成Hibernate配置文件:hibernate.cfg.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/fileupload</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="model/Tfile.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5、建立 FileManager.java
package manager;
import model.Tfile;
public interface FileManger ...{
/** *//**
* 保存上传的文件
* @param tfile
*/
public void save(Tfile tfile);
}
6、实现保存文件功能:FileManagerImpl.java
package manager;
import model.Tfile;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class FileManagerImpl extends HibernateDaoSupport implements FileManger ...{
@Override
public void save(Tfile tfile) ...{
getHibernateTemplate().save(tfile);
}
}
7、编写相对应的Action和ActionFrom
FileAcion.java
package web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import manager.FileManger;
import model.Tfile;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class FileAction extends Action ...{
private FileManger fileManager;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception ...{
FileActionForm faf=(FileActionForm)form;
Tfile tfile =new Tfile();
tfile.setRemark(faf.getRamark());
tfile.setFileContent(faf.getMyfile().getFileData());
tfile.setFileName(faf.getMyfile().getFileName());
fileManager.save(tfile);
return mapping.findForward("success");
}
public void setFileManager(FileManger fileManager) ...{
this.fileManager = fileManager;
}
}
FileActionForm.java
package web;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class FileActionForm extends ActionForm ...{
private FormFile myfile;
private String ramark;
public FormFile getMyfile() ...{
return myfile;
}
public void setMyfile(FormFile myfile) ...{
this.myfile = myfile;
}
public String getRamark() ...{
return ramark;
}
public void setRamark(String ramark) ...{
this.ramark = ramark;
}
}
8、配置Spring文件:
applicationContext-actions.xml:主要是配置Action
applicationContext-beans.xml:Bean配置
applicationContext-common.xml:这个文件主要是配置SessionFactory和事务管理
1、applicationContext-actions.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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean name="/upload" class="web.FileAction" scope="prototype">
<property name="fileManager" ref="fileManager"></property>
</bean>
</beans>
2、applicationContext-beans.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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="fileManager" class="manager.FileManagerImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
3、applicationContext-common.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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- lobHandler -->
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
<!-- lobHandel 关于这个bean,请大家Google查看起说明,这里就不解释了,因为本身我也不是太了解 -->
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="lobHandler">
<ref bean="lobHandler"/>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置事务传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置哪些类的方法进行事务管理 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution (* manager.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
</beans>
9、struts-config.xml文件的配置:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="fileForm" type="web.FileActionForm"></form-bean>
</form-beans>
<action-mappings>
<!-- upload begin -->
<action path="/upload"
type="org.springframework.web.struts.DelegatingActionProxy"
name="fileForm"
scope="request"
>
<forward name="success" path="/index.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="MessageResources" />
</struts-config>
10、最后一步是上传文件页面
<form action="upload.do" method="post" enctype="multipart/form-data">
文件说明:<input type="text" name="remark"/><br>
文件:<input type="file" name="myfile" /><br>
<input type="submit" value="保存"/>
</form>
上传成功之后,我直接返回了index.jsp,即文件上传页面.想看到底有没有上传成功,去数据库查看即可.
文章出处:http://www.diybl.com/course/3_program/java/javashl/2008510/115182.html
最后
以上就是苹果帅哥为你收集整理的Struts+Spring+Hibernate环境下文件上传下载示例(上传)的全部内容,希望文章能够帮你解决Struts+Spring+Hibernate环境下文件上传下载示例(上传)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复