通过URL下载图片进行压缩并上传到oss和本地

package test.com.redis;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cloopen.rest.sdk.utils.encoder.BASE64Decoder;
import com.cloopen.rest.sdk.utils.encoder.BASE64Encoder;
import com.ovopark.shopweb.common.oss.OssHelper;

public class Atest {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(Atest.class);
    private static final int PROTECTED_LENGTH = 1024 * 1024;// 输入流保护 1M

    public static String downLoad(String strUrl, Boolean isProxy, String host, String port) {
        if (isProxy) {
            setProxy(host, port);
        }
        URL url = null;
        try {
            url = new URL(strUrl);
        } catch (MalformedURLException e2) {
            LOGGER.error("下载图片异常,新建URL失败");
            return "";
        }
        InputStream is = null;
        try {
            is = url.openStream();
            return readInfoStreamToBytes(is);
        } catch (IOException e1) {
            LOGGER.error("下载图片异常");
            return "";
        }
    }

    public static void setProxy(String host, String port) {
        System.setProperty("proxySet", "true");
        System.setProperty("proxyHost", host);
        System.setProperty("proxyPort", port);
    }

    public static void main(String[] args) {
        String str = "http://ovopark.oss-cn-hangzhou.aliyuncs.com/2018/12/18/1545109997006425.jpeg?x-oss-process=image/resize,w_100,h_100/quality,q_80";
        String picStr = Atest.downLoad(str, true, "", "");
        GenerateImage(picStr);
        System.out.print(picStr);

    }

    public static boolean GenerateImage(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            //上传oss返回图片url
            OssHelper o = new OssHelper();
            String aString = "http://ovopark.oss-cn-hangzhou.aliyuncs.com/"+o.uploadFile(b);
            
            // 生成本地jpeg图片
            String imgFilePath = "C:\\Users\\WDZ88\\Desktop.jpeg";// 新生成的图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static String readInfoStreamToBytes(InputStream is) {
        if (is == null) {
            LOGGER.error("输入流为null");
            return null;
        }

        // 字节数组
        byte[] bCache = new byte[2048];
        int readSize = 0;// 每次读取的字节长度
        int totalSize = 0;// 总字节长度
        ByteArrayOutputStream infoStream = new ByteArrayOutputStream();
        try {
            // 一次性读取2048字节
            while ((readSize = is.read(bCache)) > 0) {
                totalSize += readSize;
                if (totalSize > PROTECTED_LENGTH) {
                    LOGGER.error("输入流超出1M大小限制");
                    return null;
                }
                // 将bcache中读取的input数据写入infoStream
                infoStream.write(bCache, 0, readSize);
            }
        } catch (IOException e1) {
            LOGGER.error("输入流读取异常");
            return null;
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                LOGGER.error("输入流关闭异常");
            }
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(infoStream.toByteArray());
    }
    
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值