首先是上传页面(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;
-
-
-
-
-
-
public class FileUploadAction extends ActionSupport {
-
-
private static final long serialVersionUID = 1L;
-
-
private File upload;
-
-
private String uploadFileName;
-
-
private String uploadContentType;
-
-
private String description;
-
-
private String uploadDir;
-
-
private String author;
-
-
private Date date;
-
-
private ResourceService resourceService;
-
-
private String result;
-
-
-
@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;
-
-
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;
-
-
-
}
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/>
当然,肯定还需要加各种拦截器,在Struts中,任何Action都需要拦截器.这一文件上传模块中必须新建拦截器,以验证是否登录,验证权限等功能,这里省略拦截器.关于拦截器的详细使用,也许过几天会详细描述.