public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
logger.debug("begin add document");
DynaActionForm dynaForm=(DynaActionForm)form;
Document doc=new Document();
Customer customer = null;
//verify if there is a file
Admin admin=SessionRequestManager.getAdmin(request.getSession());
FormFile file=(FormFile)dynaForm.get("file");
if(file==null){
logger.error("add document error,the file you upload is too big");
saveErrorMessage(request,"customer.doc.toobig");
return mapping.findForward(Constants.FORWARD_FAILURE);
}
long customerId = 0;
try{
customerId = Long.parseLong( request.getParameter("customerId") );
}catch(NumberFormatException ne){
customerId =(Long)dynaForm.get("customerId");
}
boolean isOut = false;
String storeDir = servlet.getServletContext().getRealPath("/upload");
InputStream streamIn=file.getInputStream();
OutputStream streamOut = null;
try {
customer = customerCtl.get(customerId);
File des = new File(storeDir+"\\"+customer.getName());
if(!des.exists()){
des.mkdir();
}
String fileName = (String)dynaForm.get("name");
String orginName = file.getFileName();
String ext = orginName.substring(orginName.lastIndexOf("."));
File temp = new File(des+"\\"+fileName+ext);
if(temp.exists()){
logger.error("add document error,here is file with the same name");
saveErrorMessage(request,"customer.doc.samename");
return mapping.findForward(Constants.FORWARD_FAILURE);
}
int bytes=0;
byte[] buffer=new byte[8192];
streamOut=new FileOutputStream(temp.getPath());
isOut = true;
while((bytes=streamIn.read(buffer,0,8192))!=-1){
streamOut.write(buffer,0,bytes);
}
BeanUtils.copyProperties(doc, dynaForm);
doc.setOperator(admin.getLoginName());
doc.setCustomer(customer);
doc.setCreateTime(Calendar.getInstance().getTime());
doc.setPath(temp.getPath());
documentCtl.add(doc);
} catch (ContractException ce) {
logger.error("add document error,it may occured in documentCtl.getAll(customer)", ce);
saveErrorMessage(request, "customer.addDoc.failure");
return mapping.findForward(Constants.FORWARD_FAILURE);
} catch (Exception e) {
logger.error("add document error", e);
saveErrorMessage(request, "customer.addDoc.failure");
return mapping.findForward(Constants.FORWARD_FAILURE);
}
finally{
if(isOut){
streamOut.close();
}
dynaForm.set("customerId",customerId);
request.setAttribute("customerId",customerId);
streamIn.close();
file.destroy();
}
logger.debug("add doc end ");
saveMessage(request,"customer.addDoc.success");
return mapping.findForward(Constants.FORWARD_SUCCESS);
}
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
logger.debug("begin add document");
DynaActionForm dynaForm=(DynaActionForm)form;
Document doc=new Document();
Customer customer = null;
//verify if there is a file
Admin admin=SessionRequestManager.getAdmin(request.getSession());
FormFile file=(FormFile)dynaForm.get("file");
if(file==null){
logger.error("add document error,the file you upload is too big");
saveErrorMessage(request,"customer.doc.toobig");
return mapping.findForward(Constants.FORWARD_FAILURE);
}
long customerId = 0;
try{
customerId = Long.parseLong( request.getParameter("customerId") );
}catch(NumberFormatException ne){
customerId =(Long)dynaForm.get("customerId");
}
boolean isOut = false;
String storeDir = servlet.getServletContext().getRealPath("/upload");
InputStream streamIn=file.getInputStream();
OutputStream streamOut = null;
try {
customer = customerCtl.get(customerId);
File des = new File(storeDir+"\\"+customer.getName());
if(!des.exists()){
des.mkdir();
}
String fileName = (String)dynaForm.get("name");
String orginName = file.getFileName();
String ext = orginName.substring(orginName.lastIndexOf("."));
File temp = new File(des+"\\"+fileName+ext);
if(temp.exists()){
logger.error("add document error,here is file with the same name");
saveErrorMessage(request,"customer.doc.samename");
return mapping.findForward(Constants.FORWARD_FAILURE);
}
int bytes=0;
byte[] buffer=new byte[8192];
streamOut=new FileOutputStream(temp.getPath());
isOut = true;
while((bytes=streamIn.read(buffer,0,8192))!=-1){
streamOut.write(buffer,0,bytes);
}
BeanUtils.copyProperties(doc, dynaForm);
doc.setOperator(admin.getLoginName());
doc.setCustomer(customer);
doc.setCreateTime(Calendar.getInstance().getTime());
doc.setPath(temp.getPath());
documentCtl.add(doc);
} catch (ContractException ce) {
logger.error("add document error,it may occured in documentCtl.getAll(customer)", ce);
saveErrorMessage(request, "customer.addDoc.failure");
return mapping.findForward(Constants.FORWARD_FAILURE);
} catch (Exception e) {
logger.error("add document error", e);
saveErrorMessage(request, "customer.addDoc.failure");
return mapping.findForward(Constants.FORWARD_FAILURE);
}
finally{
if(isOut){
streamOut.close();
}
dynaForm.set("customerId",customerId);
request.setAttribute("customerId",customerId);
streamIn.close();
file.destroy();
}
logger.debug("add doc end ");
saveMessage(request,"customer.addDoc.success");
return mapping.findForward(Constants.FORWARD_SUCCESS);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货