81,122
社区成员




public class PersoForm extends ActionForm {
private FormFile pic;
public FormFile getPic() {
return pic;
}
public void setPic(FormFile pic) {
this.pic = pic;
}
...
public ActionForward persis(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws SQLException {
Integer id=0;
if(!request.getParameter("id").equals(""))id=new Integer(request.getParameter("id"));
boolean self=false;
Perso psSess=(Perso)request.getSession().getAttribute("u");
if(psSess.getId().equals(id))self=true;
SimpleDateFormat df=new SimpleDateFormat("MMddHHmmss");
String datStr=df.format(new Date());
PersoForm uf=(PersoForm)form;
uf.setPsw(null);
//Ou.pl(uf.getPsw());
Perso ps=new Perso();
BeanUtils.copyProperties(uf,ps);
if(self){
ps.setJobid(new Integer(request.getParameter("job2")));
ps.setGroupid(new Integer(request.getParameter("gru2")));
}
if(ps.getGroupid()>4)ps.setJobid(uf.getJobid());
String dir="";
try {
if(uf.getPic().getFileName()==null||uf.getPic().getFileName().equals("")){
request.setAttribute("msg", "未选择文件上传");
}else{
String uploadPath = request.getSession().getServletContext().getRealPath("/");
dir="\\upload\\sig"+datStr+"."+(uf.getPic().getFileName().split("\\.")[1]) ;
String dir1=uploadPath+dir;
OutputStream out=new FileOutputStream(new File(dir1));
System.out.println(uf.getPic());
byte b[]=uf.getPic().getFileData();
out.write(b);
out.close();
ps.setPhoto("/upload/sig"+datStr+"."+uf.getPic().getFileName().split("\\.")[1]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try{
persoBiz.persis(ps);
}catch(SQLException e){
if(e.getErrorCode()==1062){
request.setAttribute("msg", "已存在");
return mapping.findForward("mm");
}
}
request.setAttribute("msg", "已保存"+ps.getName());
if(self)
return new ActionForward("/perso.do?op=pre&id="+id);
else
return mapping.findForward("lis");
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
List list = upload.parseRequest(request);
Iterator iter = list.iterator();
FileItem item = null;
String filePath = "";
String fileName = "";
String s = "";
String suffix = "";
long size = 0 ;
while (iter.hasNext()) {
item = (FileItem) iter.next();
if (!item.isFormField()) {
fileName = item.getName();
s = fileName.substring(fileName.lastIndexOf("\\") + 1);
suffix = s.substring(s.lastIndexOf("."));
s = uuid + suffix;
filePath = ConstantFactory.getConstant("path.upload.dcm").concat("\\\\").concat(s);
size = item.getSize();
if (size != 0) {
File uploadedFile = new File(filePath);
item.write(uploadedFile);
}
}
}