/** * 产生异常 * * @param * @return */ public ActionForward doError( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { demoService.doError(); setOkTipMsg("交易成功", response); return mapping.findForward("exceptionView"); }
/** * Web表单文件上传 单个/批量同理 * * @param * @return */ public ActionForward doUpload( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { BaseActionForm cForm = (BaseActionForm) form; // 单个文件,如果是多个就cForm.getFile2()....支持最多5个文件 FormFile myFile = cForm.getFile1(); // 获取web应用根路径,也可以直接指定服务器任意盘符路径 String savePath = getServlet().getServletContext().getRealPath("/") + "/upload/"; // String savePath = "d:/upload/"; // 检查路径是否存在,如果不存在则创建之 File file = new File(savePath); if (!file.exists()) { file.mkdir(); } // 文件按天归档 savePath = savePath + G4Utils.getCurDate() + "/"; File file1 = new File(savePath); if (!file1.exists()) { file1.mkdir(); } // 文件真实文件名 String fileName = myFile.getFileName(); // 我们一般会根据某种命名规则对其进行重命名 // String fileName = ; File fileToCreate = new File(savePath, fileName); // 检查同名文件是否存在,不存在则将文件流写入文件磁盘系统 if (!fileToCreate.exists()) { FileOutputStream os = new FileOutputStream(fileToCreate); os.write(myFile.getFileData()); os.flush(); os.close(); } else { // 此路径下已存在同名文件,是否要覆盖或给客户端提示信息由你自己决定 FileOutputStream os = new FileOutputStream(fileToCreate); os.write(myFile.getFileData()); os.flush(); os.close(); } // 我们通常还会把这个文件的相关信息持久化到数据库 Dto inDto = cForm.getParamAsDto(request); inDto.put( "title", G4Utils.isEmpty(inDto.getAsString("title")) ? fileName : inDto.getAsString("title")); inDto.put("filesize", myFile.getFileSize()); inDto.put("path", savePath + fileName); demoService.doUpload(inDto); setOkTipMsg("文件上传成功", response); return mapping.findForward(null); }
/** * 删除文件 * * @param mapping * @param form * @param request * @param response * @return * @throws Exception */ public ActionForward delFiles( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { BaseActionForm aForm = (BaseActionForm) form; Dto dto = aForm.getParamAsDto(request); String[] strChecked = dto.getAsString("strChecked").split(","); for (int i = 0; i < strChecked.length; i++) { String fileid = strChecked[i]; Dto fileDto = (BaseDto) g4Reader.queryForObject("Demo.queryFileByFileID", fileid); String path = fileDto.getAsString("path"); File file = new File(path); file.delete(); demoService.delFile(fileid); } setOkTipMsg("文件删除成功", response); return mapping.findForward(null); }