1. 程式人生 > >通過URL下載圖片進行壓縮並上傳到oss和本地

通過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());
    }
    
}