图片上传至OSS服务器的分析

1.接受前端的图片
前端把图片转换成base64编码的字符串
格式如下:
String data = “data:image/jpeg;base64,逗号后才是真正图片的完整字符编码集”;
String base64Data = data.split(",")[1];//截取有效数据
2.
将字符串用base解码成字节数组–>再转化为流
byte[] bytes = new BASE64Decoder().decodeBuffer(imgTwo[i].split(",")[1]);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
OSS工具类将图片信息以流的形式上传到OSS服务器,成为最终图片
String s = new OSSUtil().uploadImageToOSS("", byteArrayInputStream);
3.OSSUtil工具类

package com.dtboom.huicai.common.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectRequest;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;

/**

  • @Description:

  • @Author: ztk

  • @Date: 2019/3/11 10:50
    /
    public class OSSUtil {
    private static String endpoint ;//oss服务器路径
    private static String accessKeyId ;//授权KeyId
    private static String accessKeySecret;//授权秘钥
    private static String bucketName ;//桶名–文件存放的分类下
    /
    *

    • OSS的文件夹名
      */
      private static String folder ;//存放图片的文件夹名
      private static String key ;//url前缀(远程oss服务器中桶的访问路径)

    /**

    • 上传图片
    • @param fileName 文件名
    • @param inputStream 流
      /
      public static String uploadImageToOSS(String fileName, InputStream inputStream) {
      /
      *
      • 创建OSS客户端
        */
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
        String uuid = UUID.randomUUID().toString().replaceAll("-", “”);
        String name = uuid + “.jpg”;
        ossClient.putObject(new PutObjectRequest(bucketName, folder + name, inputStream));
        return key + folder + name;
        } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e.getMessage());
        } finally {
        ossClient.shutdown();
        }
        return null;
        }

    /**

    • 多图上传
      /
      public static String[] uploadImageToOSS(String[] fileNames, InputStream[] inputStreams) {
      /
      *
      • 创建OSS客户端
        */
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
        ConcurrentLinkedQueue concurrentLinkedQueue = new ConcurrentLinkedQueue(
        Arrays.asList(fileNames));
        ConcurrentLinkedQueue streamConcurrentLinkedQueue = new ConcurrentLinkedQueue<>(
        Arrays.asList(inputStreams));
        Iterator inputStreamss = streamConcurrentLinkedQueue.iterator();
        ConcurrentLinkedQueue c = new ConcurrentLinkedQueue();
        for (Iterator iterator = concurrentLinkedQueue.iterator();
        iterator.hasNext() && inputStreamss.hasNext(); ) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", “”);
        String[] names = iterator.next().split("[.]");
        String name = uuid + “.” + names[names.length - 1];
        ossClient.putObject(new PutObjectRequest(bucketName, folder + name, inputStreamss.next()));
        c.add(key + folder + name);
        }
        return (String[]) c.toArray(new String[0]);
        } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e.getMessage());
        } finally {
        ossClient.shutdown();
        }
        return null;
        }

    /**

    • 删除图片 警告:在没有调用其他方法的情况下,请调用closeClient方法
    • @param url URL全路径
      /
      public void deleteImg(String url) {
      OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
      if (url == null || “”.equals(url)) {
      return;
      }
      String[] paths = url.split("[.]");
      /
      *
      • 文件夹是否存在
        /
        if (!ossClient.doesObjectExist(bucketName, folder)) {
        ossClient.putObject(bucketName, folder, new ByteArrayInputStream(new byte[0]));
        }
        String[] name = paths[paths.length - 2].split("[/]");
        /
        *
      • 对象是否存在
        /
        if (ossClient
        .doesObjectExist(bucketName,
        folder + name[name.length - 1] + “.” + paths[paths.length - 1])) {
        /
        *
        • 删除存在对象
          */
          ossClient
          .deleteObject(bucketName, folder + name[name.length - 1] + “.” + paths[paths.length - 1]);
          }
          ossClient.shutdown();
          }
          }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值