美文网首页程序员Spring-Boot
将阿里 oss 存储照片转换成 Base64编码

将阿里 oss 存储照片转换成 Base64编码

作者: 王传贺 | 来源:发表于2019-11-22 14:03 被阅读0次

一 : 背景

由于网络不通,无法通过url直接获取阿里oss存储照片,所以转换成Base64编码 供前端使用

二 : Java 代码

@ApiOperation("获取base64")
    @GetMapping(value = "/getBaseFile")
    public JSONData getBaseFile(@RequestParam String file) throws Exception{
        // 指定过期时间为一年。
        Date expiration = new Date(new Date().getTime() + 1000 * 60 * 658800 * 2);
        // 设置图片处理样式。
        String style = "image/resize,m_fixed,w_100,h_100/rotate,90";
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, file, HttpMethod.GET);
        req.setExpiration(expiration);
        req.setProcess(style);
        URL signedUrl = ossClient.generatePresignedUrl(req);
        //Map<String, String> customHeaders = new HashMap<String, String>();
        // 添加GetObject请求头。
        //customHeaders.put("Range", "bytes=100-1000");
       // OSSObject object = ossClient.getObject(signedUrl, customHeaders);
        //http://hztc-oss.oss-cn-hz-hty-d01-a.wtops-procloud.com/hztc-oss/hiuhiu.jpg
//        1,导包
//        2,得到HttpClient对象
        System.out.println(signedUrl.toString());
        HttpClient client = new DefaultHttpClient();
        //3,设置请求方式
        HttpGet get = new HttpGet(signedUrl.toString());
        //4,执行请求, 获取响应信息
        HttpResponse response = client.execute(get);
        Map<String,String> map = new HashMap<>();
        if(response.getStatusLine().getStatusCode() == 200)
        {
            //得到实体
            HttpEntity entity = response.getEntity();
            byte[] data = EntityUtils.toByteArray(entity);
            BASE64Encoder encoder = new BASE64Encoder();
            //返回Base64编码过的字节数组字符串
            String encode = encoder.encode(data);
            map.put("encode",encode);
            map.put("file",signedUrl.toString());
            JSONData jsonData = new JSONData();
            jsonData.setData(map);
            return jsonData;
        } else {
            return null;
        }
//        InputStream in = null;
//        byte[] data = null;
//        //读取图片字节数组
//        try{
//            //in = new FileInputStream(new File(signedUrl.toURI()));
//            in = new FileInputStream(new File(signedUrl.getFile()));
//            data = new byte[in.available()];
//            in.read(data);
//            in.close();
//        }catch (IOException e){
//            e.printStackTrace();
//        }
//        //对字节数组Base64编码
//        BASE64Encoder encoder = new BASE64Encoder();
//        //返回Base64编码过的字节数组字符串
//        String encode = encoder.encode(data);
//        return new JSONData(encode);
    }


    public static void main(String[] args) {
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try{
            in = new FileInputStream("http://****.oss-cn-hz-hty-d01-a.wtops-procloud.com/hztc-oss/hiuhiu.jpg");
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        //返回Base64编码过的字节数组字符串
        String encode = encoder.encode(data);
        System.out.println(encode);
    }

相关文章

网友评论

    本文标题:将阿里 oss 存储照片转换成 Base64编码

    本文链接:https://www.haomeiwen.com/subject/ojzfwctx.html