| package com.doumee.api.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.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.huaweiOBS.ObsUtil; | 
| import com.doumee.core.utils.qiyeweixin.QywxUtil; | 
| import com.doumee.core.utils.qiyeweixin.model.response.QywxUploadMediaResponse; | 
| import com.doumee.dao.system.model.SystemDictData; | 
| import com.doumee.service.common.EmailService; | 
| import io.swagger.annotations.Api; | 
| import io.swagger.annotations.ApiImplicitParam; | 
| import io.swagger.annotations.ApiImplicitParams; | 
| import io.swagger.annotations.ApiOperation; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| 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; | 
| import javax.servlet.http.HttpServletResponse; | 
| import java.io.File; | 
| import java.io.IOException; | 
| import java.io.InputStream; | 
| import java.io.PrintWriter; | 
| import java.util.*; | 
|   | 
| /** | 
|  * @author Eva.Caesar Liu | 
|  * @date 2023/02/14 11:14 | 
|  */ | 
| @Api(tags = "公共上传接口") | 
| @Trace(exclude = true) | 
| @RestController | 
| @RequestMapping("/web/public") | 
| @Slf4j | 
| public class PublicCloudController extends BaseController { | 
|     @Autowired | 
|     private SystemDictDataBiz systemDictDataBiz; | 
|   | 
|     @ApiOperation(value = "上传OBS", notes = "上传OBS", httpMethod = "POST", position = 6) | 
|     @ApiImplicitParams({ | 
|             @ApiImplicitParam(name = "folder", value = "文件夹", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class), | 
|             @ApiImplicitParam(name = "file", value = "文件夹", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class), | 
|     }) | 
|     @PostMapping(value = "/upload", headers = "content-type=multipart/form-data") | 
|     @ResponseBody | 
|     public void upload(HttpServletRequest request, HttpServletResponse response, String folder,String flag) throws Exception { | 
|         Date d1 = new Date(); | 
|         log.error("总得上传文件成功=============开始========="+DateUtil.getPlusTime2(d1)); | 
|         if(Objects.isNull(folder)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"目标文件夹错误"); | 
|         } | 
|         String prefixPath = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.RESOURCE_PATH).getCode(); | 
|         InputStream is = null; | 
|         response.setCharacterEncoding("UTF-8"); | 
|         response.setContentType("text/html;charset=UTF-8"); | 
|         Map<String, Object> context = new HashMap<>(); | 
|         try { | 
|             CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); | 
|             if (multipartResovler.isMultipart(request)) { | 
|                 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; | 
|                 Iterator<String> it = multipartRequest.getFileNames(); | 
|                 while (it.hasNext()) { | 
|                     MultipartFile file = multipartRequest.getFile(it.next()); | 
|                     System.out.println(file.getSize()); | 
|                     String originname = file.getOriginalFilename(); | 
|                     is = file.getInputStream(); | 
|                     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 =false; | 
|                     ObsUtil obsUtil = new ObsUtil( systemDictDataBiz.queryByCode(Constants.HWY_OBS,Constants.HWY_OBS_ACCESSID).getCode(), | 
|                             systemDictDataBiz.queryByCode(Constants.HWY_OBS,Constants.HWY_OBS_ACCESSKEY).getCode(), | 
|                             systemDictDataBiz.queryByCode(Constants.HWY_OBS,Constants.HWY_OBS_ENDPOINT).getCode()); | 
|                     r = obsUtil.uploadFile( systemDictDataBiz.queryByCode(Constants.HWY_OBS,Constants.HWY_OBS_BUCKET).getCode(),is,fileName);//上传 | 
|                     if(r){ | 
|                         context.put("success", true); | 
|                         context.put("code", 200); | 
|                         context.put("errno",0); | 
|                         JSONObject fileJSON = new JSONObject(); | 
| //                        fileJSON.put("prefixPath", prefixPath); | 
| //                        fileJSON.put("folder", folder); | 
|                         fileJSON.put("url", prefixPath+fileName); | 
|                         fileJSON.put("imgaddr", fName); | 
|                         fileJSON.put("imgname", fileName); | 
|                         fileJSON.put("originname", originname); | 
|                         fileJSON.put("flag", flag); | 
|                         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; | 
|                     } | 
|                 } | 
|             } | 
|         } catch (Exception e) { | 
|             log.error("【上传FTP失败】======================"+e.getMessage()); | 
|         } | 
|         context.put("code", 0); | 
|         context.put("message", "上传失败"); | 
|         context.put("errno",0); | 
|         writerJson(response, context); | 
|         return; | 
|     } | 
|   | 
|   | 
|     public void uploadFileLocal(HttpServletRequest request,  HttpServletResponse response, String rootPath,String dir) throws Exception { | 
|         response.setCharacterEncoding("UTF-8"); | 
|         response.setContentType("text/html;charset=UTF-8"); | 
|         CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); | 
|         Map<String, Object> context = new HashMap<>(); | 
|         if (multipartResovler.isMultipart(request)) { | 
|             MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; | 
|             Iterator<String> it = multipartRequest.getFileNames(); | 
|             while (it.hasNext()) { | 
|                 MultipartFile file = multipartRequest.getFile((String) it.next()); | 
|                 if (file != null) { | 
|                     if (file.getSize() > 200 * 1024 * 1024L) { | 
|                         context.put("code", 4000); | 
|                         context.put("message", "上传文件过大"); | 
|                         return; | 
|                     } | 
|                     System.out.println(file.getOriginalFilename()); | 
|                     if (file.getOriginalFilename() == null) { | 
|                         context.put("code", 4000); | 
|                         context.put("message", "文件名不可为空"); | 
|                         return; | 
|                     } | 
|                     String nowDate = DateUtil.getNowShortDate(); | 
|                     String folder = nowDate + "/"; | 
|                     String strDirPath = rootPath + folder; | 
|                     File dirPath = new File(strDirPath); | 
|                     if (!dirPath.exists()) { | 
|                         dirPath.mkdirs(); | 
|                     } | 
|                     String uploadFileName = file.getOriginalFilename(); | 
|                     String x = UUID.randomUUID().toString().replace("-", "") | 
|                             + uploadFileName.substring(uploadFileName.lastIndexOf(".")); | 
|                     String fileName = folder + x; | 
|                     String fileNames = nowDate + "/" + x; | 
|                     uploadFileName = uploadFileName.replace(" ", ""); | 
|                     uploadFileName = uploadFileName.replace(",", ","); | 
|                     uploadFileName = uploadFileName.replaceAll(",", "-"); | 
|                     String fileAndPath = dir + fileName; | 
|                     // 判断如果临时目录中存在相同名称的文件先删除,在上传 | 
|                     File tempFile = new File(rootPath + fileName); | 
|                     if (tempFile.isFile() && tempFile.exists()) { | 
|                         tempFile.getAbsoluteFile().delete(); | 
|                     } | 
|                     // 上传到服务器临时文件夹 | 
|                     file.transferTo(tempFile); | 
|                     // 转移到FTP服务器 | 
|                     String nfix = ""; | 
|                     if (null != uploadFileName) { | 
|                         nfix = uploadFileName.substring(uploadFileName.lastIndexOf(".")); | 
|                     } | 
|   | 
|                     if (StringUtils.equalsIgnoreCase(nfix, ".exe")) { | 
|                         context.put("code", 4000); | 
|                         context.put("message", "对不起,文件格式\".exe\"上传有误!"); | 
|                         return; | 
|                     } | 
|                     if (StringUtils.equalsIgnoreCase(nfix, ".dll")) { | 
|                         context.put("code", 4000); | 
|                         context.put("message", "对不起,文件格式\".dll\"上传有误!"); | 
|                         return; | 
|                     } | 
|   | 
|                     String remoteFileName = fileAndPath; | 
|   | 
|                     if (true) { | 
|                         context.put("success", true); | 
|                         context.put("code", 200); | 
|                         context.put("errno",0); | 
|                         JSONObject fileJSON = new JSONObject(); | 
|                         fileJSON.put("url", remoteFileName); | 
|                         fileJSON.put("imgaddr", fileNames); | 
|                         fileJSON.put("imgname", uploadFileName); | 
|                         fileJSON.put("originname", uploadFileName); | 
|                         context.put("data",fileJSON); | 
|                         context.put("message","请求成功"); | 
|                         writerJson(response, context); | 
|                         return; | 
|                     } else { | 
|                         // 移动失败 | 
|                         context.put("code", 0); | 
|                         context.put("message", "上传失败"); | 
|                         writerJson(response, context); | 
|                     } | 
|                 } | 
|                 context.put("code", 0); | 
|                 context.put("message", "上传失败"); | 
|                 writerJson(response, context); | 
|                 return; | 
|             } | 
|         } | 
|         context.put("code", 0); | 
|         context.put("message", "上传失败"); | 
|         writerJson(response, context); | 
|         return; | 
|     } | 
|   | 
|   | 
|   | 
|     public static void writerJson(HttpServletResponse response, Object object) { | 
|         response.setContentType("application/json"); | 
|         writer(response, JSONObject.toJSONString(object)); | 
|     } | 
|   | 
|     private static void writer(HttpServletResponse response, String str) { | 
|         try { | 
|             StringBuffer result = new StringBuffer(); | 
|             //设置页面不缓存 | 
|             response.setHeader("Pragma", "No-cache"); | 
|             response.setHeader("Cache-Control", "no-cache"); | 
|             response.setCharacterEncoding("UTF-8"); | 
|             PrintWriter out = null; | 
|             out = response.getWriter(); | 
|             out.print(str); | 
|             out.flush(); | 
|             out.close(); | 
|         } catch (IOException e) { | 
|             e.printStackTrace(); | 
|         } | 
|     } | 
|   | 
| } |