| @@ -1,13 +1,11 @@ | |||||
| package com.iformall.controller.sys; | package com.iformall.controller.sys; | ||||
| import com.amazonaws.services.s3.model.*; | |||||
| import com.iformall.annotation.SystemControllerLog; | import com.iformall.annotation.SystemControllerLog; | ||||
| import com.iformall.common.ErrorCode; | import com.iformall.common.ErrorCode; | ||||
| import com.iformall.common.ResultData; | import com.iformall.common.ResultData; | ||||
| import com.iformall.controller.base.BaseController; | import com.iformall.controller.base.BaseController; | ||||
| import com.iformall.domain.po.base.TenantEntity; | import com.iformall.domain.po.base.TenantEntity; | ||||
| import com.iformall.file.aliyun.AliyunOSS; | import com.iformall.file.aliyun.AliyunOSS; | ||||
| import com.iformall.service.QrCodeService; | |||||
| import com.iformall.utils.ImgUtil; | import com.iformall.utils.ImgUtil; | ||||
| import io.swagger.annotations.Api; | import io.swagger.annotations.Api; | ||||
| import io.swagger.annotations.ApiOperation; | import io.swagger.annotations.ApiOperation; | ||||
| @@ -37,18 +35,6 @@ public class UploadController extends BaseController { | |||||
| @Autowired | @Autowired | ||||
| private AliyunOSS aliyunOSS; | private AliyunOSS aliyunOSS; | ||||
| @Autowired | |||||
| private QrCodeService qrCodeService; | |||||
| private String getFileName(TenantEntity tenantEntity, String fileName) { | |||||
| if (StringUtils.isBlank(tenantEntity.getParentTenantId())) { | |||||
| fileName = tenantEntity.getTenantId() + "/" + fileName; | |||||
| } else { | |||||
| fileName = tenantEntity.getTenantId() + "/" + tenantEntity.getParentTenantId() + "/" +fileName; | |||||
| } | |||||
| return fileName; | |||||
| } | |||||
| /** | /** | ||||
| * 上传文件 | * 上传文件 | ||||
| * | * | ||||
| @@ -64,21 +50,12 @@ public class UploadController extends BaseController { | |||||
| TenantEntity tenantEntity = getTenantInfo(); | TenantEntity tenantEntity = getTenantInfo(); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(multiReq.getSize()); | |||||
| String fileName = UUID.randomUUID().toString(); | |||||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | ||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | ||||
| fileName = getFileName(tenantEntity, fileName + fileFormat); | |||||
| } else { | |||||
| fileName = getFileName(tenantEntity, fileName); | |||||
| } | } | ||||
| // ResultData data = qrCodeService.awsUpload(multiReq.getInputStream(), metadata, fileName, tenantEntity); | |||||
| ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ||||
| return data; | return data; | ||||
| } | } | ||||
| @@ -103,16 +80,13 @@ public class UploadController extends BaseController { | |||||
| if (size > length) { | if (size > length) { | ||||
| return new ResultData(ErrorCode.PICTURE_SIZE_EXCEED); | return new ResultData(ErrorCode.PICTURE_SIZE_EXCEED); | ||||
| } | } | ||||
| String fileName = UUID.randomUUID().toString(); | |||||
| String tempFileName = fileName; | |||||
| String tempFileName = UUID.randomUUID().toString(); | |||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | ||||
| fileName = getFileName(tenantEntity, fileName + fileFormat); | |||||
| } else { | |||||
| fileName = getFileName(tenantEntity, fileName); | |||||
| } | } | ||||
| @@ -138,19 +112,11 @@ public class UploadController extends BaseController { | |||||
| logger.info("local new file: " + localNewImgFileName); | logger.info("local new file: " + localNewImgFileName); | ||||
| // 删除本地缓存 | // 删除本地缓存 | ||||
| localFile.delete(); | localFile.delete(); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(newFile.length()); | |||||
| // ResultData data = qrCodeService.awsUpload(new FileInputStream(newFile), metadata, fileName, tenantEntity); | |||||
| ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, new FileInputStream(newFile)); | ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, new FileInputStream(newFile)); | ||||
| // 删除本地缓存 | // 删除本地缓存 | ||||
| newFile.delete(); | newFile.delete(); | ||||
| return data; | return data; | ||||
| } else { | } else { | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(size); | |||||
| // ResultData data = qrCodeService.awsUpload(multiReq.getInputStream(), metadata, fileName, tenantEntity); | |||||
| ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ||||
| return data; | return data; | ||||
| } | } | ||||
| @@ -180,26 +146,14 @@ public class UploadController extends BaseController { | |||||
| for(MultipartFile multipartFile: files) { | for(MultipartFile multipartFile: files) { | ||||
| Map<String, String> map = new HashMap<>(); | Map<String, String> map = new HashMap<>(); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multipartFile.getContentType()); | |||||
| metadata.setContentLength(multipartFile.getSize()); | |||||
| FileOutputStream fos = null; | |||||
| BufferedInputStream fs = null; | |||||
| map.put("key", multipartFile.getOriginalFilename()); | map.put("key", multipartFile.getOriginalFilename()); | ||||
| String fileName = UUID.randomUUID().toString(); | |||||
| int dot = multipartFile.getOriginalFilename().lastIndexOf('.'); | int dot = multipartFile.getOriginalFilename().lastIndexOf('.'); | ||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multipartFile.getOriginalFilename().substring(dot, multipartFile.getOriginalFilename().length());; | fileFormat = multipartFile.getOriginalFilename().substring(dot, multipartFile.getOriginalFilename().length());; | ||||
| fileName = getFileName(tenantEntity, fileName + fileFormat); | |||||
| } else { | |||||
| fileName = getFileName(tenantEntity, fileName); | |||||
| } | } | ||||
| // ResultData data1 = qrCodeService.awsUpload(multipartFile.getInputStream(), metadata, fileName, getTenantInfo()); | |||||
| ResultData data1 = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multipartFile.getInputStream()); | ResultData data1 = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multipartFile.getInputStream()); | ||||
| if(data1.code == ResultData.SUCCESS) { | if(data1.code == ResultData.SUCCESS) { | ||||
| @@ -233,21 +187,12 @@ public class UploadController extends BaseController { | |||||
| public ResultData cimgUpload(@RequestParam("file") MultipartFile multiReq) throws Exception{ | public ResultData cimgUpload(@RequestParam("file") MultipartFile multiReq) throws Exception{ | ||||
| logger.info("[" + getIpAddr() + "] UploadController::cimgUpload"); | logger.info("[" + getIpAddr() + "] UploadController::cimgUpload"); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(multiReq.getSize()); | |||||
| String fileName = multiReq.getOriginalFilename(); | |||||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | ||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | ||||
| } | } | ||||
| fileName = "cimg/" + fileName; | |||||
| // ResultData data = qrCodeService.awsUpload(multiReq.getInputStream(), metadata, fileName, new TenantEntity() {{ | |||||
| // setTenantId("cimg"); | |||||
| // }}); | |||||
| ResultData data = aliyunOSS.uploadFile("cimg", fileFormat, multiReq.getInputStream()); | ResultData data = aliyunOSS.uploadFile("cimg", fileFormat, multiReq.getInputStream()); | ||||
| return data; | return data; | ||||
| } | } | ||||
| @@ -1,22 +1,9 @@ | |||||
| package com.iformall.controller; | package com.iformall.controller; | ||||
| import com.amazonaws.AmazonClientException; | |||||
| import com.amazonaws.AmazonServiceException; | |||||
| import com.amazonaws.auth.AWSCredentials; | |||||
| import com.amazonaws.auth.AWSCredentialsProvider; | |||||
| import com.amazonaws.auth.BasicAWSCredentials; | |||||
| import com.amazonaws.services.s3.AmazonS3; | |||||
| import com.amazonaws.services.s3.AmazonS3ClientBuilder; | |||||
| import com.amazonaws.services.s3.model.CannedAccessControlList; | |||||
| import com.amazonaws.services.s3.model.ObjectMetadata; | |||||
| import com.amazonaws.services.s3.model.PutObjectRequest; | |||||
| import com.iformall.common.ResultData; | import com.iformall.common.ResultData; | ||||
| import com.iformall.config.AwsProperty; | |||||
| import com.iformall.domain.po.MallResource; | |||||
| import com.iformall.domain.po.base.TenantEntity; | import com.iformall.domain.po.base.TenantEntity; | ||||
| import com.iformall.file.aliyun.AliyunOSS; | import com.iformall.file.aliyun.AliyunOSS; | ||||
| import com.iformall.service.MallResourceService; | |||||
| import com.iformall.utils.ImgUtil; | import com.iformall.utils.ImgUtil; | ||||
| import io.swagger.annotations.Api; | import io.swagger.annotations.Api; | ||||
| import io.swagger.annotations.ApiOperation; | import io.swagger.annotations.ApiOperation; | ||||
| @@ -31,7 +18,6 @@ import org.springframework.web.bind.annotation.RestController; | |||||
| import org.springframework.web.multipart.MultipartFile; | import org.springframework.web.multipart.MultipartFile; | ||||
| import java.io.*; | import java.io.*; | ||||
| import java.net.URL; | |||||
| import java.util.*; | import java.util.*; | ||||
| import java.util.List; | import java.util.List; | ||||
| @@ -42,94 +28,12 @@ public class UploadController extends BaseController { | |||||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||
| @Autowired | |||||
| private AwsProperty awsProperty; | |||||
| @Autowired | |||||
| private MallResourceService mallResourceService; | |||||
| @Autowired | @Autowired | ||||
| private String fmUploadDir; | private String fmUploadDir; | ||||
| @Autowired | @Autowired | ||||
| private AliyunOSS aliyunOSS; | private AliyunOSS aliyunOSS; | ||||
| private AmazonS3 s3 = null; | |||||
| private String getFileName(TenantEntity tenantEntity, String fileName) { | |||||
| if (StringUtils.isBlank(tenantEntity.getParentTenantId())) { | |||||
| fileName = tenantEntity.getTenantId() + "/" + fileName; | |||||
| } else { | |||||
| fileName = tenantEntity.getTenantId() + "/" + tenantEntity.getParentTenantId() + "/" + fileName; | |||||
| } | |||||
| return fileName; | |||||
| } | |||||
| private ResultData awsUpload(InputStream inputStream, ObjectMetadata metadata, String fileName, TenantEntity tenantEntity) { | |||||
| ResultData data = new ResultData(); | |||||
| try { | |||||
| if (s3 == null) { | |||||
| s3 = AmazonS3ClientBuilder.standard() | |||||
| .withRegion(awsProperty.getClientRegion()) | |||||
| .withCredentials(new AWSCredentialsProvider() { | |||||
| @Override | |||||
| public AWSCredentials getCredentials() { | |||||
| return new BasicAWSCredentials(awsProperty.getAccess(), awsProperty.getSecret()); | |||||
| } | |||||
| @Override | |||||
| public void refresh() { | |||||
| } | |||||
| }) | |||||
| .build(); | |||||
| } | |||||
| s3.putObject( | |||||
| new PutObjectRequest(awsProperty.getBucketName(), fileName, inputStream, metadata) | |||||
| .withCannedAcl(CannedAccessControlList.PublicRead)); | |||||
| URL url = s3.getUrl(awsProperty.getBucketName(), fileName); | |||||
| logger.info(url.toString()); | |||||
| MallResource mr = new MallResource(); | |||||
| mr.updateTenantInfo(tenantEntity); | |||||
| mr.setBucket(awsProperty.getBucketName()); | |||||
| mr.setS3Key(fileName); | |||||
| mr.setUrl(url.toString()); | |||||
| mallResourceService.saveOrUpdate(mr); | |||||
| data.code = ResultData.SUCCESS; | |||||
| Map<String, String> map = new HashMap<>(); | |||||
| map.put("url", url.toString()); | |||||
| data.data = map; | |||||
| } catch (AmazonServiceException ase) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an AmazonServiceException, which " + | |||||
| "means your request made it " + | |||||
| "to Amazon S3, but was rejected with an error response" + | |||||
| " for some reason."); | |||||
| logger.warn(ase.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } catch (AmazonClientException ace) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an AmazonClientException, which " + | |||||
| "means the client encountered " + | |||||
| "an internal error while trying to " + | |||||
| "communicate with S3, " + | |||||
| "such as not being able to access the network."); | |||||
| logger.warn("Error Message: " + ace.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } catch (Exception ioe) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an IOException: " + ioe.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } | |||||
| return data; | |||||
| } | |||||
| /** | /** | ||||
| * 上传文件 | * 上传文件 | ||||
| * | * | ||||
| @@ -144,24 +48,12 @@ public class UploadController extends BaseController { | |||||
| TenantEntity tenantEntity = getTenantInfo(); | TenantEntity tenantEntity = getTenantInfo(); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(multiReq.getSize()); | |||||
| FileOutputStream fos = null; | |||||
| BufferedInputStream fs = null; | |||||
| String fileName = UUID.randomUUID().toString(); | |||||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | ||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | ||||
| fileName = getFileName(tenantEntity, fileName + fileFormat); | |||||
| } else { | |||||
| fileName = getFileName(tenantEntity, fileName); | |||||
| } | } | ||||
| // ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, tenantEntity); | |||||
| ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ||||
| return data; | return data; | ||||
| } | } | ||||
| @@ -180,20 +72,14 @@ public class UploadController extends BaseController { | |||||
| TenantEntity tenantEntity = getTenantInfo(); | TenantEntity tenantEntity = getTenantInfo(); | ||||
| String fileName = UUID.randomUUID().toString(); | |||||
| String tempFileName = fileName; | |||||
| String tempFileName = UUID.randomUUID().toString(); | |||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | ||||
| fileName = getFileName(tenantEntity, fileName + fileFormat); | |||||
| } else { | |||||
| fileName = getFileName(tenantEntity, fileName); | |||||
| } | } | ||||
| System.out.println(fileName); | |||||
| FileOutputStream fos = null; | FileOutputStream fos = null; | ||||
| String imgFormat = ImgUtil.getImgFormat(fileFormat); | String imgFormat = ImgUtil.getImgFormat(fileFormat); | ||||
| @@ -215,19 +101,11 @@ public class UploadController extends BaseController { | |||||
| File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName, imgFormat); | File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName, imgFormat); | ||||
| // 删除本地缓存 | // 删除本地缓存 | ||||
| localFile.delete(); | localFile.delete(); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(newFile.length()); | |||||
| // ResultData data = awsUpload(new FileInputStream(newFile), metadata, fileName, tenantEntity); | |||||
| ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, new FileInputStream(newFile)); | ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, new FileInputStream(newFile)); | ||||
| // 删除本地缓存 | // 删除本地缓存 | ||||
| newFile.delete(); | newFile.delete(); | ||||
| return data; | return data; | ||||
| } else { | } else { | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(multiReq.getSize()); | |||||
| // ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, tenantEntity); | |||||
| ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ||||
| return data; | return data; | ||||
| } | } | ||||
| @@ -253,26 +131,14 @@ public class UploadController extends BaseController { | |||||
| for (MultipartFile multipartFile : files) { | for (MultipartFile multipartFile : files) { | ||||
| Map<String, String> map = new HashMap<>(); | Map<String, String> map = new HashMap<>(); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multipartFile.getContentType()); | |||||
| metadata.setContentLength(multipartFile.getSize()); | |||||
| FileOutputStream fos = null; | |||||
| BufferedInputStream fs = null; | |||||
| map.put("key", multipartFile.getOriginalFilename()); | map.put("key", multipartFile.getOriginalFilename()); | ||||
| String fileName = UUID.randomUUID().toString(); | |||||
| int dot = multipartFile.getOriginalFilename().lastIndexOf('.'); | int dot = multipartFile.getOriginalFilename().lastIndexOf('.'); | ||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multipartFile.getOriginalFilename().substring(dot, multipartFile.getOriginalFilename().length()); | fileFormat = multipartFile.getOriginalFilename().substring(dot, multipartFile.getOriginalFilename().length()); | ||||
| getFileName(tenantEntity, fileName + fileFormat); | |||||
| } else { | |||||
| getFileName(tenantEntity, fileName); | |||||
| } | } | ||||
| // ResultData data1 = awsUpload(multipartFile.getInputStream(), metadata, fileName, tenantEntity); | |||||
| ResultData data1 = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multipartFile.getInputStream()); | ResultData data1 = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multipartFile.getInputStream()); | ||||
| if (data1.code == ResultData.SUCCESS) { | if (data1.code == ResultData.SUCCESS) { | ||||
| Map _data = (Map) data1.data; | Map _data = (Map) data1.data; | ||||
| @@ -1,24 +1,11 @@ | |||||
| package com.iformall.controller; | package com.iformall.controller; | ||||
| import com.amazonaws.AmazonClientException; | |||||
| import com.amazonaws.AmazonServiceException; | |||||
| import com.amazonaws.auth.AWSCredentials; | |||||
| import com.amazonaws.auth.AWSCredentialsProvider; | |||||
| import com.amazonaws.auth.BasicAWSCredentials; | |||||
| import com.amazonaws.services.s3.AmazonS3; | |||||
| import com.amazonaws.services.s3.AmazonS3ClientBuilder; | |||||
| import com.amazonaws.services.s3.model.CannedAccessControlList; | |||||
| import com.amazonaws.services.s3.model.ObjectMetadata; | |||||
| import com.amazonaws.services.s3.model.PutObjectRequest; | |||||
| import com.iformall.common.ErrorCode; | import com.iformall.common.ErrorCode; | ||||
| import com.iformall.common.ResultData; | import com.iformall.common.ResultData; | ||||
| import com.iformall.config.AwsProperty; | |||||
| import com.iformall.domain.po.base.TenantEntity; | import com.iformall.domain.po.base.TenantEntity; | ||||
| import com.iformall.file.aliyun.AliyunOSS; | import com.iformall.file.aliyun.AliyunOSS; | ||||
| import io.swagger.annotations.Api; | import io.swagger.annotations.Api; | ||||
| import io.swagger.annotations.ApiOperation; | import io.swagger.annotations.ApiOperation; | ||||
| import org.apache.commons.lang3.StringUtils; | |||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
| @@ -28,10 +15,7 @@ import org.springframework.web.bind.annotation.RequestParam; | |||||
| import org.springframework.web.bind.annotation.RestController; | import org.springframework.web.bind.annotation.RestController; | ||||
| import org.springframework.web.multipart.MultipartFile; | import org.springframework.web.multipart.MultipartFile; | ||||
| import java.io.BufferedInputStream; | |||||
| import java.io.FileOutputStream; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.net.URL; | |||||
| import java.util.*; | import java.util.*; | ||||
| @RestController | @RestController | ||||
| @@ -41,80 +25,9 @@ public class UploadController extends BaseController { | |||||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||
| @Autowired | |||||
| private AwsProperty awsProperty; | |||||
| @Autowired | @Autowired | ||||
| private AliyunOSS aliyunOSS; | private AliyunOSS aliyunOSS; | ||||
| private AmazonS3 s3 = null; | |||||
| private ResultData awsUpload(MultipartFile multiReq, ObjectMetadata metadata, String fileName) { | |||||
| ResultData data = new ResultData(); | |||||
| try { | |||||
| if (s3 == null) { | |||||
| s3 = AmazonS3ClientBuilder.standard() | |||||
| .withRegion(awsProperty.getClientRegion()) | |||||
| .withCredentials(new AWSCredentialsProvider() { | |||||
| @Override | |||||
| public AWSCredentials getCredentials() { | |||||
| return new BasicAWSCredentials(awsProperty.getAccess(), awsProperty.getSecret()); | |||||
| } | |||||
| @Override | |||||
| public void refresh() { | |||||
| } | |||||
| }) | |||||
| .build(); | |||||
| } | |||||
| s3.putObject( | |||||
| new PutObjectRequest(awsProperty.getBucketName(), fileName, multiReq.getInputStream(), metadata) | |||||
| .withCannedAcl(CannedAccessControlList.PublicRead)); | |||||
| URL url = s3.getUrl(awsProperty.getBucketName(), fileName); | |||||
| logger.info(url.toString()); | |||||
| data.code = ResultData.SUCCESS; | |||||
| Map<String, String> map = new HashMap<>(); | |||||
| map.put("url", url.toString()); | |||||
| data.data = map; | |||||
| } catch (AmazonServiceException ase) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an AmazonServiceException, which " + | |||||
| "means your request made it " + | |||||
| "to Amazon S3, but was rejected with an error response" + | |||||
| " for some reason."); | |||||
| logger.warn(ase.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } catch (AmazonClientException ace) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an AmazonClientException, which " + | |||||
| "means the client encountered " + | |||||
| "an internal error while trying to " + | |||||
| "communicate with S3, " + | |||||
| "such as not being able to access the network."); | |||||
| logger.warn("Error Message: " + ace.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } catch (IOException ioe) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an IOException: " + ioe.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } | |||||
| return data; | |||||
| } | |||||
| private String getFileName(TenantEntity tenantEntity, String fileName) { | |||||
| if (StringUtils.isBlank(tenantEntity.getParentTenantId())) { | |||||
| fileName = tenantEntity.getTenantId() + "/" + fileName; | |||||
| } else { | |||||
| fileName = tenantEntity.getTenantId() + "/" + tenantEntity.getParentTenantId() + "/" + fileName; | |||||
| } | |||||
| return fileName; | |||||
| } | |||||
| /** | /** | ||||
| * 上传文件 | * 上传文件 | ||||
| @@ -130,29 +43,16 @@ public class UploadController extends BaseController { | |||||
| TenantEntity tenantEntity = getTenantInfo(); | TenantEntity tenantEntity = getTenantInfo(); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(multiReq.getSize()); | |||||
| FileOutputStream fos = null; | |||||
| BufferedInputStream fs = null; | |||||
| String fileName = UUID.randomUUID().toString(); | |||||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | ||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | ||||
| fileName = getFileName(tenantEntity, fileName + fileFormat); | |||||
| } else { | |||||
| fileName = getFileName(tenantEntity, fileName); | |||||
| } | } | ||||
| // ResultData data = awsUpload(multiReq, metadata, fileName); | |||||
| try { | try { | ||||
| ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ||||
| return data; | return data; | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| //e.printStackTrace(); | |||||
| logger.error(e.getMessage()); | logger.error(e.getMessage()); | ||||
| return new ResultData(ErrorCode.PICTURE_ANALYZING_ERROR); | return new ResultData(ErrorCode.PICTURE_ANALYZING_ERROR); | ||||
| } | } | ||||
| @@ -179,26 +79,14 @@ public class UploadController extends BaseController { | |||||
| for (MultipartFile multipartFile : files) { | for (MultipartFile multipartFile : files) { | ||||
| Map<String, String> map = new HashMap<>(); | Map<String, String> map = new HashMap<>(); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multipartFile.getContentType()); | |||||
| metadata.setContentLength(multipartFile.getSize()); | |||||
| FileOutputStream fos = null; | |||||
| BufferedInputStream fs = null; | |||||
| map.put("key", multipartFile.getOriginalFilename()); | map.put("key", multipartFile.getOriginalFilename()); | ||||
| String fileName = UUID.randomUUID().toString(); | |||||
| int dot = multipartFile.getOriginalFilename().lastIndexOf('.'); | int dot = multipartFile.getOriginalFilename().lastIndexOf('.'); | ||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multipartFile.getOriginalFilename().substring(dot, multipartFile.getOriginalFilename().length()); | fileFormat = multipartFile.getOriginalFilename().substring(dot, multipartFile.getOriginalFilename().length()); | ||||
| fileName = getFileName(tenantEntity, fileName + fileFormat); | |||||
| } else { | |||||
| fileName = getFileName(tenantEntity, fileName); | |||||
| } | } | ||||
| // ResultData data1 = awsUpload(multipartFile, metadata, fileName); | |||||
| try { | try { | ||||
| ResultData data1 = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multipartFile.getInputStream()); | ResultData data1 = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multipartFile.getInputStream()); | ||||
| if (data1.code == ResultData.SUCCESS) { | if (data1.code == ResultData.SUCCESS) { | ||||
| @@ -212,7 +100,6 @@ public class UploadController extends BaseController { | |||||
| return data; | return data; | ||||
| } | } | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| //e.printStackTrace(); | |||||
| logger.error(e.getMessage()); | logger.error(e.getMessage()); | ||||
| return new ResultData(ErrorCode.PICTURE_ANALYZING_ERROR); | return new ResultData(ErrorCode.PICTURE_ANALYZING_ERROR); | ||||
| } | } | ||||
| @@ -1,6 +1,5 @@ | |||||
| package com.iformall.service; | package com.iformall.service; | ||||
| import com.amazonaws.services.s3.model.ObjectMetadata; | |||||
| import com.iformall.common.ResultData; | import com.iformall.common.ResultData; | ||||
| import com.iformall.domain.po.base.TenantEntity; | import com.iformall.domain.po.base.TenantEntity; | ||||
| import com.iformall.exception.MallinkException; | import com.iformall.exception.MallinkException; | ||||
| @@ -8,7 +7,6 @@ import org.springframework.stereotype.Service; | |||||
| import org.springframework.web.bind.annotation.RequestBody; | import org.springframework.web.bind.annotation.RequestBody; | ||||
| import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | import javax.servlet.http.HttpServletResponse; | ||||
| import java.io.InputStream; | |||||
| import java.util.Map; | import java.util.Map; | ||||
| @Service | @Service | ||||
| @@ -23,5 +21,5 @@ public interface QrCodeService { | |||||
| ResultData uploadQrcode(TenantEntity tenantEntity, int type, String pageUrl, String sceneParam, | ResultData uploadQrcode(TenantEntity tenantEntity, int type, String pageUrl, String sceneParam, | ||||
| int withText, String text1, String text2, | int withText, String text1, String text2, | ||||
| String name); | String name); | ||||
| ResultData awsUpload(InputStream inputStream, ObjectMetadata metadata, String fileName, TenantEntity tenantEntity); | |||||
| } | } | ||||
| @@ -2,26 +2,13 @@ package com.iformall.service.impl; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaService; | import cn.binarywang.wx.miniapp.api.WxMaService; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor; | import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor; | ||||
| import com.amazonaws.AmazonClientException; | |||||
| import com.amazonaws.AmazonServiceException; | |||||
| import com.amazonaws.auth.AWSCredentials; | |||||
| import com.amazonaws.auth.AWSCredentialsProvider; | |||||
| import com.amazonaws.auth.BasicAWSCredentials; | |||||
| import com.amazonaws.services.s3.AmazonS3; | |||||
| import com.amazonaws.services.s3.AmazonS3ClientBuilder; | |||||
| import com.amazonaws.services.s3.model.CannedAccessControlList; | |||||
| import com.amazonaws.services.s3.model.ObjectMetadata; | |||||
| import com.amazonaws.services.s3.model.PutObjectRequest; | |||||
| import com.iformall.common.ErrorCode; | import com.iformall.common.ErrorCode; | ||||
| import com.iformall.common.Result; | import com.iformall.common.Result; | ||||
| import com.iformall.common.ResultData; | import com.iformall.common.ResultData; | ||||
| import com.iformall.config.AwsProperty; | |||||
| import com.iformall.domain.po.MallResource; | |||||
| import com.iformall.domain.po.base.TenantEntity; | import com.iformall.domain.po.base.TenantEntity; | ||||
| import com.iformall.domain.po.WxAppinfo; | import com.iformall.domain.po.WxAppinfo; | ||||
| import com.iformall.exception.MallinkException; | import com.iformall.exception.MallinkException; | ||||
| import com.iformall.file.aliyun.AliyunOSS; | import com.iformall.file.aliyun.AliyunOSS; | ||||
| import com.iformall.service.MallResourceService; | |||||
| import com.iformall.service.QrCodeService; | import com.iformall.service.QrCodeService; | ||||
| import com.iformall.service.WxAppinfoService; | import com.iformall.service.WxAppinfoService; | ||||
| import com.iformall.service.wechat.FmOpenService; | import com.iformall.service.wechat.FmOpenService; | ||||
| @@ -36,7 +23,6 @@ import org.springframework.stereotype.Service; | |||||
| import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | import javax.servlet.http.HttpServletResponse; | ||||
| import java.io.*; | import java.io.*; | ||||
| import java.net.URL; | |||||
| import java.util.*; | import java.util.*; | ||||
| /** | /** | ||||
| @@ -52,82 +38,11 @@ public class QrCodeServiceImpl implements QrCodeService { | |||||
| @Autowired | @Autowired | ||||
| private boolean isFmOpen; | private boolean isFmOpen; | ||||
| @Autowired | @Autowired | ||||
| private AwsProperty awsProperty; | |||||
| @Autowired | |||||
| private MallResourceService mallResourceService; | |||||
| @Autowired | |||||
| private String fmUploadDir; | private String fmUploadDir; | ||||
| private AmazonS3 s3 = null; | |||||
| @Autowired | @Autowired | ||||
| private AliyunOSS aliyunOSS; | private AliyunOSS aliyunOSS; | ||||
| @Override | |||||
| public ResultData awsUpload(InputStream inputStream, ObjectMetadata metadata, String fileName, TenantEntity tenantEntity) { | |||||
| ResultData data = new ResultData(); | |||||
| try { | |||||
| if(s3 == null) { | |||||
| s3 = AmazonS3ClientBuilder.standard() | |||||
| .withRegion(awsProperty.getClientRegion()) | |||||
| .withCredentials(new AWSCredentialsProvider() { | |||||
| @Override | |||||
| public AWSCredentials getCredentials() { | |||||
| return new BasicAWSCredentials(awsProperty.getAccess(), awsProperty.getSecret()); | |||||
| } | |||||
| @Override | |||||
| public void refresh() { | |||||
| } | |||||
| }) | |||||
| .build(); | |||||
| } | |||||
| s3.putObject( | |||||
| new PutObjectRequest(awsProperty.getBucketName(), fileName, inputStream, metadata) | |||||
| .withCannedAcl(CannedAccessControlList.PublicRead)); | |||||
| URL url = s3.getUrl(awsProperty.getBucketName(), fileName); | |||||
| logger.info(url.toString()); | |||||
| MallResource mr = new MallResource(); | |||||
| mr.updateTenantInfo(tenantEntity); | |||||
| mr.setBucket(awsProperty.getBucketName()); | |||||
| mr.setS3Key(fileName); | |||||
| mr.setUrl(url.toString()); | |||||
| mallResourceService.saveOrUpdate(mr); | |||||
| data.code = ResultData.SUCCESS; | |||||
| Map<String, String> map = new HashMap<>(); | |||||
| map.put("url", url.toString()); | |||||
| data.data = map; | |||||
| } catch (AmazonServiceException ase) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an AmazonServiceException, which " + | |||||
| "means your request made it " + | |||||
| "to Amazon S3, but was rejected with an error response" + | |||||
| " for some reason."); | |||||
| logger.warn(ase.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } catch (AmazonClientException ace) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an AmazonClientException, which " + | |||||
| "means the client encountered " + | |||||
| "an internal error while trying to " + | |||||
| "communicate with S3, " + | |||||
| "such as not being able to access the network."); | |||||
| logger.warn("Error Message: " + ace.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } catch (Exception ioe) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an IOException: " + ioe.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } | |||||
| return data; | |||||
| } | |||||
| @Override | @Override | ||||
| public void downQrCode(TenantEntity tenantEntity, HttpServletRequest request, HttpServletResponse response, Map<String, Object> params) throws MallinkException { | public void downQrCode(TenantEntity tenantEntity, HttpServletRequest request, HttpServletResponse response, Map<String, Object> params) throws MallinkException { | ||||
| @@ -281,24 +196,12 @@ public class QrCodeServiceImpl implements QrCodeService { | |||||
| try { | try { | ||||
| //上传 | //上传 | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType("image/jpeg"); | |||||
| metadata.setContentLength(dest.length()); | |||||
| FileOutputStream fos = null; | |||||
| BufferedInputStream fs = null; | |||||
| String fn = UUID.randomUUID().toString(); | |||||
| int dot = fileName.lastIndexOf('.'); | int dot = fileName.lastIndexOf('.'); | ||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = dest.getName().substring(dot, dest.getName().length()); | fileFormat = dest.getName().substring(dot, dest.getName().length()); | ||||
| fn = getFileName(tenantEntity, fn + fileFormat); | |||||
| } else { | |||||
| fn = getFileName(tenantEntity, fn); | |||||
| } | } | ||||
| // ResultData data = awsUpload(new FileInputStream(dest), metadata, fn, tenantEntity); | |||||
| ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, new FileInputStream(dest)); | ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, new FileInputStream(dest)); | ||||
| org.apache.commons.io.FileUtils.forceDelete(dest); | org.apache.commons.io.FileUtils.forceDelete(dest); | ||||
| return data; | return data; | ||||
| @@ -308,15 +211,6 @@ public class QrCodeServiceImpl implements QrCodeService { | |||||
| return new ResultData(); | return new ResultData(); | ||||
| } | } | ||||
| private String getFileName(TenantEntity tenantEntity, String fn) { | |||||
| if (StringUtils.isBlank(tenantEntity.getParentTenantId())) { | |||||
| fn = tenantEntity.getTenantId() + "/" + fn; | |||||
| } else { | |||||
| fn = tenantEntity.getTenantId() + "/" + tenantEntity.getParentTenantId() + "/" + fn; | |||||
| } | |||||
| return fn; | |||||
| } | |||||
| private void copyFileToDest(int withText, String text1, String text2, String destPath1, File dest, File codeFile) throws IOException { | private void copyFileToDest(int withText, String text1, String text2, String destPath1, File dest, File codeFile) throws IOException { | ||||
| if (withText == 1) { | if (withText == 1) { | ||||
| File dest1 = new File(destPath1); | File dest1 = new File(destPath1); | ||||
| @@ -1,21 +1,10 @@ | |||||
| package com.iformall.controller.sys; | package com.iformall.controller.sys; | ||||
| import com.amazonaws.AmazonClientException; | |||||
| import com.amazonaws.AmazonServiceException; | |||||
| import com.amazonaws.auth.AWSCredentials; | |||||
| import com.amazonaws.auth.AWSCredentialsProvider; | |||||
| import com.amazonaws.auth.BasicAWSCredentials; | |||||
| import com.amazonaws.services.s3.AmazonS3; | |||||
| import com.amazonaws.services.s3.AmazonS3ClientBuilder; | |||||
| import com.amazonaws.services.s3.model.*; | |||||
| import com.iformall.common.ResultData; | import com.iformall.common.ResultData; | ||||
| import com.iformall.config.AwsProperty; | |||||
| import com.iformall.controller.base.BaseController; | import com.iformall.controller.base.BaseController; | ||||
| import com.iformall.domain.po.MallResource; | |||||
| import com.iformall.domain.po.base.TenantEntity; | import com.iformall.domain.po.base.TenantEntity; | ||||
| import com.iformall.file.aliyun.AliyunOSS; | import com.iformall.file.aliyun.AliyunOSS; | ||||
| import com.iformall.service.MallResourceService; | |||||
| import com.iformall.utils.ImgUtil; | import com.iformall.utils.ImgUtil; | ||||
| import io.swagger.annotations.Api; | import io.swagger.annotations.Api; | ||||
| import io.swagger.annotations.ApiOperation; | import io.swagger.annotations.ApiOperation; | ||||
| @@ -27,7 +16,6 @@ import org.springframework.web.bind.annotation.*; | |||||
| import org.springframework.web.multipart.MultipartFile; | import org.springframework.web.multipart.MultipartFile; | ||||
| import java.io.*; | import java.io.*; | ||||
| import java.net.URL; | |||||
| import java.util.*; | import java.util.*; | ||||
| import java.util.List; | import java.util.List; | ||||
| @@ -38,93 +26,12 @@ public class UploadController extends BaseController { | |||||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||
| @Autowired | |||||
| private AwsProperty awsProperty; | |||||
| @Autowired | @Autowired | ||||
| private String fmUploadDir; | private String fmUploadDir; | ||||
| @Autowired | @Autowired | ||||
| private AliyunOSS aliyunOSS; | private AliyunOSS aliyunOSS; | ||||
| @Autowired | |||||
| private MallResourceService mallResourceService; | |||||
| private String getFileName(TenantEntity tenantEntity, String fileName) { | |||||
| if (StringUtils.isBlank(tenantEntity.getParentTenantId())) { | |||||
| fileName = tenantEntity.getTenantId() + "/" + fileName; | |||||
| } else { | |||||
| fileName = tenantEntity.getTenantId() + "/" + tenantEntity.getParentTenantId() + "/" +fileName; | |||||
| } | |||||
| return fileName; | |||||
| } | |||||
| private AmazonS3 s3 = null; | |||||
| private ResultData awsUpload(InputStream inputStream, ObjectMetadata metadata, String fileName, TenantEntity tenantVEntity) { | |||||
| ResultData data = new ResultData(); | |||||
| try { | |||||
| if(s3 == null) { | |||||
| s3 = AmazonS3ClientBuilder.standard() | |||||
| .withRegion(awsProperty.getClientRegion()) | |||||
| .withCredentials(new AWSCredentialsProvider() { | |||||
| @Override | |||||
| public AWSCredentials getCredentials() { | |||||
| return new BasicAWSCredentials(awsProperty.getAccess(), awsProperty.getSecret()); | |||||
| } | |||||
| @Override | |||||
| public void refresh() { | |||||
| } | |||||
| }) | |||||
| .build(); | |||||
| } | |||||
| s3.putObject( | |||||
| new PutObjectRequest(awsProperty.getBucketName(), fileName, inputStream, metadata) | |||||
| .withCannedAcl(CannedAccessControlList.PublicRead)); | |||||
| URL url = s3.getUrl(awsProperty.getBucketName(), fileName); | |||||
| logger.info(url.toString()); | |||||
| MallResource mr = new MallResource(); | |||||
| mr.updateTenantInfo(tenantVEntity); | |||||
| mr.setBucket(awsProperty.getBucketName()); | |||||
| mr.setS3Key(fileName); | |||||
| mr.setUrl(url.toString()); | |||||
| mallResourceService.saveOrUpdate(mr); | |||||
| data.code = ResultData.SUCCESS; | |||||
| Map<String, String> map = new HashMap<>(); | |||||
| map.put("url", url.toString()); | |||||
| data.data = map; | |||||
| } catch (AmazonServiceException ase) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an AmazonServiceException, which " + | |||||
| "means your request made it " + | |||||
| "to Amazon S3, but was rejected with an error response" + | |||||
| " for some reason."); | |||||
| logger.warn(ase.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } catch (AmazonClientException ace) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an AmazonClientException, which " + | |||||
| "means the client encountered " + | |||||
| "an internal error while trying to " + | |||||
| "communicate with S3, " + | |||||
| "such as not being able to access the network."); | |||||
| logger.warn("Error Message: " + ace.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } catch (Exception ioe) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an IOException: " + ioe.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } | |||||
| return data; | |||||
| } | |||||
| /** | /** | ||||
| * 上传文件 | * 上传文件 | ||||
| @@ -140,24 +47,12 @@ public class UploadController extends BaseController { | |||||
| TenantEntity tenantEntity = getTenantInfo(); | TenantEntity tenantEntity = getTenantInfo(); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(multiReq.getSize()); | |||||
| FileOutputStream fos = null; | |||||
| BufferedInputStream fs = null; | |||||
| String fileName = UUID.randomUUID().toString(); | |||||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | ||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | ||||
| fileName = getFileName(tenantEntity, fileName + fileFormat); | |||||
| } else { | |||||
| fileName = getFileName(tenantEntity, fileName); | |||||
| } | } | ||||
| // ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, tenantEntity); | |||||
| ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ||||
| return data; | return data; | ||||
| } | } | ||||
| @@ -176,20 +71,13 @@ public class UploadController extends BaseController { | |||||
| TenantEntity tenantEntity = getTenantInfo(); | TenantEntity tenantEntity = getTenantInfo(); | ||||
| String fileName = UUID.randomUUID().toString(); | |||||
| String tempFileName = fileName; | |||||
| String tempFileName = UUID.randomUUID().toString(); | |||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | ||||
| fileName = getFileName(tenantEntity, fileName + fileFormat); | |||||
| } else { | |||||
| fileName = getFileName(tenantEntity, fileName); | |||||
| } | } | ||||
| System.out.println(fileName); | |||||
| FileOutputStream fos = null; | FileOutputStream fos = null; | ||||
| String imgFormat = ImgUtil.getImgFormat(fileFormat); | String imgFormat = ImgUtil.getImgFormat(fileFormat); | ||||
| if(StringUtils.isNotBlank(imgFormat)) { | if(StringUtils.isNotBlank(imgFormat)) { | ||||
| @@ -210,19 +98,12 @@ public class UploadController extends BaseController { | |||||
| File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName, imgFormat); | File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName, imgFormat); | ||||
| // 删除本地缓存 | // 删除本地缓存 | ||||
| localFile.delete(); | localFile.delete(); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(newFile.length()); | |||||
| // ResultData data = awsUpload(new FileInputStream(newFile), metadata, fileName, tenantEntity); | |||||
| ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, new FileInputStream(newFile)); | ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, new FileInputStream(newFile)); | ||||
| // 删除本地缓存 | // 删除本地缓存 | ||||
| newFile.delete(); | newFile.delete(); | ||||
| return data; | return data; | ||||
| } else { | } else { | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(multiReq.getSize()); | |||||
| // ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, tenantEntity); | |||||
| ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ResultData data = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multiReq.getInputStream()); | ||||
| return data; | return data; | ||||
| } | } | ||||
| @@ -248,26 +129,14 @@ public class UploadController extends BaseController { | |||||
| for(MultipartFile multipartFile: files) { | for(MultipartFile multipartFile: files) { | ||||
| Map<String, String> map = new HashMap<>(); | Map<String, String> map = new HashMap<>(); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multipartFile.getContentType()); | |||||
| metadata.setContentLength(multipartFile.getSize()); | |||||
| FileOutputStream fos = null; | |||||
| BufferedInputStream fs = null; | |||||
| map.put("key", multipartFile.getOriginalFilename()); | map.put("key", multipartFile.getOriginalFilename()); | ||||
| String fileName = UUID.randomUUID().toString(); | |||||
| int dot = multipartFile.getOriginalFilename().lastIndexOf('.'); | int dot = multipartFile.getOriginalFilename().lastIndexOf('.'); | ||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multipartFile.getOriginalFilename().substring(dot, multipartFile.getOriginalFilename().length()); | fileFormat = multipartFile.getOriginalFilename().substring(dot, multipartFile.getOriginalFilename().length()); | ||||
| fileName = getFileName(tenantEntity, fileName + fileFormat); | |||||
| } else { | |||||
| fileName = getFileName(tenantEntity, fileName); | |||||
| } | } | ||||
| // ResultData data1 = awsUpload(multipartFile.getInputStream(), metadata, fileName, tenantEntity); | |||||
| ResultData data1 = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multipartFile.getInputStream()); | ResultData data1 = aliyunOSS.uploadFile(tenantEntity.getTenantId(), fileFormat, multipartFile.getInputStream()); | ||||
| if(data1.code == ResultData.SUCCESS) { | if(data1.code == ResultData.SUCCESS) { | ||||
| Map _data = (Map)data1.data; | Map _data = (Map)data1.data; | ||||
| @@ -300,46 +169,15 @@ public class UploadController extends BaseController { | |||||
| public ResultData cimgUpload(@RequestParam("file") MultipartFile multiReq) throws Exception { | public ResultData cimgUpload(@RequestParam("file") MultipartFile multiReq) throws Exception { | ||||
| logger.info("[" + getIpAddr() + "] UploadController::cimgUpload"); | logger.info("[" + getIpAddr() + "] UploadController::cimgUpload"); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(multiReq.getSize()); | |||||
| String fileName = multiReq.getOriginalFilename(); | |||||
| fileName = "cimg/" + fileName; | |||||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | ||||
| String fileFormat = ""; | String fileFormat = ""; | ||||
| if (dot >= 0) { | if (dot >= 0) { | ||||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | ||||
| } | } | ||||
| // ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, new TenantEntity() {{ | |||||
| // setTenantId("cimg"); | |||||
| // }}); | |||||
| ResultData data = aliyunOSS.uploadFile("cimg", fileFormat, multiReq.getInputStream()); | ResultData data = aliyunOSS.uploadFile("cimg", fileFormat, multiReq.getInputStream()); | ||||
| return data; | return data; | ||||
| } | } | ||||
| // /** | |||||
| // * AWS | |||||
| // * @return | |||||
| // * @throws Exception | |||||
| // */ | |||||
| // @GetMapping(value = "/lists") | |||||
| // @ApiOperation("aws列举文件") | |||||
| // public ResultData awsList() throws Exception { | |||||
| // logger.info("[" + getIpAddr() + "] UploadController::cimgUpload"); | |||||
| // | |||||
| // try{ | |||||
| // ObjectListing ol = s3.listObjects(awsProperty.getBucketName()); | |||||
| // List<S3ObjectSummary> objects = ol.getObjectSummaries(); | |||||
| // for (S3ObjectSummary os: objects) { | |||||
| // logger.info(os.toString()); | |||||
| // break; | |||||
| // } | |||||
| // }catch (Exception e){ | |||||
| // e.printStackTrace(); | |||||
| // } | |||||
| // | |||||
| // return new ResultData(); | |||||
| // } | |||||
| } | } | ||||