1 /**
 2      * 处理提交公司信息
 3      * @param request
 4      * @param response
 5      * @return
 6      * @throws IOException
 7      */
 8     @RequestMapping(value = "/companyInfo", method = RequestMethod.POST)
 9     public ModelAndView companyInfo(HttpServletRequest request,
10             HttpServletResponse response) throws IOException {
11 
12         ModelAndView mv = new ModelAndView();
13         SysUserModel sysUserModel = Context.getCurrentUser();
14         final String memorySize = Config.get("memorySize");// 内存缓冲大小
15         final String sizeMax = Config.get("sizeMax");// 文件允许的最大值
16 
17         final PrintWriter out = response.getWriter();
18         if (ServletFileUpload.isMultipartContent(request)) {
19             try {
20                 // 创建磁盘工厂
21                 DiskFileItemFactory factory = new DiskFileItemFactory();
22                 // 设置内存缓冲大小
23                 factory.setSizeThreshold(Integer.parseInt(memorySize));
24                 // 设置临时目录
25                 String tempdir = request.getServletContext().getRealPath(
26                         "/tempDir");
27                 File file = new File(tempdir);
28                 if (!file.exists()) {
29                     file.mkdir();
30                 }
31                 factory.setRepository(file);
32                 // 创建处理工具
33                 ServletFileUpload upload = new ServletFileUpload(factory);
34                 // 设置最大允许的尺寸
35                 int setFileSize = Integer.parseInt(sizeMax);
36                 int fileSize = request.getContentLength();
37                 upload.setSizeMax(setFileSize);
38 
39                 if (fileSize <= setFileSize) {
40                     // 解析
41                     String contentType = request.getContentType();
42                     if (contentType == null && "".equals(contentType)) {
43                         mv.addObject("msg", "文件上传失败");  
44                     }
45                     
46                     Class<?> clazz = SysCompanyModel.class;
47                     SysCompanyModel sysCompanyModel = (SysCompanyModel) clazz.newInstance();
48                     if(sysUserModel.getCompanyId()!=null){
49                         sysCompanyModel = sysCompanyService.getSysCompanyModelById(sysUserModel.getCompanyId());
50                     }
51                     
52                     MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
53                     Map<String, MultipartFile> map = multipartRequest.getFileMap();
54                     Enumeration params = multipartRequest.getParameterNames();
55                     while (params.hasMoreElements()) {
56                         String name = (String) params.nextElement();
57                         String value = multipartRequest.getParameter(name);
58                         BeanUtils.setProperty(sysCompanyModel,name,value);
59                     }
60                         
61                     Iterator<String> files = multipartRequest.getFileNames();
62                     
63                     while (files.hasNext()) {
64                         String name = files.next();
65                         MultipartFile multipartFile = multipartRequest.getFile(name);
66                         
67                         if(multipartFile.getSize()!=0){
68                             byte[] data = multipartFile.getBytes();
69                             if(data != null) {
70                                 BeanUtils.setProperty(sysCompanyModel,name, data);
71                             }
72                         }
73                     }
74                     
75                     sysCompanyModel = sysCompanyService.save(sysCompanyModel);
76                     sysUserModel.setCompanyId(sysCompanyModel.getCompanyId());
77                     sysUserModel = sysUserService.save(sysUserModel);
78                     
79                     //request.getSession().setAttribute("userInfo", sysUserModel);
80                     //Context.setRequest(request);
81                     mv.setViewName(MVUtils
82                             .redirect("/member/companyInfo.shtml"));
83                 }else{
84                     mv.addObject("msg", "上传文件过大");
85                 }
86             } catch (Exception e) {
87                 mv.addObject("msg", "文件上传失败");
88             }
89         }
90         return mv;
91     }