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"); }
} |