很臭屁地贴一段毕设时候的代码,关于FileUplaod上传文件的.一个关键点,前台的form的enctype="multipart/form-data",表单传入的均为二进制数据,所以参数不能由request.getParameter获得!前几天一个同事很纠结后台得不到其他的表单域.所有的人都把问题指向了前台的一个新插件,后面才发现时这个原因.
UUID uuid=UUID.randomUUID();此语句可以解决文件名问题,生成唯一性的序列.
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception
{
// 前台的form的enctype="multipart/form-data",
//表单传入的均为二进制数据,所以参数不能由request.getParameter获得!!
User user=(User) request.getSession().getAttribute("user");
Dispatch dispatch=new Dispatch();
dispatch.setDrafter(user.getId());
dispatch.setDraftDept(user.getDepartment());
dispatch.setDraftDate(formater.format(new Date()));
//处理表单和文件上传的代码!!
String contextPath=request.getSession().getServletContext().getRealPath("/");
String uploadPath = contextPath + "upload//dispatch//"; // 文件上传保存的路径
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding(request.getCharacterEncoding());//解决路径或者文件名中文乱码
List items =upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext())
{
FileItem f = (FileItem) iter.next();
if (f.isFormField())
{
String fieldName=f.getFieldName();
if(fieldName.equals("title"))
dispatch.setTitle(f.getString("utf8"));//解决表单域中文乱码!!
if(fieldName.equals("keyWord"))
dispatch.setKeyWord(f.getString("utf8"));
if(fieldName.equals("urgentLevel"))
dispatch.setUrgentLevel(f.getString("utf8"));
if(fieldName.equals("secretLevel"))
dispatch.setSecretLevel(f.getString("utf8"));
if(fieldName.equals("pageNumber"))
dispatch.setPageNumber(f.getString("utf8"));
if(fieldName.equals("mainSendDept"))
dispatch.setMainSendDept(f.getString("utf8"));
if(fieldName.equals("mainSendDeptNames"))
dispatch.setMainSendDeptNames(f.getString("utf8"));
if(fieldName.equals("remarks"))
dispatch.setRemarks(f.getString("utf8"));
}
else
{
String fileName=f.getName();
if (StringUtils.isNotEmpty(fileName))
{
UUID uuid=UUID.randomUUID();
int index = fileName.lastIndexOf('.');
String docType = fileName.substring(index+1);
String attach=uploadPath+uuid+"."+docType;
dispatch.setDocType(docType);
dispatch.setDocMark(fileName.substring(0,index)+formater.format(new Date()));
dispatch.setFilename(fileName);
dispatch.setAttach(attach);
File file=new File(attach);
f.write(file);
}
}
}
//保存后返回记录ID做为流程变量.
String businessId=dispatchService.saveDispatch(dispatch);
Map<String,Object> map=new HashMap<String,Object>();
//加一个标识,可以从ID判断是哪个流程.这样可以在任务那边做更少的处理.
map.put("businessId", businessId);
map.put("type", "dispatch");
StringprocessInstanceId=this.processEngine.getExecutionService().startProcessInstanceByKey("dispatchdoc",map).getId();
dispatchService.setProcessInstanceId(processInstanceId, businessId);
List<Task> tasks=processEngine.getTaskService().findPersonalTasks(user.getId());
ProcessInstance processInstance;
for(Task t:tasks)
{
processInstance= (ProcessInstance) processEngine.getExecutionService().findExecutionById(t.getExecutionId()).getProcessInstance();
if(processInstance.getId().equals(processInstanceId))
processEngine.getTaskService().completeTask(t.getId(),"提交");
}
dispatchService.setStatus("1", businessId);
dispatch=dispatchService.getDispatch(businessId);
request.setAttribute("dispatch", dispatch);
return mapping.findForward("view");
}