爱心技术专栏专题

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

摘录:爱心 来源:爱心 加入时间:2006年09月13日
摘要:
使用struts,ibaits和JSTL开发简便通用的文件上传系统(7)
public ActionForward add(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
try {

Service.initSet();

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

站点:爱心种子小博士 关键字:使用struts,ibaits和JSTL
使用struts,ibaits和JSTL开发简便通用的文件上传系统(7)
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");
}

}

转载:转载请保留本信息,本文来自http://www.51dibs.com/lp07/la/a/l_a_c7a69559fe7acbe1.html

客户服务中心信箱:[email protected] [email protected] 网站地图

声明

爱心赞助: