|  |  |  | 
|---|
|  |  |  | import com.doumee.core.exception.BusinessException; | 
|---|
|  |  |  | import com.doumee.core.utils.DateUtil; | 
|---|
|  |  |  | import com.doumee.core.utils.FtpUtil; | 
|---|
|  |  |  | import com.doumee.core.utils.azure.AzureBlobUtil; | 
|---|
|  |  |  | import com.doumee.dao.system.model.SystemDictData; | 
|---|
|  |  |  | import com.doumee.service.common.EmailService; | 
|---|
|  |  |  | import io.swagger.annotations.Api; | 
|---|
|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | private SystemDictDataBiz systemDictDataBiz; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private EmailService emailService; | 
|---|
|  |  |  | @Value("${upload.type") | 
|---|
|  |  |  | private String  uploadType; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static FtpUtil ftp  = null; | 
|---|
|  |  |  | public static AzureBlobUtil azureBlobUtil  = null; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "测试邮件发送") | 
|---|
|  |  |  | @RequestMapping(method= RequestMethod.POST,value="/testEmail") | 
|---|
|  |  |  | 
|---|
|  |  |  | 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(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | initUploadTool(); | 
|---|
|  |  |  | context.put("success", true); | 
|---|
|  |  |  | context.put("code", 200); | 
|---|
|  |  |  | context.put("errno",0); | 
|---|
|  |  |  | 
|---|
|  |  |  | String date = DateUtil.getNowShortDate(); | 
|---|
|  |  |  | String fName = date + "/" + UUID.randomUUID() + endType; | 
|---|
|  |  |  | String fileName = folder + fName; | 
|---|
|  |  |  | boolean r = ftp.uploadInputstreamBatch(is, fileName, Constants.equalsInteger(index ,fileList.length),index); | 
|---|
|  |  |  | boolean r =false; | 
|---|
|  |  |  | if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ | 
|---|
|  |  |  | r = azureBlobUtil.uploadFileWithResponse( fileName,is);//上传 | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | r = ftp.uploadInputstreamBatch(is, fileName, Constants.equalsInteger(index ,fileList.length),index); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (r) { | 
|---|
|  |  |  | JSONObject fileJSON = new JSONObject(); | 
|---|
|  |  |  | fileJSON.put("url", prefixPath + fileName); | 
|---|
|  |  |  | 
|---|
|  |  |  | 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(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | initUploadTool(); | 
|---|
|  |  |  | context.put("success", true); | 
|---|
|  |  |  | context.put("code", 200); | 
|---|
|  |  |  | context.put("errno",0); | 
|---|
|  |  |  | 
|---|
|  |  |  | List<MultipartFile> fileList = multipartRequest.getFiles("file"); | 
|---|
|  |  |  | Iterator<String> it = multipartRequest.getFileNames(); | 
|---|
|  |  |  | JSONArray jsonArray = new JSONArray(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (fileList.size() > 0) { | 
|---|
|  |  |  | //遍历文件列表 | 
|---|
|  |  |  | Iterator<MultipartFile> fileIte = fileList.iterator(); | 
|---|
|  |  |  | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | boolean r =false; | 
|---|
|  |  |  | if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ | 
|---|
|  |  |  | r = azureBlobUtil.uploadFileWithResponse( fileName,is);//上传 | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | r = ftp.uploadInputstreamBatch(is, fileName, Constants.equalsInteger(index ,fileList.size()),index); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (r) { | 
|---|
|  |  |  | JSONObject fileJSON = new JSONObject(); | 
|---|
|  |  |  | fileJSON.put("url", prefixPath + fileName); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //                while (it.hasNext()) { | 
|---|
|  |  |  | //                    MultipartFile file = multipartRequest.getFile(it.next()); | 
|---|
|  |  |  | //                    String originname = file.getOriginalFilename(); | 
|---|
|  |  |  | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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(), | 
|---|
|  |  |  | 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{ | 
|---|
|  |  |  | 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(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "上传文件到FTP") | 
|---|
|  |  |  | @RequestMapping(method= RequestMethod.POST,value="/upload") | 
|---|
|  |  |  | @ResponseBody | 
|---|
|  |  |  | 
|---|
|  |  |  | 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(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | initUploadTool(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); | 
|---|
|  |  |  | if (multipartResovler.isMultipart(request)) { | 
|---|
|  |  |  | 
|---|
|  |  |  | String date = DateUtil.getNowShortDate(); | 
|---|
|  |  |  | String fName =  date+"/"+ UUID.randomUUID()+endType; | 
|---|
|  |  |  | String fileName = folder+"/"+fName; | 
|---|
|  |  |  | boolean r = ftp.uploadInputstream(is,fileName); | 
|---|
|  |  |  | boolean r =false; | 
|---|
|  |  |  | if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ | 
|---|
|  |  |  | r = azureBlobUtil.uploadFileWithResponse( fileName,is);//上传 | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | r = ftp.uploadInputstream(is,fileName); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(r){ | 
|---|
|  |  |  | context.put("success", true); | 
|---|
|  |  |  | context.put("code", 200); | 
|---|