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

这一层的代码也是多次见到的老朋友了。事实上对于大多数数据库操作,我们都只需要上面这么一点代码。然后我们建立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里加一个。



这样,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());
09-28 00:46