forked from kidgrow-microservices-platform

zhaoxiaohao
2020-08-02 637d130f18d82b758dd999f2a02cd1047de528cd
kidgrow-business/kidgrow-filecenter/kidgrow-filecenter-biz/src/main/java/com/kidgrow/filecenter/service/impl/AbstractIFileService.java
@@ -19,12 +19,16 @@
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
/**
@@ -40,6 +44,8 @@
    private static final String FILE_SPLIT = ".";
    private static Logger logger = LoggerFactory.getLogger(AbstractIFileService.class);
    @Value("${kidgrow.file-server.oss.domain}")
    private  String DOMAIN;
    @Override
    public FileInfo upload(MultipartFile file) throws Exception {
        FileInfo fileInfo = FileUtil.getFileInfo(file);
@@ -221,40 +227,94 @@
    @Override
    public ResultBody baseDownLoad(Map<String, Object> map) throws Exception {
        List<FileInfo> fileInfos = baseMapper.selectByMap(map);
        if(fileInfos.size()>0){
            FileInfo fileInfo = fileInfos.get(0);
            OSSObject down1 = this.down(fileInfo.getUrl().replace("https://kidgrow.oss-accelerate.aliyuncs.com/",""));
            InputStream objectContent = down1.getObjectContent();
            //返回Base64编码过的字节数组字符串
            byte[] data = null;
            // 读取图片字节数组
        String urlDiZhi=MapUtils.getString(map,"url");
        if(!urlDiZhi.contains(DOMAIN)){
            ByteArrayOutputStream outPut = new ByteArrayOutputStream();
            byte[] data = new byte[1024];
            try {
                ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
                byte[] buff = new byte[100];
                int rc = 0;
                while ((rc = objectContent.read(buff, 0, 100)) > 0) {
                    swapStream.write(buff, 0, rc);
                // 创建URL
                URL url = new URL("http://192.168.2.25:8008/123/J-1.jpg");
                // 创建链接
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(10 * 1000);
                InputStream inStream = conn.getInputStream();
                int len = -1;
                while ((len = inStream.read(data)) != -1) {
                    outPut.write(data, 0, len);
                }
                data = swapStream.toByteArray();
                inStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (objectContent != null) {
                    try {
                        objectContent.close();
                        down1.close();
                    } catch (IOException e) {
                        throw new Exception("输入流关闭异常");
            }
            String substring = urlDiZhi.substring(urlDiZhi.lastIndexOf(".") + 1, urlDiZhi.length());
            String str="data:image/" + substring + ";base64,";
            // 对字节数组Base64编码
            BASE64Encoder encoder = new BASE64Encoder();
            System.out.println(encoder.encode(outPut.toByteArray()));
            return ResultBody.ok().data(str+encoder.encode(outPut.toByteArray()).replaceAll("\n", "").replaceAll("\r", ""));
        }else {
            List<FileInfo> fileInfos = baseMapper.selectByMap(map);
            if (fileInfos.size() > 0) {
                FileInfo fileInfo = fileInfos.get(0);
                OSSObject down = this.down(fileInfo.getUrl().replace(DOMAIN, ""));
                InputStream objectContent = down.getObjectContent();
                //返回Base64编码过的字节数组字符串
                byte[] data = null;
                // 读取图片字节数组
                try {
                    ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
                    byte[] buff = new byte[100];
                    int rc = 0;
                    while ((rc = objectContent.read(buff, 0, 100)) > 0) {
                        swapStream.write(buff, 0, rc);
                    }
                    data = swapStream.toByteArray();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (objectContent != null) {
                        try {
                            objectContent.close();
                            down.close();
                        } catch (IOException e) {
                            throw new Exception("输入流关闭异常");
                        }
                    }
                }
                String encode = "data:" + fileInfo.getContentType() + ";base64," + Base64.getEncoder().encodeToString(data);
                return ResultBody.ok().data(encode);
            } else {
                return ResultBody.failed("获取数据失败");
            }
            String encode="data:"+fileInfo.getContentType()+";base64,"+Base64.getEncoder().encodeToString(data);
            return ResultBody.ok().data(encode);
        }else {
            return ResultBody.failed("获取数据失败");
        }
    }
    public static void main(String[] args) {
        ByteArrayOutputStream outPut = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        try {
            // 创建URL
            URL url = new URL("http://192.168.2.25:8008/123/J-1.jpg");
            // 创建链接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(10 * 1000);
            InputStream inStream = conn.getInputStream();
            int len = -1;
            while ((len = inStream.read(data)) != -1) {
                outPut.write(data, 0, len);
            }
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        System.out.println(encoder.encode(outPut.toByteArray()));
    }
}