Commit 6f3033b2 authored by shiyu's avatar shiyu

媒体工具类

parent b27f5e7c
...@@ -9,6 +9,10 @@ import java.nio.file.Path; ...@@ -9,6 +9,10 @@ import java.nio.file.Path;
import java.util.Base64; import java.util.Base64;
import java.util.stream.Stream; import java.util.stream.Stream;
import com.wwdz.ch.core.consts.CommConsts;
import com.wwdz.ch.core.consts.MediaTypeEnum;
import com.wwdz.ch.core.util.MediaUtil;
import com.xxdxxs.utils.CommonUtils;
import com.xxdxxs.utils.JsonUtils; import com.xxdxxs.utils.JsonUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
...@@ -196,4 +200,25 @@ public class QiniuStorage implements Storage { ...@@ -196,4 +200,25 @@ public class QiniuStorage implements Storage {
public String generateUrl(String keyName) { public String generateUrl(String keyName) {
return endpoint + "/" + keyName; return endpoint + "/" + keyName;
} }
/**
* 原图片地址没尺寸信息的加上尺寸信息重新上传
* @param imageUrl
* @return keyName
*/
public String reStore(String imageUrl) {
try {
URL url = new URL(imageUrl);
InputStream inputStream = url.openStream();
BufferedImage bufferedImage = ImageIO.read(inputStream);
String keyName = CommConsts.UPLOAD_PRE_DIRECTORY + CommonUtils.getUUID() + "_" + bufferedImage.getWidth() + "x" + bufferedImage.getHeight() + "." + MediaTypeEnum.IMAGE_JPG.getExt();
InputStream imageStream = MediaUtil.bufferedImageToInputStream(bufferedImage, MediaTypeEnum.IMAGE_JPG.getExt());
store(imageStream, 1, MediaTypeEnum.IMAGE_JPG.getMime(), keyName);
return keyName;
} catch (Exception e) {
logger.error("获取图片尺寸重新上传图片出错 : {}", e);
}
return null;
}
} }
...@@ -77,7 +77,7 @@ public class MediaUtil { ...@@ -77,7 +77,7 @@ public class MediaUtil {
* @param type 图片格式(eg:jpg、png) * @param type 图片格式(eg:jpg、png)
* @return * @return
*/ */
private static InputStream bufferedImageToInputStream(BufferedImage image, String type) { public static InputStream bufferedImageToInputStream(BufferedImage image, String type) {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
ImageIO.write(image, type, os); ImageIO.write(image, type, os);
return new ByteArrayInputStream(os.toByteArray()); return new ByteArrayInputStream(os.toByteArray());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment