js|上传
使用struts,ibaits和JSTL开发简便通用的文件上传系统
文件上传也是一个老生常谈的问题了。struts提供了很方便的文件上传组件,利用struts,很容易开发文件上传的系统。本文结合ibatis和JSTL开发一个简便而通用的文件上传系统。通过本文,你可以学到如何在struts里配置文件上传功能、struts1.1的动态form配置和使用、如果简单的使用ibatis,如果简单使用的JSTL。
首先我们要在struts-config里加上一句,来指定临时目录以及限制大小。
<controller maxFileSize="2M" tempDir="d:/temp"/>
然后只要在form中加上enctype="multipart/form-data" 就可以实现struts的上传了,十分简单吧?
下面我们在数据库中建立一张表。
create sequence Attachment_seq;
DROP TABLE ATTACHMENT;
create table Attachment
(
AttachID INT not null, --自动增长号 AttachFilename VARCHAR2(250) null, --文件名 AttachFileSize INT not null, --文件大小 AttachMimeType VARCHAR2(70) null, --文件类型 AttachDesc VARCHAR2(250) null, --说明 AttachCreationIP VARCHAR2(20) not null, --上传的IP AttachCreationDate TIMESTAMP not null, --创建时间 AttachModifiedDate TIMESTAMP not null, --文件保存路径 ATTACHFILEPATH VARCHAR2 (250) NOT NULL,
primary key (AttachID)
);
create or replace trigger Attach_trig_autoinc
before insert on Attachment
for each row
begin
if (:new.AttachID is null) then
select Attachment_seq.nextval into :new.AttachID from dual;
end if;
end;
字段的内容都很简单。
下面建立ibatis的SQL map文件。建立标准的insert、update、delete和find的SQL。相信看过前面系列文章的朋友对此已经很熟悉了。
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sql-mapPUBLIC "-//iBATIS.com//DTD SQL Map 1.0//EN""http://www.ibatis.com/dtd/sql-map.dtd";><sql-map name="attachment"><!-- ============================================= mapped-statement insert ============================================= --><dynamic-mapped-statement name="insertattachmentDao" > insert into ATTACHMENT ( <dynamic prepend=""> <isPropertyAvailable prepend="," property="ATTACHID" > <isNotNull prepend="" property="ATTACHID" > ATTACHID </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHFILENAME" > <isNotNull prepend="" property="ATTACHFILENAME" > ATTACHFILENAME </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHFILEPATH" > <isNotNull prepend="" property="ATTACHFILEPATH" > ATTACHFILEPATH </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHFILESIZE" > <isNotNull prepend="" property="ATTACHFILESIZE" > ATTACHFILESIZE </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHMIMETYPE" > <isNotNull prepend="" property="ATTACHMIMETYPE" > ATTACHMIMETYPE </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHDESC" > <isNotNull prepend="" property="ATTACHDESC" > ATTACHDESC </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHCREATIONIP" > <isNotNull prepend="" property="ATTACHCREATIONIP" > ATTACHCREATIONIP </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHCREATIONDATE" > <isNotNull prepend="" property="ATTACHCREATIONDATE" > ATTACHCREATIONDATE </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHMODIFIEDDATE" > <isNotNull prepend="" property="ATTACHMODIFIEDDATE" > ATTACHMODIFIEDDATE </isNotNull> </isPropertyAvailable> </dynamic> ) valueS (
<dynamic prepend="">
<isPropertyAvailable prepend="," property="ATTACHID" >
<isNotNull prepend="" property="ATTACHID" >
#ATTACHID#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILENAME" >
<isNotNull prepend="" property="ATTACHFILENAME" >
#ATTACHFILENAME#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILEPATH" >
<isNotNull prepend="" property="ATTACHFILEPATH" >
#ATTACHFILEPATH#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILESIZE" >
<isNotNull prepend="" property="ATTACHFILESIZE" >
#ATTACHFILESIZE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHMIMETYPE" >
<isNotNull prepend="" property="ATTACHMIMETYPE" >
#ATTACHMIMETYPE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHDESC" >
<isNotNull prepend="" property="ATTACHDESC" >
#ATTACHDESC#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHCREATIONIP" >
<isNotNull prepend="" property="ATTACHCREATIONIP" >
#ATTACHCREATIONIP#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHCREATIONDATE" >
<isNotNull prepend="" property="ATTACHCREATIONDATE" >
#ATTACHCREATIONDATE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHMODIFIEDDATE" >
<isNotNull prepend="" property="ATTACHMODIFIEDDATE" >
#ATTACHMODIFIEDDATE#
</isNotNull>
</isPropertyAvailable>
</dynamic>)
</dynamic-mapped-statement>
<!-- =============================================
mapped-statement update
============================================= -->
<dynamic-mapped-statement name="updateattachmentDao" >
update ATTACHMENT
<dynamic prepend="set">
<isPropertyAvailable prepend="," property="ATTACHID" >
ATTACHID=#ATTACHID#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILENAME" >
ATTACHFILENAME=#ATTACHFILENAME:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILEPATH" >
ATTACHFILEPATH=#ATTACHFILEPATH:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILESIZE" >
ATTACHFILESIZE=#ATTACHFILESIZE#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHMIMETYPE" >
ATTACHMIMETYPE=#ATTACHMIMETYPE:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHDESC" >
ATTACHDESC=#ATTACHDESC:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHCREATIONIP" >
ATTACHCREATIONIP=#ATTACHCREATIONIP:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHCREATIONDATE" >
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHMODIFIEDDATE" >
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isPropertyAvailable>
</dynamic>
<dynamic prepend="where">
<isPropertyAvailable prepend="and" property="ATTACHID" >
ATTACHID=#ATTACHID#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILENAME" >
ATTACHFILENAME=#ATTACHFILENAME:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILEPATH" >
ATTACHFILEPATH=#ATTACHFILEPATH:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILESIZE" >
ATTACHFILESIZE=#ATTACHFILESIZE#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHMIMETYPE" >
ATTACHMIMETYPE=#ATTACHMIMETYPE:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHDESC" >
ATTACHDESC=#ATTACHDESC:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHCREATIONIP" >
ATTACHCREATIONIP=#ATTACHCREATIONIP:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHCREATIONDATE" >
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHMODIFIEDDATE" >
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isPropertyAvailable>
</dynamic>
</dynamic-mapped-statement>
<!-- =============================================
mapped-statement updateByPrimaryKey
============================================= -->
<dynamic-mapped-statement name="updateByPrimaryKeyattachmentDao" >
update ATTACHMENT
<dynamic prepend="set">
<isPropertyAvailable prepend="," property="ATTACHID" >
ATTACHID=#ATTACHID#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILENAME" >
ATTACHFILENAME=#ATTACHFILENAME:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILEPATH" >
ATTACHFILEPATH=#ATTACHFILEPATH:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILESIZE" >
ATTACHFILESIZE=#ATTACHFILESIZE#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHMIMETYPE" >
ATTACHMIMETYPE=#ATTACHMIMETYPE:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHDESC" >
ATTACHDESC=#ATTACHDESC:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHCREATIONIP" >
ATTACHCREATIONIP=#ATTACHCREATIONIP:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHCREATIONDATE" >
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHMODIFIEDDATE" >
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isPropertyAvailable>
</dynamic>
<dynamic prepend="where">
<isPropertyAvailable prepend="and" property="ATTACHID" >
ATTACHID=#ATTACHID#
</isPropertyAvailable>
</dynamic>
</dynamic-mapped-statement>
<!-- =============================================
mapped-statement delete
============================================= -->
<dynamic-mapped-statement name="deleteattachmentDao" result-class="java.util.HashMap">
delete from ATTACHMENT
<dynamic prepend="where">
<isPropertyAvailable prepend="and" property="ATTACHID" >
<isNotNull prepend="" property="ATTACHID" >
ATTACHID=#ATTACHID#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILENAME" >
<isNotNull prepend="" property="ATTACHFILENAME" >
ATTACHFILENAME=#ATTACHFILENAME#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property=" ATTACHFILEPATH" >
<isNotNull prepend="" property=" ATTACHFILEPATH" >
ATTACHFILEPATH=# ATTACHFILEPATH#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILESIZE" >
<isNotNull prepend="" property="ATTACHFILESIZE" >
ATTACHFILESIZE=#ATTACHFILESIZE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHMIMETYPE" >
<isNotNull prepend="" property="ATTACHMIMETYPE" >
ATTACHMIMETYPE=#ATTACHMIMETYPE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHDESC" >
<isNotNull prepend="" property="ATTACHDESC" >
ATTACHDESC=#ATTACHDESC#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHCREATIONIP" >
<isNotNull prepend="" property="ATTACHCREATIONIP" >
ATTACHCREATIONIP=#ATTACHCREATIONIP#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHCREATIONDATE" >
<isNotNull prepend="" property="ATTACHCREATIONDATE" >
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHMODIFIEDDATE" >
<isNotNull prepend="" property="ATTACHMODIFIEDDATE" >
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isNotNull>
</isPropertyAvailable>
</dynamic>
</dynamic-mapped-statement>
<!-- =============================================
mapped-statement deleteByPrimaryKey
============================================= -->
<mapped-statement name="deleteByPrimaryKeyattachmentDao" >
delete from ATTACHMENT
where
ATTACHID=#ATTACHID#
</mapped-statement>
<!-- =============================================
mapped-statement find
============================================= -->
<dynamic-mapped-statement name="findattachmentDao" result-class="java.util.HashMap">
select * from ATTACHMENT
<dynamic prepend="where">
<isPropertyAvailable prepend="and" property="ATTACHID" >
<isNotNull prepend="" property="ATTACHID" >
ATTACHID=#ATTACHID#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILENAME" >
<isNotNull prepend="" property="ATTACHFILENAME" >
ATTACHFILENAME=#ATTACHFILENAME#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILEPATH" >
<isNotNull prepend="" property="ATTACHFILEPATH" >
ATTACHFILEPATH=#ATTACHFILEPATH#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILESIZE" >
<isNotNull prepend="" property="ATTACHFILESIZE" >
ATTACHFILESIZE=#ATTACHFILESIZE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHMIMETYPE" >
<isNotNull prepend="" property="ATTACHMIMETYPE" >
ATTACHMIMETYPE=#ATTACHMIMETYPE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHDESC" >
<isNotNull prepend="" property="ATTACHDESC" >
ATTACHDESC=#ATTACHDESC#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHCREATIONIP" >
<isNotNull prepend="" property="ATTACHCREATIONIP" >
ATTACHCREATIONIP=#ATTACHCREATIONIP#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHCREATIONDATE" >
<isNotNull prepend="" property="ATTACHCREATIONDATE" >
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHMODIFIEDDATE" >
<isNotNull prepend="" property="ATTACHMODIFIEDDATE" >
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isNotNull>
</isPropertyAvailable>
</dynamic>
<dynamic prepend="order by ">
<isNotNull prepend="" property="sortOrder" >
$sortOrder$
</isNotNull>
<isNull prepend="" property="sortOrder" >
</isNull>
</dynamic>
</dynamic-mapped-statement>
<!-- =============================================
mapped-statement findByPrimaryKey
============================================= -->
<mapped-statement name="findByPrimaryKeyattachmentDao" result-class="java.util.HashMap">
select * from ATTACHMENT
where
ATTACHID=#ATTACHID#
</mapped-statement>
<!-- =============================================
mapped-statement findcount
============================================= -->
<dynamic-mapped-statement name="findattachmentDaoCount" result-class="java.lang.Integer">
select count(1) from ATTACHMENT
<dynamic prepend="where">
<isPropertyAvailable prepend="and" property="ATTACHID" >
<isNotNull prepend="" property="ATTACHID" >
ATTACHID=#ATTACHID#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILENAME" >
<isNotNull prepend="" property="ATTACHFILENAME" >
ATTACHFILENAME=#ATTACHFILENAME#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILEPATH" >
<isNotNull prepend="" property="ATTACHFILEPATH" >
ATTACHFILEPATH=#ATTACHFILENAME#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILESIZE" >
<isNotNull prepend="" property="ATTACHFILESIZE" >
ATTACHFILESIZE=#ATTACHFILESIZE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHMIMETYPE" >
<isNotNull prepend="" property="ATTACHMIMETYPE" >
ATTACHMIMETYPE=#ATTACHMIMETYPE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHDESC" >
<isNotNull prepend="" property="ATTACHDESC" >
ATTACHDESC=#ATTACHDESC#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHCREATIONIP" >
<isNotNull prepend="" property="ATTACHCREATIONIP" >
ATTACHCREATIONIP=#ATTACHCREATIONIP#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHCREATIONDATE" >
<isNotNull prepend="" property="ATTACHCREATIONDATE" >
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHMODIFIEDDATE" >
<isNotNull prepend="" property="ATTACHMODIFIEDDATE" >
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isNotNull>
</isPropertyAvailable>
</dynamic>
</dynamic-mapped-statement>
</sql-map>
下一小就是建立数据操作层的类代码
/* * Created on 2003-10-11 * * To change the template for this generated file go to * Window - Preferences - Java - Code Generation - Code and Comments */package com.ewuxi.champion.persistence.dao;
import java.util.HashMap;
import java.util.List;
import com.ewuxi.champion.exception.DaoException;
import com.ibatis.db.sqlmap.SqlMap;
/**
* @author champion
*
*attachment数据库操作对象
*/
public class AttachDb {
/**
* @param vo
* @throws DaoException
* 插入一条记录
*/
public void insert(HashMap vo) throws DaoException {
try {
SqlMap sqlMap = DaoCommon.getSqlMap(DaoCommon.getDefautDao());
sqlMap.executeUpdate("insertattachmentDao", vo);
} catch (Exception e) {
throw new DaoException(e);
}
}
/**
* @param vo
* @throws DaoException
* 删除一条记录
*/
public void delete(HashMap vo) throws DaoException {
try {
SqlMap sqlMap = DaoCommon.getSqlMap(DaoCommon.getDefautDao());
sqlMap.executeUpdate("deleteByPrimaryKeyattachmentDao", vo);
} catch (Exception e) {
throw new DaoException(e);
}
}
/**
* @param vo
* @throws DaoException
* 修改一条记录
*/
public void update(HashMap vo) throws DaoException {
try {
SqlMap sqlMap = DaoCommon.getSqlMap(DaoCommon.getDefautDao());
sqlMap.executeUpdate("updateByPrimaryKeyattachmentDao", vo);
} catch (Exception e) {
throw new DaoException(e);
}
}
/**
* @param vo
* @return
* @throws DaoException
* 查找一条记录
*/
public HashMap findByPk(HashMap vo) throws DaoException {
try {
SqlMap sqlMap = DaoCommon.getSqlMap(DaoCommon.getDefautDao());
return (HashMap) sqlMap.executeQueryForObject(
"findByPrimaryKeyattachmentDao",
vo);
} catch (Exception e) {
throw new DaoException(e);
}
}
public List find(Object vo) throws DaoException {
try {
SqlMap sqlMap = DaoCommon.getSqlMap(DaoCommon.getDefautDao());
return (List) sqlMap.executeQueryForList("findattachmentDao", vo);
} catch (Exception e) {
throw new DaoException(e);
}
}
}
这一层的代码也是多次见到的老朋友了。事实上对于大多数数据库操作,我们都只需要上面这么一点代码。然后我们建立add的action方法
public ActionForward add(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
try {
Service.initSet();
if (form instanceof DynaActionForm) {
DynaActionForm fm = (DynaActionForm) form;
FormFile ff = (FormFile) fm.get("upload");
if (ff != null && ff.getFileSize()>0) {
String fileName =
Service.getPath().substring(
0,
Service.getPath().length()
- "/WEB-INF/classes".length())
+ "file/"
+ ff.getFileName();
HashMap map = new HashMap();
map.put(Constattachment.ATTACHDESC, fm.get("desc"));
map.put(Constattachment.ATTACHFILENAME, ff.getFileName());
map.put(
Constattachment.ATTACHMIMETYPE,
ff.getContentType());
map.put(Constattachment.ATTACHCREATIONDATE, new Date());
map.put(Constattachment.ATTACHMODIFIEDDATE, new Date());
map.put(
Constattachment.ATTACHFILESIZE,
String.valueOf(ff.getFileSize()));
map.put(
Constattachment.ATTACHFILEPATH,
"/file/" + ff.getFileName());
map.put(
Constattachment.ATTACHCREATIONIP,
request.getRemoteAddr());
FileManager.saveFile(fileName, ff);
AttachDb attachDb = new AttachDb();
DaoCommon.startTransaction();
attachDb.insert(map);
DaoCommon.commit();
request.setAttribute("url", fm.get("url"));
request.setAttribute(
"fileName",
"/file/" + ff.getFileName());
log.info(ff.getFileName());
return mapping.findForward("success");
}
}
ActionErrors errors = new ActionErrors();
errors.add(
ActionErrors.GLOBAL_ERROR,
new ActionError("errors.general", "请选择一个文件!"));
saveErrors(request, errors);
return mapping.findForward("false");
} catch (DaoException e) {
log.error(e, e);
DaoCommon.rollBack();
ActionErrors errors = new ActionErrors();
errors.add(
ActionErrors.GLOBAL_ERROR,
new ActionError("errors.general", "数据库操作错误!"));
saveErrors(request, errors);
return mapping.findForward("false");
} catch (FileNotFoundException e) {
log.error(e, e);
ActionErrors errors = new ActionErrors();
errors.add(
ActionErrors.GLOBAL_ERROR,
new ActionError("errors.general", "文件保存错误!"));
saveErrors(request, errors);
return mapping.findForward("false");
} catch (IOException e) {
log.error(e, e);
ActionErrors errors = new ActionErrors();
errors.add(
ActionErrors.GLOBAL_ERROR,
new ActionError("errors.general", "文件操作错误!"));
saveErrors(request, errors);
return mapping.findForward("false");
} catch (Exception e) {
log.error(e, e);
ActionErrors errors = new ActionErrors();
errors.add(
ActionErrors.GLOBAL_ERROR,
new ActionError("errors.general", "意外错误!"));
saveErrors(request, errors);
return mapping.findForward("false");
}
}
从上面可以看出,文件的保存工作很简单,只有三句
DynaActionForm fm = (DynaActionForm) form;
FormFile ff = (FormFile) fm.get("upload");
FileManager.saveFile(fileName, ff);
此处有一个DynaActionForm,通过DynaActionForm我们可以节省一个ActionForm的工作了。当然,这也多了一个配置工作。在struts-conifg.xml里加一个
<!-- upload form bean -->
<form-bean name="uploadForm"
type="org.apache.struts.action.DynaActionForm">
<form-property name="upload" type="org.apache.struts.upload.FormFile"/>
<form-property name="url" type="java.lang.String"/>
<form-property name="desc" type="java.lang.String"/>
</form-bean>
这样,struts在页面提交以后会自动去找upload、rul和 desc这三个输入 ,并将它转成相应的数据类型。
所以我们很容易得到一个FormFile对象,而这个对象就包含了上传文件的所有信息。因此,我们的数据表相应的信息也有了
map.put(Constattachment.ATTACHFILENAME, ff.getFileName());
map.put( Constattachment.ATTACHMIMETYPE, ff.getContentType());
map.put( Constattachment.ATTACHFILESIZE, String.valueOf(ff.getFileSize()));
map.put( Constattachment.ATTACHFILEPATH, "/file/" + ff.getFileName());
最后我们只要简单的调用函数插入数据库
DaoCommon.startTransaction();
attachDb.insert(map);
DaoCommon.commit();
核心技术基本讲解完毕。下面讲一下实现的流程。大家可以先看一下demo。首先是一个index页面,可以选择一个上传一个图片文件直接显示出来。也可以把内容显示在input框中。
<form name="form1"><img name="image1" src="">
<input type="text" name="txt">
</form>
<input type="button" value="检测" >
<input type="button" value="上传文件" /upload.do?m=list&url=document.image1.src"/>´)">
<input type="button" value="上传文件(文件名到输入框)" /upload.do?m=list&url=document.form1.txt.value"/>´)">
index.jsp的主要内容如上:主要特别的一点的地方就是<c:url>,是JSTL的url的tag,通过它可以得到相对路径的URL。两个按钮的弹出框都是upload.do,方法是list.list的方法很少
AttachDb attachDb = new AttachDb();
DaoCommon.startTransaction();
request.setAttribute("fileslist", attachDb.find(new HashMap()));
DaoCommon.rollBack();
主要就是列出表中所有文件。放到fileslist这个对象中去。然后指到attachlist.jsp这个jsp文件去。作为view层,attachlist很简单的。
<a href="attach.jsp?url=<c:out value="${param[´url´]}"/>">添加文件</a>
<table width="100%" border="1">
<tr>
<td>名称</td>
<td>格式</td>
<td>文件</td>
<td>选择</td>
</tr>
<c:forEach var="i" items="${fileslist}">
<tr>
<td><c:out value="${i[´ATTACHDESC´]}"/></td>
<td><c:out value="${i[´ATTACHMIMETYPE´]}"/></td>
<td><c:out value="${i[´ATTACHFILEPATH´]}"/></td>
<td><a href="#" /${i[´ATTACHFILEPATH´]}"/>´);">选择</a> <a href="<c:url value="upload.do?m=del"><c:param name="url" value="${param[´url´]}"/><c:param name="ATTACHID" value="${i[´ATTACHID´]}"/></c:url>">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
<script>
function selectone(value){
opener.<c:out value="${param[´url´]}"/>=value
window.close();
}
</script>
首先我们可以看看怎么显示表格 <c:forEach var="i" items="${fileslist}">通过forEach这个tag.得到fieslist里的对象。 <c:out value="${i[´ATTACHDESC´]}"/>,通过这一句也可以看到jstl显示HashMap的数据是多么容易。 下面就是参数化的URL,看下面的URL。
<a href="<c:url value="upload.do?m=del"><c:param name="url" value="${param[´url´]}"/><c:param name="ATTACHID" value="${i[´ATTACHID´]}"/></c:url>">这里有两个参数,一个是url,是从前面的param,也就是前一页的参数取来的。另一个参数是从javaBean对象中取得。ATTACHID就是唯一键,删除的时候就只要这一个参数就可以了。
删除的代码也简单,把ATTACHID放入hashMap,然后执行删除(注,文件没有实际删除,不过要实现文件删除的代码也很简单。)。最后也是读取所有的文件,再返回到本页
AttachDb attachDb = new AttachDb();
DaoCommon.startTransaction();
HashMap delMap = new HashMap();
delMap.put(
Constattachment.ATTACHID,
request.getParameter("ATTACHID"));
attachDb.delete(delMap);
request.setAttribute("fileslist", attachDb.find(new HashMap()));
DaoCommon.commit();
添加文件指向attach.jsp。这个文件是上传的主要文件,实现一个实际的上传界面。
<a href="<c:url value="/upload.do?m=list"><c:param name="url" value="${param[´url´]}"/></c:url>">文件管理器</a>
<p>
<html:form action="/upload" enctype="multipart/form-data" >
说明: <input name="desc" type="input" size="30"> <br>
文件:<input name="upload" type="file" id="upload" size="50"><br>
<input name="url" type="hidden" value="<c:out value="${param[´url´]}"/>">
<input name="m" type="hidden" value="add">
<input type="submit" name="Submit" value="上传">
</html:form >
</p>
</body>
提交以后执行前面所说的add方面。然后转到success.jsp。其内容就是把前面的fileName转到index页面,同时关闭自己。
<script>
opener.<c:out value="${url}"/>="<c:url value="/${fileName}"/>";
window.close();
</script>
由此,我们完成了一个通用的文件上传系统的粗的框架。