he2802 il y a 2 ans
Parent
commit
2a4ad0c32e

+ 1 - 1
zhongzheng-admin/src/main/java/com/zhongzheng/controller/tool/TestController.java

@@ -60,7 +60,7 @@ public class TestController extends BaseController
     @PostMapping("/zip")
     public AjaxResult zip(UserEntity user)
     {
-    //    ossService.zipPeopleDownload(18L,523L,"李盛仁","445224199901040003");
+        ossService.processObject("oss/images/avatar/318/1686645474183_748829657","oss/images/avatar/318/1686645474183_748829657","image/rotate,90");
         return AjaxResult.success(users.put(user.getUserId(), user));
     }
 

+ 1 - 1
zhongzheng-common/pom.xml

@@ -96,7 +96,7 @@
         <dependency>
             <groupId>com.aliyun.oss</groupId>
             <artifactId>aliyun-sdk-oss</artifactId>
-            <version>3.10.2</version>
+            <version>3.15.2</version>
         </dependency>
 
         <dependency>

+ 2 - 0
zhongzheng-system/src/main/java/com/zhongzheng/modules/alioss/service/OssService.java

@@ -38,4 +38,6 @@ public interface OssService {
     void zipWisdomDownload(List<FileBean> list, ZipOutputStream outStream);
 
     InputStream getStreamByObject(String filePath) throws Exception;
+
+    void processObject(String targetImage ,String sourceImage,String styleType) ;
 }

+ 36 - 1
zhongzheng-system/src/main/java/com/zhongzheng/modules/alioss/service/impl/OssServiceImpl.java

@@ -6,9 +6,12 @@ import cn.hutool.json.JSONUtil;
 
 import com.aliyun.oss.OSS;
 import com.aliyun.oss.OSSClient;
+import com.aliyun.oss.OSSException;
 import com.aliyun.oss.common.utils.BinaryUtil;
+import com.aliyun.oss.common.utils.IOUtils;
 import com.aliyun.oss.internal.OSSUtils;
 import com.aliyun.oss.model.*;
+import com.aliyuncs.exceptions.ClientException;
 import com.zhongzheng.common.constant.Constants;
 import com.zhongzheng.common.core.domain.AjaxResult;
 import com.zhongzheng.common.exception.CustomException;
@@ -20,7 +23,6 @@ import com.zhongzheng.modules.alioss.vo.ResultBean;
 import com.zhongzheng.modules.grade.vo.ClassPeriodStudentExportVo;
 import com.zhongzheng.modules.grade.vo.WeekDataVo;
 import com.zhongzheng.modules.user.service.IUserStudyRecordPhotoService;
-import org.apache.commons.io.IOUtils;
 import org.apache.xmlbeans.impl.values.XmlValueNotNillableException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -343,6 +345,39 @@ public class OssServiceImpl implements OssService {
             return ossObject.getObjectContent();
     }
 
+    @Override
+    public void processObject(String targetImage,String sourceImage, String styleType) {
+        try {
+            // 将图片缩放为固定宽高100 px。
+            StringBuilder sbStyle = new StringBuilder();
+            Formatter styleFormatter = new Formatter(sbStyle);
+            // 将处理后的图片命名为example-resize.png并保存到当前Bucket。
+            // 填写Object完整路径。Object完整路径中不能包含Bucket名称。
+            styleFormatter.format("%s|sys/saveas,o_%s,b_%s", styleType,
+                    BinaryUtil.toBase64String(targetImage.getBytes()),
+                    BinaryUtil.toBase64String(ALIYUN_OSS_BUCKET_NAME.getBytes()));
+            System.out.println(sbStyle.toString());
+            ProcessObjectRequest request = new ProcessObjectRequest(ALIYUN_OSS_BUCKET_NAME, sourceImage, sbStyle.toString());
+            GenericResult processResult = ossClient.processObject(request);
+            String json = IOUtils.readStreamAsString(processResult.getResponse().getContent(), "UTF-8");
+            processResult.getResponse().getContent().close();
+            System.out.println(json);
+        } catch (OSSException oe) {
+            System.out.println("Caught an OSSException, which means your request made it to OSS, "
+                    + "but was rejected with an error response for some reason.");
+            System.out.println("Error Message:" + oe.getErrorMessage());
+            System.out.println("Error Code:" + oe.getErrorCode());
+            System.out.println("Request ID:" + oe.getRequestId());
+            System.out.println("Host ID:" + oe.getHostId());
+        }catch (IOException e) {
+
+        } finally {
+            if (ossClient != null) {
+                ossClient.shutdown();
+            }
+        }
+    }
+
     public void zipFile(List<String> fileList, ZipOutputStream zipOut,String dir) {
         if (CollectionUtils.isEmpty(fileList)) {
             return;