首先是上传页面(upload.jsp)
主要代码如下,其中文件字段按自己所需可以改,但在Action中也必须增减相应字段来接受,
- <s:form action="fileUploadAction" method="post" enctype="multipart/form-data">
- <s:file name="upload" label="文件名" />
- <s:textfield name="author" label="上传者" />
- <s:textfield name="description" label="文件描述" />
- <s:submit value="上传" />
- </s:form>
增加一个上传文件的上传文件功能的Action
- package edu.bjtu.resource.action;
- import com.opensymphony.xwork2.ActionSupport;
- import edu.bjtu.resource.service.ResourceService;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Date;
- /**
- *
- * @author yilee
- */
- public class FileUploadAction extends ActionSupport {
- private static final long serialVersionUID = 1L;
- // 代表上传文件的File对象
- private File upload;
- // 上传文件名
- private String uploadFileName;
- // 上传文件的MIME类型
- private String uploadContentType;
- // 上传文件的描述信息
- private String description;
- // 保存上传文件的目录,相对于WEB应用程序的根路径,在struts.xml中配置
- private String uploadDir;
- //作者
- private String author;
- //日期
- private Date date;
- //
- private ResourceService resourceService;
- //保存结果
- private String result;
- //省略Getter和Setter方法
- @Override
- public String execute() throws Exception {
- // 得到保存上传文件的目录的真实路径
- File dir = new File("C:\\upload");
- // 如果该目录不存在,就创建
- if (!dir.exists()) {
- dir.mkdirs();
- }
- int pos = uploadFileName.lastIndexOf(".");
- uploadContentType = uploadFileName.substring(pos + 1);//文件后缀名
- date = new Date();//当前日期
- InputStream is = new FileInputStream(upload);
- OutputStream os = new FileOutputStream(new File(dir, uploadFileName));
- byte[] buf = new byte[1024];
- int len = -1;
- while ((len = is.read(buf)) != -1) {
- os.write(buf, 0, len);
- }
- is.close();
- os.close();
- setUploadDir("C:\\upload");//保存路径
- if (resourceService.saveFile(uploadFileName, uploadDir, author, description, date)) {
- result = "{suc:1, msg:’上传成功!’}";
- } else {
- result = "{suc:0, msg:’上传失败!’}";
- }
- return SUCCESS;
- }
- }
Service层
核心方法
- public class ResourceServiceImpl implements ResourceService {
- private ResourceDao resourceDao;
- //省略Getter和Setter方法
- public boolean saveFile(String fileName, String fileDir, String author, String description, Date date) {
- if (resourceDao.save(fileName, fileDir, author, description, date)) {
- return true;
- } else {
- return false;
- }
- }
- }
操作数据库,Dao层
- public class ResourceDaoImpl extends HibernateDaoSupport implements ResourceDao {
- public boolean save(String fileName, String fileDir, String author, String description, Date date) {
- resource rs = new resource();
- rs.setAuthor(author);
- rs.setDescription(description);
- rs.setFileDir(fileDir);
- rs.setFileName(fileName);
- rs.setDate(date);
- try {
- getHibernateTemplate().save(rs);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- }
Model层 资源文件resource.java
- public class resource {
- private int id;
- private File doc;//文献
- private String fileName;//名称
- private String contentType;//类型
- private String fileDir;//路径
- private Date date;//上传日期
- private String author;//作者
- private String description;//注释
- //按需添加属性
- //省略Getter和Setter方法
- }
Hibernate映射文件resource.hbm.xml
- <?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">
- <hibernate-mapping package="edu.bjtu.resouce.model">
- <class name="edu.bjtu.resource.model.resource" table="resource">
- <id column="resource_id" name="id" type="integer">
- <generator class="increment"/>
- </id>
- <property column="fileName" name="fileName" type="string"/>
- <property column="fileDir" name="fileDir" type="string"/>
- <property column="author" name="author" type="string"/>
- <property column="description" name="description" type="string"/>
- <property column="date" name="date" type="date"/>
- </class>
- </hibernate-mapping>
Struts配置文件
- <?xml version="1.0″ encoding="UTF-8″ ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <!– 上传文件的临时目录 –>
- <constant name="struts.multipart.saveDir" value="C:\\x"></constant>
- <!– 上传文件的总大小限制 –>
- <constant name="struts.multipart.maxSize" value="102400000″></constant>
- <!– 资源文件配置 –>
- <constant name="struts.custom.i18n.resources"
- value="ApplicationResources">
- </constant>
- <package name="file" extends="struts-default">
- <action name="fileUploadAction" class="fileUploadAction">
- <result name="success">/successUpload.jsp</result>
- <param name="uploadDir">/WEB-INF/UploadFiles</param>
- </action>
- </package>
- </struts>
Spring配置文件
- <?xml version="1.0″ encoding="UTF-8″?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/WEB-INF/dtd/spring-beans.dtd">
- <beans default-lazy-init="true">
- <bean id="resourceDao" class="edu.bjtu.resource.dao.impl.ResourceDaoImpl">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- <bean id="resourceService" parent="transactionProxy">
- <property name="target">
- <bean class="edu.bjtu.resource.service.impl.ResourceServiceImpl">
- <property name="resourceDao">
- <ref local="resourceDao"/>
- </property>
- </bean>
- </property>
- </bean>
- <bean id="fileUploadAction" class="edu.bjtu.resource.action.FileUploadAction" lazy-init="false">
- <property name="resourceService">
- <ref local="resourceService"/>
- </property>
- </bean>
- </beans>
上传成功后页面, successUpload.jsp 核心代码
- <h1>上传成功</h1>
- 文件名:<s:property value="uploadFileName" /><br/>
- 文件大小:<s:property value="upload.length()" /><br/>
- 文件类型:<s:property value="uploadContentType" /><br/>
- 上传者 : <s:property value="author" /><br/>
- 文件描述:<s:property value="description" /><br/>