|
@@ -3,23 +3,17 @@ package com.qs.mp.quartz.task;
|
|
|
import com.qcloud.cos.COSClient;
|
|
|
import com.qcloud.cos.exception.CosClientException;
|
|
|
import com.qcloud.cos.exception.CosServiceException;
|
|
|
-import com.qcloud.cos.model.COSObject;
|
|
|
import com.qcloud.cos.model.COSObjectSummary;
|
|
|
import com.qcloud.cos.model.GetObjectRequest;
|
|
|
import com.qcloud.cos.model.ListObjectsRequest;
|
|
|
import com.qcloud.cos.model.ObjectListing;
|
|
|
import com.qcloud.cos.model.ObjectMetadata;
|
|
|
+import com.qs.mp.common.image.CompressUtil;
|
|
|
import com.qs.mp.common.qcloud.QcloudFileUtils;
|
|
|
import com.qs.mp.common.utils.LogUtil;
|
|
|
-import io.lettuce.core.output.ByteArrayOutput;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
import java.util.List;
|
|
|
import javax.imageio.ImageIO;
|
|
|
import net.coobird.thumbnailator.Thumbnails;
|
|
@@ -114,61 +108,65 @@ public class CosTask {
|
|
|
LogUtil.info(logger, "...图片压缩替换完成!!...");
|
|
|
}
|
|
|
|
|
|
- private void uploadAndCommpressImg(String key) {
|
|
|
+ private void uploadAndCommpressImg(String key) throws IOException, InterruptedException {
|
|
|
String path = filePath + "/thumb/" + key;
|
|
|
File downFile = new File(path);
|
|
|
GetObjectRequest getObjectRequest = new GetObjectRequest(publicBucketName, key);
|
|
|
COSClient cosClient = QcloudFileUtils.getCosClient();
|
|
|
- ObjectMetadata cosObjectmetadata = cosClient.getObject(getObjectRequest, downFile);
|
|
|
- String mimeType = cosObjectmetadata.getContentType();
|
|
|
- ByteArrayInputStream inputStream = null;
|
|
|
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);
|
|
|
+// FileInputStream fileInputStream = new FileInputStream(path);
|
|
|
+ String contentType = downObjectMeta.getContentType();
|
|
|
+// QcloudFileUtils.putStream(fileInputStream, key, publicBucketName, mimeType);
|
|
|
+
|
|
|
+ // 压缩图片
|
|
|
+ BufferedImage bufferedImage = ImageIO.read(downFile);
|
|
|
+ Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(bufferedImage);
|
|
|
+ int pWidth = bufferedImage.getWidth();
|
|
|
+ int pHeight = bufferedImage.getHeight();
|
|
|
+ builder.size(pWidth, pHeight);
|
|
|
+ double accuracy = CompressUtil.getAccuracy(downObjectMeta.getContentLength()/1024);
|
|
|
+ LogUtil.info(logger, "原始文件长:{0},大小:{1},类型:{2},压缩比:{3}", pWidth, downObjectMeta.getContentLength()/1024, contentType, 0.8);
|
|
|
+
|
|
|
+ String outFileDir = filePath + "/thumb";
|
|
|
+ File tempFile = new File(outFileDir);
|
|
|
+ if (!tempFile.exists()) {
|
|
|
+ tempFile.mkdirs();
|
|
|
+ }
|
|
|
+ String thumbName = key + "_s";
|
|
|
+ String outFilePath =
|
|
|
+ filePath + "/thumb/" + thumbName + "." + contentType.substring(contentType.lastIndexOf("/") + 1);
|
|
|
+ builder.toFile(outFilePath);
|
|
|
+
|
|
|
+ // 压缩图片
|
|
|
// 压缩图片
|
|
|
- try {
|
|
|
- BufferedImage bufferedImage = ImageIO.read(downFile);
|
|
|
- Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(bufferedImage);
|
|
|
- int pWidth = bufferedImage.getWidth();
|
|
|
- int pHeight = bufferedImage.getHeight();
|
|
|
- String thumbName = key + "_s";
|
|
|
-
|
|
|
- // 压缩图片
|
|
|
- if (pWidth > 500) {
|
|
|
- float scale = 500f / pWidth;
|
|
|
- Thumbnails.of(downFile).scale(scale).outputQuality(0.8f).toOutputStream(outputStream);
|
|
|
+ if (pWidth > 750) {
|
|
|
+ float scale = 750f / pWidth;
|
|
|
+ if ("image/png".equals(contentType)) {
|
|
|
+ Thumbnails.of(outFilePath).scale(scale).outputQuality(0.8)
|
|
|
+ .imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png")
|
|
|
+ .toFile(outFilePath);
|
|
|
} else {
|
|
|
- Thumbnails.of(downFile).scale(1f).outputQuality(0.8f).toOutputStream(outputStream);
|
|
|
+ Thumbnails.of(outFilePath).scale(scale).outputQuality(0.8).toFile(outFilePath);
|
|
|
}
|
|
|
- inputStream = new ByteArrayInputStream(outputStream.toByteArray());
|
|
|
- QcloudFileUtils.putStream(inputStream, thumbName, publicBucketName, mimeType);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- LogUtil.error(logger, e, "图片压缩上传异常");
|
|
|
- } finally {
|
|
|
- if (inputStream != null) {
|
|
|
- try {
|
|
|
- inputStream.close();
|
|
|
- } catch (IOException e) {
|
|
|
- LogUtil.error(logger, e, "关闭输入流失败");
|
|
|
- }
|
|
|
- }
|
|
|
- if (outputStream != null) {
|
|
|
- try {
|
|
|
- outputStream.close();
|
|
|
- } catch (IOException e) {
|
|
|
- LogUtil.error(logger, e, "关闭输出流失败");
|
|
|
- }
|
|
|
+ }else {
|
|
|
+ if ("image/png".equals(contentType)) {
|
|
|
+ Thumbnails.of(outFilePath).scale(1f).outputQuality(0.8)
|
|
|
+ .imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png")
|
|
|
+ .toFile(outFilePath);
|
|
|
+ } else {
|
|
|
+ Thumbnails.of(outFilePath).scale(1f).outputQuality(0.8).toFile(outFilePath);
|
|
|
}
|
|
|
-// if (cosObject != null) {
|
|
|
-// try {
|
|
|
-// cosObject.close();
|
|
|
-// } catch (IOException e) {
|
|
|
-// LogUtil.error(logger, e, "关闭cos对象失败");
|
|
|
-// }
|
|
|
-// }
|
|
|
}
|
|
|
|
|
|
+ File outFile = new File(outFilePath);
|
|
|
+ QcloudFileUtils.putFile(outFile, thumbName, publicBucketName, contentType);
|
|
|
+ LogUtil.info(logger, "压缩后文件大小:{0}", outFile.length()/ 1024);
|
|
|
+ downFile.delete();
|
|
|
+ outFile.delete();
|
|
|
+
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
+
|