|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
但是一些大型开发或者是保密型比较高的项目都会用java,原因有2点,一:java是开源的,不怕别人留后门,偷我工具,.net就不一样了,保持微软的一向风格,源代码不公开js|上传利用struts,ibaits和JSTL开辟烦琐通用的文件上传体系
文件上传也是一个陈词滥调的成绩了。struts供应了很便利的文件上传组件,使用struts,很简单开辟文件上传的体系。本文分离ibatis和JSTL开辟一个烦琐而通用的文件上传体系。经由过程本文,你能够学到怎样在struts里设置文件上传功效、struts1.1的静态form设置和利用、假如复杂的利用ibatis,假如复杂利用的JSTL。
起首我们要在struts-config里加上一句,来指定一时目次和限定巨细。
<controllermaxFileSize="2M"tempDir="d:/temp"/>
然后只需在form中加上enctype="multipart/form-data"就能够完成struts的上传了,非常复杂吧?
上面我们在数据库中创建一张表。
createsequenceAttachment_seq;
DROPTABLEATTACHMENT;
createtableAttachment
(
AttachIDINTnotnull,--主动增加号AttachFilenameVARCHAR2(250)null,--文件名AttachFileSizeINTnotnull,--文件巨细AttachMimeTypeVARCHAR2(70)null,--文件范例AttachDescVARCHAR2(250)null,--申明AttachCreationIPVARCHAR2(20)notnull,--上传的IPAttachCreationDateTIMESTAMPnotnull,--创立工夫AttachModifiedDateTIMESTAMPnotnull,--文件保留路径ATTACHFILEPATHVARCHAR2(250)NOTNULL,
primarykey(AttachID)
);
createorreplacetriggerAttach_trig_autoinc
beforeinsertonAttachment
foreachrow
begin
if(:new.AttachIDisnull)then
selectAttachment_seq.nextvalinto:new.AttachIDfromdual;
endif;
end;
字段的内容都很复杂。
上面创建ibatis的SQLmap文件。创建尺度的insert、update、delete和find的SQL。信任看过后面系列文章的伴侣对此已很熟习了。
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEsql-mapPUBLIC"-//iBATIS.com//DTDSQLMap1.0//EN""http://www.ibatis.com/dtd/sql-map.dtd";><sql-mapname="attachment"><!--=============================================mapped-statementinsert=============================================--><dynamic-mapped-statementname="insertattachmentDao">insertintoATTACHMENT(<dynamicprepend=""><isPropertyAvailableprepend=","property="ATTACHID"><isNotNullprepend=""property="ATTACHID">ATTACHID</isNotNull></isPropertyAvailable><isPropertyAvailableprepend=","property="ATTACHFILENAME"><isNotNullprepend=""property="ATTACHFILENAME">ATTACHFILENAME</isNotNull></isPropertyAvailable><isPropertyAvailableprepend=","property="ATTACHFILEPATH"><isNotNullprepend=""property="ATTACHFILEPATH">ATTACHFILEPATH</isNotNull></isPropertyAvailable><isPropertyAvailableprepend=","property="ATTACHFILESIZE"><isNotNullprepend=""property="ATTACHFILESIZE">ATTACHFILESIZE</isNotNull></isPropertyAvailable><isPropertyAvailableprepend=","property="ATTACHMIMETYPE"><isNotNullprepend=""property="ATTACHMIMETYPE">ATTACHMIMETYPE</isNotNull></isPropertyAvailable><isPropertyAvailableprepend=","property="ATTACHDESC"><isNotNullprepend=""property="ATTACHDESC">ATTACHDESC</isNotNull></isPropertyAvailable><isPropertyAvailableprepend=","property="ATTACHCREATIONIP"><isNotNullprepend=""property="ATTACHCREATIONIP">ATTACHCREATIONIP</isNotNull></isPropertyAvailable><isPropertyAvailableprepend=","property="ATTACHCREATIONDATE"><isNotNullprepend=""property="ATTACHCREATIONDATE">ATTACHCREATIONDATE</isNotNull></isPropertyAvailable><isPropertyAvailableprepend=","property="ATTACHMODIFIEDDATE"><isNotNullprepend=""property="ATTACHMODIFIEDDATE">ATTACHMODIFIEDDATE</isNotNull></isPropertyAvailable></dynamic>)valueS(
<dynamicprepend="">
<isPropertyAvailableprepend=","property="ATTACHID">
<isNotNullprepend=""property="ATTACHID">
#ATTACHID#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHFILENAME">
<isNotNullprepend=""property="ATTACHFILENAME">
#ATTACHFILENAME#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHFILEPATH">
<isNotNullprepend=""property="ATTACHFILEPATH">
#ATTACHFILEPATH#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHFILESIZE">
<isNotNullprepend=""property="ATTACHFILESIZE">
#ATTACHFILESIZE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHMIMETYPE">
<isNotNullprepend=""property="ATTACHMIMETYPE">
#ATTACHMIMETYPE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHDESC">
<isNotNullprepend=""property="ATTACHDESC">
#ATTACHDESC#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHCREATIONIP">
<isNotNullprepend=""property="ATTACHCREATIONIP">
#ATTACHCREATIONIP#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHCREATIONDATE">
<isNotNullprepend=""property="ATTACHCREATIONDATE">
#ATTACHCREATIONDATE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHMODIFIEDDATE">
<isNotNullprepend=""property="ATTACHMODIFIEDDATE">
#ATTACHMODIFIEDDATE#
</isNotNull>
</isPropertyAvailable>
</dynamic>)
</dynamic-mapped-statement>
<!--=============================================
mapped-statementupdate
=============================================-->
<dynamic-mapped-statementname="updateattachmentDao">
updateATTACHMENT
<dynamicprepend="set">
<isPropertyAvailableprepend=","property="ATTACHID">
ATTACHID=#ATTACHID#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHFILENAME">
ATTACHFILENAME=#ATTACHFILENAME:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHFILEPATH">
ATTACHFILEPATH=#ATTACHFILEPATH:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHFILESIZE">
ATTACHFILESIZE=#ATTACHFILESIZE#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHMIMETYPE">
ATTACHMIMETYPE=#ATTACHMIMETYPE:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHDESC">
ATTACHDESC=#ATTACHDESC:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHCREATIONIP">
ATTACHCREATIONIP=#ATTACHCREATIONIP:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHCREATIONDATE">
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHMODIFIEDDATE">
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isPropertyAvailable>
</dynamic>
<dynamicprepend="where">
<isPropertyAvailableprepend="and"property="ATTACHID">
ATTACHID=#ATTACHID#
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHFILENAME">
ATTACHFILENAME=#ATTACHFILENAME:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHFILEPATH">
ATTACHFILEPATH=#ATTACHFILEPATH:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHFILESIZE">
ATTACHFILESIZE=#ATTACHFILESIZE#
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHMIMETYPE">
ATTACHMIMETYPE=#ATTACHMIMETYPE:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHDESC">
ATTACHDESC=#ATTACHDESC:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHCREATIONIP">
ATTACHCREATIONIP=#ATTACHCREATIONIP:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHCREATIONDATE">
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHMODIFIEDDATE">
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isPropertyAvailable>
</dynamic>
</dynamic-mapped-statement>
<!--=============================================
mapped-statementupdateByPrimaryKey
=============================================-->
<dynamic-mapped-statementname="updateByPrimaryKeyattachmentDao">
updateATTACHMENT
<dynamicprepend="set">
<isPropertyAvailableprepend=","property="ATTACHID">
ATTACHID=#ATTACHID#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHFILENAME">
ATTACHFILENAME=#ATTACHFILENAME:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHFILEPATH">
ATTACHFILEPATH=#ATTACHFILEPATH:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHFILESIZE">
ATTACHFILESIZE=#ATTACHFILESIZE#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHMIMETYPE">
ATTACHMIMETYPE=#ATTACHMIMETYPE:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHDESC">
ATTACHDESC=#ATTACHDESC:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHCREATIONIP">
ATTACHCREATIONIP=#ATTACHCREATIONIP:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHCREATIONDATE">
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isPropertyAvailable>
<isPropertyAvailableprepend=","property="ATTACHMODIFIEDDATE">
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isPropertyAvailable>
</dynamic>
<dynamicprepend="where">
<isPropertyAvailableprepend="and"property="ATTACHID">
ATTACHID=#ATTACHID#
</isPropertyAvailable>
</dynamic>
</dynamic-mapped-statement>
<!--=============================================
mapped-statementdelete
=============================================-->
<dynamic-mapped-statementname="deleteattachmentDao"result-class="java.util.HashMap">
deletefromATTACHMENT
<dynamicprepend="where">
<isPropertyAvailableprepend="and"property="ATTACHID">
<isNotNullprepend=""property="ATTACHID">
ATTACHID=#ATTACHID#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHFILENAME">
<isNotNullprepend=""property="ATTACHFILENAME">
ATTACHFILENAME=#ATTACHFILENAME#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHFILEPATH">
<isNotNullprepend=""property="ATTACHFILEPATH">
ATTACHFILEPATH=#ATTACHFILEPATH#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHFILESIZE">
<isNotNullprepend=""property="ATTACHFILESIZE">
ATTACHFILESIZE=#ATTACHFILESIZE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHMIMETYPE">
<isNotNullprepend=""property="ATTACHMIMETYPE">
ATTACHMIMETYPE=#ATTACHMIMETYPE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHDESC">
<isNotNullprepend=""property="ATTACHDESC">
ATTACHDESC=#ATTACHDESC#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHCREATIONIP">
<isNotNullprepend=""property="ATTACHCREATIONIP">
ATTACHCREATIONIP=#ATTACHCREATIONIP#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHCREATIONDATE">
<isNotNullprepend=""property="ATTACHCREATIONDATE">
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHMODIFIEDDATE">
<isNotNullprepend=""property="ATTACHMODIFIEDDATE">
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isNotNull>
</isPropertyAvailable>
</dynamic>
</dynamic-mapped-statement>
<!--=============================================
mapped-statementdeleteByPrimaryKey
=============================================-->
<mapped-statementname="deleteByPrimaryKeyattachmentDao">
deletefromATTACHMENT
where
ATTACHID=#ATTACHID#
</mapped-statement>
<!--=============================================
mapped-statementfind
=============================================-->
<dynamic-mapped-statementname="findattachmentDao"result-class="java.util.HashMap">
select*fromATTACHMENT
<dynamicprepend="where">
<isPropertyAvailableprepend="and"property="ATTACHID">
<isNotNullprepend=""property="ATTACHID">
ATTACHID=#ATTACHID#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHFILENAME">
<isNotNullprepend=""property="ATTACHFILENAME">
ATTACHFILENAME=#ATTACHFILENAME#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHFILEPATH">
<isNotNullprepend=""property="ATTACHFILEPATH">
ATTACHFILEPATH=#ATTACHFILEPATH#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHFILESIZE">
<isNotNullprepend=""property="ATTACHFILESIZE">
ATTACHFILESIZE=#ATTACHFILESIZE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHMIMETYPE">
<isNotNullprepend=""property="ATTACHMIMETYPE">
ATTACHMIMETYPE=#ATTACHMIMETYPE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHDESC">
<isNotNullprepend=""property="ATTACHDESC">
ATTACHDESC=#ATTACHDESC#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHCREATIONIP">
<isNotNullprepend=""property="ATTACHCREATIONIP">
ATTACHCREATIONIP=#ATTACHCREATIONIP#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHCREATIONDATE">
<isNotNullprepend=""property="ATTACHCREATIONDATE">
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHMODIFIEDDATE">
<isNotNullprepend=""property="ATTACHMODIFIEDDATE">
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isNotNull>
</isPropertyAvailable>
</dynamic>
<dynamicprepend="orderby">
<isNotNullprepend=""property="sortOrder">
$sortOrder$
</isNotNull>
<isNullprepend=""property="sortOrder">
</isNull>
</dynamic>
</dynamic-mapped-statement>
<!--=============================================
mapped-statementfindByPrimaryKey
=============================================-->
<mapped-statementname="findByPrimaryKeyattachmentDao"result-class="java.util.HashMap">
select*fromATTACHMENT
where
ATTACHID=#ATTACHID#
</mapped-statement>
<!--=============================================
mapped-statementfindcount
=============================================-->
<dynamic-mapped-statementname="findattachmentDaoCount"result-class="java.lang.Integer">
selectcount(1)fromATTACHMENT
<dynamicprepend="where">
<isPropertyAvailableprepend="and"property="ATTACHID">
<isNotNullprepend=""property="ATTACHID">
ATTACHID=#ATTACHID#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHFILENAME">
<isNotNullprepend=""property="ATTACHFILENAME">
ATTACHFILENAME=#ATTACHFILENAME#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHFILEPATH">
<isNotNullprepend=""property="ATTACHFILEPATH">
ATTACHFILEPATH=#ATTACHFILENAME#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHFILESIZE">
<isNotNullprepend=""property="ATTACHFILESIZE">
ATTACHFILESIZE=#ATTACHFILESIZE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHMIMETYPE">
<isNotNullprepend=""property="ATTACHMIMETYPE">
ATTACHMIMETYPE=#ATTACHMIMETYPE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHDESC">
<isNotNullprepend=""property="ATTACHDESC">
ATTACHDESC=#ATTACHDESC#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHCREATIONIP">
<isNotNullprepend=""property="ATTACHCREATIONIP">
ATTACHCREATIONIP=#ATTACHCREATIONIP#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHCREATIONDATE">
<isNotNullprepend=""property="ATTACHCREATIONDATE">
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailableprepend="and"property="ATTACHMODIFIEDDATE">
<isNotNullprepend=""property="ATTACHMODIFIEDDATE">
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isNotNull>
</isPropertyAvailable>
</dynamic>
</dynamic-mapped-statement>
</sql-map>
下一小就是创建数据操纵层的类代码
/**Createdon2003-10-11**Tochangethetemplateforthisgeneratedfilegoto*Window-Preferences-Java-CodeGeneration-CodeandComments*/packagecom.ewuxi.champion.persistence.dao;
importjava.util.HashMap;
importjava.util.List;
importcom.ewuxi.champion.exception.DaoException;
importcom.ibatis.db.sqlmap.SqlMap;
/**
*@authorchampion
*
*attachment数据库操纵对象
*/
publicclassAttachDb{
/**
*@paramvo
*@throwsDaoException
*拔出一笔记录
*/
publicvoidinsert(HashMapvo)throwsDaoException{
try{
SqlMapsqlMap=DaoCommon.getSqlMap(DaoCommon.getDefautDao());
sqlMap.executeUpdate("insertattachmentDao",vo);
}catch(Exceptione){
thrownewDaoException(e);
}
}
/**
*@paramvo
*@throwsDaoException
*删除一笔记录
*/
publicvoiddelete(HashMapvo)throwsDaoException{
try{
SqlMapsqlMap=DaoCommon.getSqlMap(DaoCommon.getDefautDao());
sqlMap.executeUpdate("deleteByPrimaryKeyattachmentDao",vo);
}catch(Exceptione){
thrownewDaoException(e);
}
}
/**
*@paramvo
*@throwsDaoException
*修正一笔记录
*/
publicvoidupdate(HashMapvo)throwsDaoException{
try{
SqlMapsqlMap=DaoCommon.getSqlMap(DaoCommon.getDefautDao());
sqlMap.executeUpdate("updateByPrimaryKeyattachmentDao",vo);
}catch(Exceptione){
thrownewDaoException(e);
}
}
/**
*@paramvo
*@return
*@throwsDaoException
*查找一笔记录
*/
publicHashMapfindByPk(HashMapvo)throwsDaoException{
try{
SqlMapsqlMap=DaoCommon.getSqlMap(DaoCommon.getDefautDao());
return(HashMap)sqlMap.executeQueryForObject(
"findByPrimaryKeyattachmentDao",
vo);
}catch(Exceptione){
thrownewDaoException(e);
}
}
publicListfind(Objectvo)throwsDaoException{
try{
SqlMapsqlMap=DaoCommon.getSqlMap(DaoCommon.getDefautDao());
return(List)sqlMap.executeQueryForList("findattachmentDao",vo);
}catch(Exceptione){
thrownewDaoException(e);
}
}
}
这一层的代码也是屡次见到的老伴侣了。现实上关于年夜多半数据库操纵,我们都只必要下面这么一点代码。然后我们创建add的action办法
publicActionForwardadd(
ActionMappingmapping,
ActionFormform,
HttpServletRequestrequest,
HttpServletResponseresponse)
throwsException{
try{
Service.initSet();
if(forminstanceofDynaActionForm){
DynaActionFormfm=(DynaActionForm)form;
FormFileff=(FormFile)fm.get("upload");
if(ff!=null&&ff.getFileSize()>0){
StringfileName=
Service.getPath().substring(
0,
Service.getPath().length()
-"/WEB-INF/classes".length())
+"file/"
+ff.getFileName();
HashMapmap=newHashMap();
map.put(Constattachment.ATTACHDESC,fm.get("desc"));
map.put(Constattachment.ATTACHFILENAME,ff.getFileName());
map.put(
Constattachment.ATTACHMIMETYPE,
ff.getContentType());
map.put(Constattachment.ATTACHCREATIONDATE,newDate());
map.put(Constattachment.ATTACHMODIFIEDDATE,newDate());
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);
AttachDbattachDb=newAttachDb();
DaoCommon.startTransaction();
attachDb.insert(map);
DaoCommon.commit();
request.setAttribute("url",fm.get("url"));
request.setAttribute(
"fileName",
"/file/"+ff.getFileName());
log.info(ff.getFileName());
returnmapping.findForward("success");
}
}
ActionErrorserrors=newActionErrors();
errors.add(
ActionErrors.GLOBAL_ERROR,
newActionError("errors.general","请选择一个文件!"));
saveErrors(request,errors);
returnmapping.findForward("false");
}catch(DaoExceptione){
log.error(e,e);
DaoCommon.rollBack();
ActionErrorserrors=newActionErrors();
errors.add(
ActionErrors.GLOBAL_ERROR,
newActionError("errors.general","数据库操纵毛病!"));
saveErrors(request,errors);
returnmapping.findForward("false");
}catch(FileNotFoundExceptione){
log.error(e,e);
ActionErrorserrors=newActionErrors();
errors.add(
ActionErrors.GLOBAL_ERROR,
newActionError("errors.general","文件保留毛病!"));
saveErrors(request,errors);
returnmapping.findForward("false");
}catch(IOExceptione){
log.error(e,e);
ActionErrorserrors=newActionErrors();
errors.add(
ActionErrors.GLOBAL_ERROR,
newActionError("errors.general","文件操纵毛病!"));
saveErrors(request,errors);
returnmapping.findForward("false");
}catch(Exceptione){
log.error(e,e);
ActionErrorserrors=newActionErrors();
errors.add(
ActionErrors.GLOBAL_ERROR,
newActionError("errors.general","不测毛病!"));
saveErrors(request,errors);
returnmapping.findForward("false");
}
}
从下面能够看出,文件的保留事情很复杂,只要三句
DynaActionFormfm=(DynaActionForm)form;
FormFileff=(FormFile)fm.get("upload");
FileManager.saveFile(fileName,ff);
此处有一个DynaActionForm,经由过程DynaActionForm我们能够节俭一个ActionForm的事情了。固然,这也多了一个设置事情。在struts-conifg.xml里加一个
<!--uploadformbean-->
<form-beanname="uploadForm"
type="org.apache.struts.action.DynaActionForm">
<form-propertyname="upload"type="org.apache.struts.upload.FormFile"/>
<form-propertyname="url"type="java.lang.String"/>
<form-propertyname="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框中。
<formname="form1"><imgname="image1"src="">
<inputtype="text"name="txt">
</form>
<inputtype="button"value="检测">
<inputtype="button"value="上传文件"/upload.do?m=list&url=document.image1.src"/> |
|