| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.doumee.api.BaseController; |
| | | import com.doumee.biz.system.SystemDictDataBiz; |
| | | import com.doumee.config.annotation.EncryptionReq; |
| | | import com.doumee.config.annotation.EncryptionResp; |
| | | import com.doumee.core.annotation.trace.Trace; |
| | | import com.doumee.core.constants.Constants; |
| | | import com.doumee.core.constants.ResponseStatus; |
| | | import com.doumee.core.exception.BusinessException; |
| | | import com.doumee.core.model.ApiResponse; |
| | | import com.doumee.core.utils.DateUtil; |
| | | import com.doumee.core.utils.FtpUtil; |
| | | import com.doumee.core.utils.azure.AzureBlobUtil; |
| | | import com.doumee.core.utils.qiyeweixin.QywxUtil; |
| | | import com.doumee.core.utils.qiyeweixin.model.response.QywxUploadMediaResponse; |
| | | import com.doumee.dao.business.dto.WebQwUploadDto; |
| | | import com.doumee.dao.system.model.SystemDictData; |
| | | import com.doumee.service.common.EmailService; |
| | | import io.swagger.annotations.Api; |
| | |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.MultipartHttpServletRequest; |
| | | import org.springframework.web.multipart.MultipartRequest; |
| | | import org.springframework.web.multipart.commons.CommonsMultipartResolver; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | |
| | | private SystemDictDataBiz systemDictDataBiz; |
| | | @Autowired |
| | | private EmailService emailService; |
| | | @Value("${upload.type") |
| | | @Value("${upload.type}") |
| | | private String uploadType; |
| | | |
| | | |
| | | public static FtpUtil ftp = null; |
| | | public static AzureBlobUtil azureBlobUtil = null; |
| | | @GetMapping("/image/{folder}/{date}/{name}") |
| | | public void getImage(@PathVariable String folder,@PathVariable String date,@PathVariable String name, HttpServletResponse response) throws IOException { |
| | | try { |
| | | initUploadTool(); |
| | | response.setContentType("image/jpeg"); // 根据你的图片类型设置正确的MIME类型 |
| | | log.info("===================图片blob名称"+folder+"/"+date+"/"+name); |
| | | azureBlobUtil.downloadBlobToStream(folder+"/"+date+"/"+name,response.getOutputStream()); |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | @ApiOperation(value = "测试企业素材上传下载") |
| | | @RequestMapping(method= RequestMethod.POST,value="/testQwImg") |
| | | @ResponseBody |
| | | public ApiResponse<Map<String, Object>> testQwImg( @RequestParam(required = false) String imgurl ) throws Exception { |
| | | QywxUploadMediaResponse r = QywxUtil.uploadMedia(systemDictDataBiz.queryByCode(Constants.QYWX,Constants.QYWX_TOKEN).getCode(),"image",imgurl); |
| | | if(r!=null && r.getMedia_id()!=null){ |
| | | String folder = systemDictDataBiz.queryByCode(Constants.FTP,Constants.WORKORDER_FILE_PATH).getCode(); |
| | | String prefixPath = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode(); |
| | | Map<String, Object> context = new HashMap<>(); |
| | | try { |
| | | initUploadTool(); |
| | | context.put("success", true); |
| | | context.put("code", 200); |
| | | context.put("errno",0); |
| | | JSONArray jsonArray = new JSONArray(); |
| | | Integer index = Constants.ONE; |
| | | //获得每一个文件 |
| | | String endType = ".jpg"; |
| | | String originname = UUID.randomUUID().toString()+endType; |
| | | InputStream is = QywxUtil.getMediaInputstream(systemDictDataBiz.queryByCode(Constants.QYWX,Constants.QYWX_TOKEN).getCode(),r.getMedia_id()); |
| | | if(is == null){ |
| | | context.put("code", 0); |
| | | context.put("message", "上传失败"); |
| | | context.put("errno",0); |
| | | }else{ |
| | | String date = DateUtil.getNowShortDate(); |
| | | String fName = date + "/" + originname; |
| | | String fileName = folder + fName; |
| | | boolean r1 =false; |
| | | if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ |
| | | r1 = azureBlobUtil.uploadFileWithResponseAndSize( fileName,is,null);//上传 |
| | | }else{ |
| | | r1 = ftp.uploadInputstreamBatch(is, fileName, true,index); |
| | | } |
| | | if (r1) { |
| | | 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); |
| | | |
| | | } |
| | | return ApiResponse.success(context); |
| | | } |
| | | return ApiResponse.failed("上传失败"+r!=null?JSONObject.toJSONString(r):""); |
| | | |
| | | } |
| | | @ApiOperation(value = "测试邮件发送") |
| | | @RequestMapping(method= RequestMethod.POST,value="/testEmail") |
| | | @ResponseBody |
| | |
| | | @ApiOperation(value = "批量上传文件到FTP") |
| | | @RequestMapping(method= RequestMethod.POST,value="/uploadBatch") |
| | | @ResponseBody |
| | | public void uploadBatch(@RequestParam("files") MultipartFile[] fileList,HttpServletRequest request, HttpServletResponse response, String folder) throws Exception { |
| | | public void uploadBatch(MultipartRequest request, HttpServletResponse response, String folder) throws Exception { |
| | | if(Objects.isNull(folder)){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST); |
| | | } |
| | | Set<String> setList = request.getMultiFileMap().keySet(); |
| | | if(Objects.isNull(setList)){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST); |
| | | } |
| | | List<MultipartFile> fileList = new ArrayList<>(); |
| | | for(String str:setList){ |
| | | List<MultipartFile> fl = request.getMultiFileMap().get(str); |
| | | fileList.addAll(fl); |
| | | } |
| | | if(Objects.isNull(fileList)){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST); |
| | |
| | | context.put("success", true); |
| | | context.put("code", 200); |
| | | context.put("errno",0); |
| | | JSONArray jsonArray = new JSONArray(); |
| | | if (fileList.length > 0) { |
| | | JSONArray jsonArray = new JSONArray(); |
| | | if (fileList.size() > 0) { |
| | | //遍历文件列表 |
| | | Integer index = Constants.ONE; |
| | | for (MultipartFile file :fileList) { |
| | | //获得每一个文件 |
| | | String originname = file.getOriginalFilename(); |
| | | log.error("【上传FTP成功】=============开始========="+index+"===="+originname); |
| | | InputStream is = file.getInputStream(); |
| | | String endType = originname.substring(originname.lastIndexOf("."), originname.length()); |
| | | String date = DateUtil.getNowShortDate(); |
| | |
| | | String fileName = folder + fName; |
| | | boolean r =false; |
| | | if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ |
| | | r = azureBlobUtil.uploadFileWithResponse( fileName,is);//上传 |
| | | r = azureBlobUtil.uploadFileWithResponseAndSize( fileName,is,null);//上传 |
| | | }else{ |
| | | r = ftp.uploadInputstreamBatch(is, fileName, Constants.equalsInteger(index ,fileList.length),index); |
| | | r = ftp.uploadInputstreamBatch(is, fileName, Constants.equalsInteger(index ,fileList.size()),index); |
| | | } |
| | | if (r) { |
| | | JSONObject fileJSON = new JSONObject(); |
| | |
| | | fileJSON.put("originname", originname); |
| | | jsonArray.add(fileJSON); |
| | | } |
| | | log.error("【上传FTP成功】=============结束========="+index+"====="+r+"===="+fName); |
| | | index = index + 1; |
| | | } |
| | | context.put("data",jsonArray); |
| | |
| | | writerJson(response, context); |
| | | return; |
| | | } |
| | | @ApiOperation(value = "批量从企业临时素材库上传文件到文件服务器") |
| | | @PostMapping(value="/uploadBatchFromWx") |
| | | @ResponseBody |
| | | public void uploadBatchFromWx(@RequestBody WebQwUploadDto param, HttpServletResponse response) throws Exception { |
| | | if(Objects.isNull(param.getFolder()) || param.getMediaIds()==null || param.getMediaIds().size()==0){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST); |
| | | } |
| | | SystemDictData folderData = systemDictDataBiz.queryByCode(Constants.FTP,param.getFolder()); |
| | | if(Objects.isNull(folderData)||StringUtils.isBlank(folderData.getCode())){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"目标文件夹错误"); |
| | | } |
| | | String folder = systemDictDataBiz.queryByCode(Constants.FTP,param.getFolder()).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 { |
| | | initUploadTool(); |
| | | context.put("success", true); |
| | | context.put("code", 200); |
| | | context.put("errno",0); |
| | | JSONArray jsonArray = new JSONArray(); |
| | | Integer index = Constants.ONE; |
| | | for (String media : param.getMediaIds()) { |
| | | //获得每一个文件 |
| | | String endType = ".jpg"; |
| | | String originname = UUID.randomUUID().toString()+endType; |
| | | Map<String,Object> map = QywxUtil.getMediaInputstreamMap(systemDictDataBiz.queryByCode(Constants.QYWX,Constants.QYWX_TOKEN).getCode(),media); |
| | | if(map == null || map.get("data") == null){ |
| | | log.error("企业微信素材下载失败:========="+media); |
| | | continue; |
| | | } |
| | | log.error("企业微信素材下载成功:========="+media); |
| | | String date = DateUtil.getNowShortDate(); |
| | | String fName = date + "/" + originname; |
| | | String fileName = folder + fName; |
| | | boolean r =false; |
| | | if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ |
| | | r = azureBlobUtil.uploadFileWithResponseAndSize( fileName, (InputStream) map.get("data"),(Integer)map.get("size"));//上传 |
| | | }else{ |
| | | r = ftp.uploadInputstreamBatch( (InputStream) map.get("data"), fileName, Constants.equalsInteger(index ,param.getMediaIds().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; |
| | | } |
| | | 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="/uploadBatchOld") |
| | | @ResponseBody |
| | |
| | | String fileName = folder + fName; |
| | | boolean r =false; |
| | | if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ |
| | | r = azureBlobUtil.uploadFileWithResponse( fileName,is);//上传 |
| | | r = azureBlobUtil.uploadFileWithResponseAndSize( fileName,is,null);//上传 |
| | | }else{ |
| | | r = ftp.uploadInputstreamBatch(is, fileName, Constants.equalsInteger(index ,fileList.size()),index); |
| | | } |
| | |
| | | } |
| | | 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) { |
| | |
| | | private void initUploadTool() throws Exception{ |
| | | if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ |
| | | if(azureBlobUtil == null){ |
| | | azureBlobUtil = new AzureBlobUtil(systemDictDataBiz.queryByCode(Constants.AZURE_BLOB,Constants.AZURE_BLOB_ACCESSNAME).getCode(), |
| | | azureBlobUtil = new AzureBlobUtil( |
| | | systemDictDataBiz.queryByCode(Constants.AZURE_BLOB,Constants.AZURE_BLOB_ACCESSNAME).getCode(), |
| | | systemDictDataBiz.queryByCode(Constants.AZURE_BLOB,Constants.AZURE_BLOB_ACCESSKEY).getCode(), |
| | | systemDictDataBiz.queryByCode(Constants.AZURE_BLOB,Constants.AZURE_BLOB_ENDPOINT).getCode(), |
| | | systemDictDataBiz.queryByCode(Constants.AZURE_BLOB,Constants.AZURE_BLOB_CONTAINER).getCode()); |
| | | }else{ |
| | | azureBlobUtil.initClient(); |
| | | } |
| | | }else{ |
| | | if(ftp == null){ |
| | |
| | | String fileName = folder+"/"+fName; |
| | | boolean r =false; |
| | | if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ |
| | | r = azureBlobUtil.uploadFileWithResponse( fileName,is);//上传 |
| | | r = azureBlobUtil.uploadFileWithResponseAndSize( fileName,is,null);//上传 |
| | | }else{ |
| | | r = ftp.uploadInputstream(is,fileName); |
| | | } |