server 端 android上传头像 接口代码

 @ResponseBody
        @RequestMapping("/方法名")
        public AjaxMsg addUserHeadImg(HttpServletRequest request, @RequestParam("文件参数") String image,@RequestParam("其他参数") String userId){
            Map<String, Object> map = new HashMap<>();
               FileOutputStream fos = null;    
               String newFileName =UUID.randomUUID().toString()+".png";
               try{    
                   String imageName =  request.getSession().getServletContext().getRealPath("/") + File.separator + “保存路径”;
                   byte[] buffer = new BASE64Decoder().decodeBuffer(image); 
                   File destDir = new File(imageName);      
                   if(!destDir.exists()) {  
                       destDir.mkdir();    

                   }  
                   fos = new FileOutputStream(new File(destDir,newFileName));     
                   fos.write(buffer);    
                   fos.flush();    
                   fos.close();    
                   userService.uploadHeadImg(headImgPath+newFileName,userId);
                   map.put("result", 0);   //返回

               }catch (Exception e){    

                   e.printStackTrace();  
                   map.put("result", 1);

               }    
             return AjaxMsg.success(map);
        }

posted @ 2018-05-02 14:48  你若盛开  阅读(119)  评论(0编辑  收藏  举报