|  |  |  | 
|---|
|  |  |  | package com.doumee.cloud.common; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONArray; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.doumee.api.BaseController; | 
|---|
|  |  |  | import com.doumee.biz.system.SystemDictDataBiz; | 
|---|
|  |  |  | import com.doumee.core.annotation.trace.Trace; | 
|---|
|  |  |  | import com.doumee.core.constants.ResponseStatus; | 
|---|
|  |  |  | import com.doumee.core.exception.BusinessException; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.HKConstants; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.param.BaseResponse; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.param.request.FacePictureCheckRequest; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.param.respose.FacePictureCheckResponse; | 
|---|
|  |  |  | import com.doumee.core.haikang.service.HKService; | 
|---|
|  |  |  | import com.doumee.core.utils.Constants; | 
|---|
|  |  |  | import com.doumee.core.utils.DateUtil; | 
|---|
|  |  |  | import com.doumee.core.utils.FtpUtil; | 
|---|
|  |  |  | import com.doumee.core.utils.aliyun.ALiYunUtil; | 
|---|
|  |  |  | import com.doumee.dao.system.model.SystemDict; | 
|---|
|  |  |  | import com.doumee.dao.system.model.SystemDictData; | 
|---|
|  |  |  | import io.swagger.annotations.Api; | 
|---|
|  |  |  | import io.swagger.annotations.ApiImplicitParam; | 
|---|
|  |  |  | import io.swagger.annotations.ApiImplicitParams; | 
|---|
|  |  |  | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.io.InputStream; | 
|---|
|  |  |  | import java.io.PrintWriter; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.Iterator; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.UUID; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author Eva.Caesar Liu | 
|---|
|  |  |  | 
|---|
|  |  |  | @RequestMapping(Constants.CLOUD_SERVICE_URL_INDEX+"/public") | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | public class PublicCloudController extends BaseController { | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SystemDictDataBiz systemDictDataBiz; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static FtpUtil ftp  = null; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "批量上传文件到FTP") | 
|---|
|  |  |  | @RequestMapping(method= RequestMethod.POST,value="/uploadBatch") | 
|---|
|  |  |  | @ResponseBody | 
|---|
|  |  |  | public void uploadBatch(HttpServletRequest request, HttpServletResponse response, String folder) throws Exception { | 
|---|
|  |  |  | if(Objects.isNull(folder)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.BAD_REQUEST); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | SystemDictData folderData =  systemDictDataBiz.queryByCode(Constants.FTP,folder); | 
|---|
|  |  |  | if(Objects.isNull(folderData)||StringUtils.isBlank(folderData.getCode())){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"目标文件夹错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | folder = systemDictDataBiz.queryByCode(Constants.FTP,folder).getCode(); | 
|---|
|  |  |  | String prefixPath = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode(); | 
|---|
|  |  |  | response.setCharacterEncoding("UTF-8"); | 
|---|
|  |  |  | response.setContentType("text/html;charset=UTF-8"); | 
|---|
|  |  |  | Map<String, Object> context = new HashMap<>(); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(ftp == null){ | 
|---|
|  |  |  | ftp = new FtpUtil(systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_HOST).getCode(), | 
|---|
|  |  |  | Integer.parseInt(systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_PORT).getCode()), | 
|---|
|  |  |  | systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_USERNAME).getCode(), | 
|---|
|  |  |  | systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_PWD).getCode()); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | ftp.connect(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | context.put("success", true); | 
|---|
|  |  |  | context.put("code", 200); | 
|---|
|  |  |  | context.put("errno",0); | 
|---|
|  |  |  | CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); | 
|---|
|  |  |  | if (multipartResovler.isMultipart(request)) { | 
|---|
|  |  |  | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; | 
|---|
|  |  |  | List<MultipartFile> fileList = multipartRequest.getFiles("file"); | 
|---|
|  |  |  | Iterator<String> it = multipartRequest.getFileNames(); | 
|---|
|  |  |  | JSONArray jsonArray = new JSONArray(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (fileList.size() > 0) { | 
|---|
|  |  |  | //遍历文件列表 | 
|---|
|  |  |  | Iterator<MultipartFile> fileIte = fileList.iterator(); | 
|---|
|  |  |  | Integer index = Constants.ONE; | 
|---|
|  |  |  | while (fileIte.hasNext()) { | 
|---|
|  |  |  | //获得每一个文件 | 
|---|
|  |  |  | MultipartFile file = fileIte.next(); | 
|---|
|  |  |  | String originname = file.getOriginalFilename(); | 
|---|
|  |  |  | InputStream is = file.getInputStream(); | 
|---|
|  |  |  | String endType = originname.substring(originname.lastIndexOf("."), originname.length()); | 
|---|
|  |  |  | String date = DateUtil.getNowShortDate(); | 
|---|
|  |  |  | String fName = date + "/" + UUID.randomUUID() + endType; | 
|---|
|  |  |  | String fileName = folder + fName; | 
|---|
|  |  |  | boolean r = ftp.uploadInputstreamBatch(is, fileName, Constants.equalsInteger(index ,fileList.size()),index); | 
|---|
|  |  |  | if (r) { | 
|---|
|  |  |  | JSONObject fileJSON = new JSONObject(); | 
|---|
|  |  |  | fileJSON.put("url", prefixPath + fileName); | 
|---|
|  |  |  | fileJSON.put("imgaddr", fName); | 
|---|
|  |  |  | fileJSON.put("imgname", fileName); | 
|---|
|  |  |  | fileJSON.put("originname", originname); | 
|---|
|  |  |  | jsonArray.add(fileJSON); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | index = index + 1; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //                while (it.hasNext()) { | 
|---|
|  |  |  | //                    MultipartFile file = multipartRequest.getFile(it.next()); | 
|---|
|  |  |  | //                    String originname = file.getOriginalFilename(); | 
|---|
|  |  |  | //                    is = file.getInputStream(); | 
|---|
|  |  |  | //                    String endType = originname.substring(originname.lastIndexOf("."),originname.length()); | 
|---|
|  |  |  | //                    String date = DateUtil.getNowShortDate(); | 
|---|
|  |  |  | //                    String fName =  date+"/"+ UUID.randomUUID()+endType; | 
|---|
|  |  |  | //                    String fileName = folder+"/"+fName; | 
|---|
|  |  |  | //                    boolean r = ftp.uploadInputstream(is,fileName); | 
|---|
|  |  |  | //                    if(r){ | 
|---|
|  |  |  | //                        JSONObject fileJSON = new JSONObject(); | 
|---|
|  |  |  | //                        fileJSON.put("url", prefixPath+fileName); | 
|---|
|  |  |  | //                        fileJSON.put("imgaddr", fName); | 
|---|
|  |  |  | //                        fileJSON.put("imgname", fileName); | 
|---|
|  |  |  | //                        fileJSON.put("originname", originname); | 
|---|
|  |  |  | //                        jsonArray.add(fileJSON); | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | context.put("data",jsonArray); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("【上传FTP失败】======================"+e.getMessage()); | 
|---|
|  |  |  | context.put("code", 0); | 
|---|
|  |  |  | context.put("message", "上传失败"); | 
|---|
|  |  |  | context.put("errno",0); | 
|---|
|  |  |  | writerJson(response, context); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | writerJson(response, context); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "上传文件到FTP") | 
|---|
|  |  |  | @RequestMapping(method= RequestMethod.POST,value="/upload") | 
|---|
|  |  |  | @ResponseBody | 
|---|
|  |  |  | public void upload(HttpServletRequest request, HttpServletResponse response, String folder) throws Exception { | 
|---|
|  |  |  | public void upload(HttpServletRequest request, HttpServletResponse response, String folder,Integer isFace) throws Exception { | 
|---|
|  |  |  | //        folder = systemDictDataBiz.queryByCode(Constants.FTP,folder).getCode(); | 
|---|
|  |  |  | Date d1 = new Date(); | 
|---|
|  |  |  | log.error("总得上传文件成功=============开始========="+DateUtil.getPlusTime2(d1)); | 
|---|
|  |  |  | folder =StringUtils.defaultString(folder).replace("/", ""); | 
|---|
|  |  |  | String prefixPath = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode(); | 
|---|
|  |  |  | InputStream is = null; | 
|---|
|  |  |  | 
|---|
|  |  |  | MultipartFile file = multipartRequest.getFile(it.next()); | 
|---|
|  |  |  | String originname = file.getOriginalFilename(); | 
|---|
|  |  |  | is = file.getInputStream(); | 
|---|
|  |  |  | String endType = originname.substring(originname.lastIndexOf("."),originname.length()); | 
|---|
|  |  |  | String endType = ".jpg"; | 
|---|
|  |  |  | if(originname.indexOf(".")>0){ | 
|---|
|  |  |  | endType=originname.substring(originname.lastIndexOf("."),originname.length()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String date = DateUtil.getNowShortDate(); | 
|---|
|  |  |  | String fName =  date+"/"+ UUID.randomUUID()+endType; | 
|---|
|  |  |  | String fileName = folder+"/"+fName; | 
|---|
|  |  |  | boolean r = ftp.uploadInputstream(is,fileName); | 
|---|
|  |  |  | if(r){ | 
|---|
|  |  |  | if(Objects.nonNull(isFace) && Constants.equalsInteger(isFace,Constants.ZERO)){ | 
|---|
|  |  |  | //验证人脸评分 | 
|---|
|  |  |  | //人脸评分 | 
|---|
|  |  |  | FacePictureCheckRequest param = new FacePictureCheckRequest(); | 
|---|
|  |  |  | String prefixUrl = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_LOCAL_RESOURCE_PATH).getCode(); | 
|---|
|  |  |  | param.setFacePicUrl(prefixUrl + fileName); | 
|---|
|  |  |  | BaseResponse<FacePictureCheckResponse> responseBaseResponse = HKService.facePictureCheck(param); | 
|---|
|  |  |  | if(responseBaseResponse == null || !StringUtils.equals(responseBaseResponse.getCode(), HKConstants.RESPONSE_SUCCEE)){ | 
|---|
|  |  |  | context.put("code", 0); | 
|---|
|  |  |  | context.put("message", "对不起,人脸评分获取数据失败~"); | 
|---|
|  |  |  | context.put("errno",0); | 
|---|
|  |  |  | writerJson(response, context); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | FacePictureCheckResponse facePictureCheckResponse = responseBaseResponse.getData(); | 
|---|
|  |  |  | if(Objects.isNull(facePictureCheckResponse) || Objects.isNull(facePictureCheckResponse.getCheckResult())){ | 
|---|
|  |  |  | context.put("code", 0); | 
|---|
|  |  |  | context.put("message", "对不起,人脸评分获取数据失败~"); | 
|---|
|  |  |  | context.put("errno",0); | 
|---|
|  |  |  | writerJson(response, context); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(!facePictureCheckResponse.getCheckResult()){ | 
|---|
|  |  |  | context.put("code", 0); | 
|---|
|  |  |  | context.put("message", facePictureCheckResponse.getStatusMessage()); | 
|---|
|  |  |  | context.put("errno",0); | 
|---|
|  |  |  | writerJson(response, context); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | context.put("success", true); | 
|---|
|  |  |  | context.put("code", 200); | 
|---|
|  |  |  | context.put("errno",0); | 
|---|
|  |  |  | 
|---|
|  |  |  | context.put("data",fileJSON); | 
|---|
|  |  |  | context.put("message","请求成功"); | 
|---|
|  |  |  | writerJson(response, context); | 
|---|
|  |  |  | Date d2= new Date(); | 
|---|
|  |  |  | log.error("总的上传文件成功=============结束========="+DateUtil.getPlusTime2(d2) +"耗时秒:"+( (d2.getTime()-d1.getTime()) /1000)); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void uploadFileLocal(HttpServletRequest request, String folder, HttpServletResponse response, String rootPath,String dir) throws Exception { | 
|---|
|  |  |  | response.setCharacterEncoding("UTF-8"); | 
|---|