使用struts,ibaits和JSTL开发简便通用的文件上传系统

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>

由此,我们完成了一个通用的文件上传系统的粗的框架。

时间: 2024-11-05 18:37:55

使用struts,ibaits和JSTL开发简便通用的文件上传系统的相关文章

asp.net 简便无刷新文件上传系统

asp.net 简便无刷新文件上传系统 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>简便无刷新文件上传系统</title> <s

asp.net 简便无刷新文件上传系统_实用技巧

ps:flash实现的效果是好得多,但这不是我研究的范围,也没什么可比性. 兼容:ie6/7/8, firefox 3.5.5, opera 10.01, safari 4.0.3, chrome 3.0 效果预览 文件上传 选择文件 重命名 操作 状态 重置 选择文件 重置 选择文件 重置 选择文件 ps:由于需要后台,要测试系统请下载实例测试. ps2:在完整实例文件中,还有一个文件属性查看实例. 程序说明 [upload] 程序中最重要的方法就是upload了,调用它就可以进行无刷新上传.

简便无刷新文件上传系统

文件属性查看: 文件上传 选择文件 重命名 操作 状态 重置 选择文件

.net开发相关问题(文件上传,拜托各位大神帮忙解答,错误报告如下)

问题描述 报出找不到文件路径的错误!代码错误已经定位在了标红的代码处:HomeController.csif(!HasFiles.HasFile(Request.Files[upload]))continue;stringminiType=Request.Files[upload].ContentType;StreamfileStream=Request.Files[upload].InputStream;stringpath=AppDomain.CurrentDomain.BaseDirect

PHP通用文件上传类

    由于做项目中经常遇到文件上传,因此封装了一个通用的文件上传类,作用支持组文件上传,并且不同的上传域可以上传不同的文件类型,不同的文件类型限制的文件大小也可能不相同.举例来说:用户可上传一种展品并可为该展品上传一张缩略图,那么缩略图文件限制类型可能为jpg,gif,png等,而展品文件限制类型可能为mov,avi,mpeg等,而图片大小可能限制为100KB,音频视频大小可能限制为2MB.类代码如下: /* 文件上传类 */class Upload{    public $InputName

Struts 框架 之 文件上传下载案例

Struts 框架 文件上传 1. 先准备 Struts 环境 (我使用的是struts 2.3.4版本) 导jar包:   jar包的具体作用在前面的文章有讲. 配置 web.xml <!-- Struts核心拦截器 --> <filter> <filter-name>Struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepare

在Struts 2中实现文件上传

前一阵子有些朋友在电子邮件中问关于Struts 2实现文件上传的问题, 所以今天我们就来讨论一下这个问题. 实现原理 Struts 2是通过Commons FileUpload文件上传.Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中.从而我们就能够以本地文件方式的操作浏览器上传的文件. 具体实现 前段时间Apache发布了Struts 2.0.6 GA,所以本文的实现是以该版本的Struts

软件开发-怎样实现文件上传下载问题

问题描述 怎样实现文件上传下载问题 我想在注册页面实现自己上传本地头像,请问怎么实现(我用的是java web开发)

安卓文件上传下载-我是安卓开发学了一点,大家可以给我讲讲如何写一个上传下载的功能

问题描述 我是安卓开发学了一点,大家可以给我讲讲如何写一个上传下载的功能 安卓我是0基础,现在我们老师命令我写一个文件上传下载,可是我只看了那么一点,大家可以给我讲讲思路,自己实际案例 解决方案 首先看看你们老师的要求是上传下载到哪里?然后再搜索方法案例,因为数据存储有多种方式都不一样的 解决方案二: http://download.csdn.net/detail/airlke/8172213