在Maven工程中使用OSS Java SDK

<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.10.2</version>
</dependency>

首先说下阿里上面的配置:

java怎么下载oss上面的图片_java怎么下载oss上面的图片

创建一个bucket,那么我们就可以获取到Bucket 和 Endpoint

java怎么下载oss上面的图片_状态码_02

接下来我们呢就要创建AccessKey,获取accessKeyId 和 accessKeySecret

java怎么下载oss上面的图片_java怎么下载oss上面的图片_03

 回到界面上看外网的域名

java怎么下载oss上面的图片_状态码_04

 这就是我们待会要配置到后台的东西。

java怎么下载oss上面的图片_ide_05

将配置配置到spring 容器中,方便我们获取配置信息

/**
 * @Author Lxq
 * @Date 2020/10/28 11:10
 * @Version 1.0
 * 阿里oss配置
 */
@Qualifier("aliyunOssConfig")
@ConditionalOnProperty(prefix = "oss.aliyun",name = "enable",havingValue = "true")
@Configuration
@ConfigurationProperties(prefix = "oss.aliyun")
public class AliyunOssConfig  implements Serializable {

    private String domain;
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
    private Boolean enable;

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    public String getBucketName() {
        return bucketName;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }
}


StorageService


/**
 * @Author Lxq
 * @Date 2020/10/28 11:23
 * @Version 1.0
 */
public interface StorageService {
    /**
     * Upload string
     *
     * @param data     data
     * @param fileName file name
     * @param path     path
     * @return the string
     * @throws IOException io exception
     */
    String upload(byte[] data, String fileName, String path) throws IOException;

    /**
     * Upload string
     *
     * @param inputStream input stream
     * @param fileName    file name
     * @param path        path
     * @return the string
     * @throws IOException io exception
     */
    String upload(InputStream inputStream, String fileName, String path);

    /**
     * Download input stream
     *
     * @param key key
     * @return the input stream
     * @throws IOException io exception
     */
    InputStream download(String key) throws IOException;

    /**
     * Download *
     *
     * @param key  key
     * @param path path
     * @throws InterruptedException interrupted exception
     * @throws IOException          io exception
     */
    void download(String key, String path) throws InterruptedException, IOException;

    /**
     * Delete *
     *
     * @param key key
     * @throws IOException io exception
     */
    void delete(String key) throws IOException;

    /**
     * 返回该文件的访问地址
     * @param objectName
     * @return
     */
    URL getUrl(String objectName);
}


AliyunStorageServiceImpl


/**
 * @Author Lxq
 * @Date 2020/10/28 11:26
 * @Version 1.0
 */
@ConditionalOnBean(AliyunOssConfig.class)
@Service
public class AliyunStorageServiceImpl implements StorageService {

    /**
     *   OSSClient实例
     */
    private OSS ossClient;

    /**
     * aliyun配置
     */
    private AliyunOssConfig config;

    @Autowired
    public AliyunStorageServiceImpl(AliyunOssConfig config) {
        this.config = config;
        ossClient = new OSSClientBuilder().build(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret());
    }

    @Override
    public String upload(byte[] data, String fileName, String path) throws IOException {
        return upload( new ByteArrayInputStream(data),fileName,path);
    }

    @Override
    public String upload(InputStream inputStream, String fileName, String path)  {
        path = PathUtil.resolvePath(path, fileName);
        /**
         * String yourBucketName, String yourObjectName, InputStream inputStream
         */
        try {
            ossClient.putObject(config.getBucketName(), path, inputStream);
        } catch (OSSException ex) {
            throw new OSSException("OSS异常,异常码=" + ex.getErrorCode() + " 异常信息=" + ex.getErrorMessage());
        } catch (ClientException ex) {
            throw new OSSException("OSS客户端异常,异常码=" + ex.getErrorCode() + " 异常信息=" + ex.getErrorMessage());
        }finally {
            // 关闭OSSClient。
            ossClient.shutdown();
        }
        return config.getDomain().concat("/").concat(path);
    }

    @Override
    public InputStream download(String key) throws IOException {
        // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。
        OSSObject object = ossClient.getObject(new GetObjectRequest(config.getBucketName(), key));
        return object.getObjectContent();
    }

    @Override
    public void download(String key, String path) throws InterruptedException, IOException {
        // 创建文件
        PathUtil.createFile(path);
        ossClient.getObject(new GetObjectRequest(config.getBucketName(),key),new File(path));
    }

    @Override
    public void delete(String key) throws IOException {
        // 删除文件。如需删除文件夹,请将ObjectName设置为对应的文件夹名称。如果文件夹非空,则需要将文件夹下的所有object删除后才能删除该文件夹。
        ossClient.deleteObject(config.getBucketName(), key);

    }

    @Override
    public URL getUrl(String objectName) {
        // 设置URL过期时间为1小时。
        Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
        // 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
        return ossClient.generatePresignedUrl(config.getBucketName(), "bOR4pQysRBGbGfnI_20201028140138_1603864898851.jpg", expiration);
    }
}


OssFactory 工厂


**
 * @Author Lxq
 * @Date 2020/10/28 13:01
 * @Version 1.0
 * oss工厂类,方便以后添加其他产品的oss
 */
@Component
public class OssFactory implements BeanFactoryAware {

    private Logger log = LoggerFactory.getLogger(OssFactory.class);

    private AliyunOssConfig aliyunOssConfig;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        boolean enable = false;
        if (beanFactory.containsBean(OssConstant.ALI_CONFIG)) {
            aliyunOssConfig = beanFactory.getBean(AliyunOssConfig.class);
            log.info("==>  启用阿里云OSS对象存储服务");
            enable = true;
        }
        if (!enable) {
            log.info("==>  未启用OSS服务 ...");
        }
    }

    public StorageService build(Integer ossType){
        StorageService storageService;
        // 根据不同的 OSS 类型创建不同的 OSS 实现类
        storageService = getStorageService(ossType);
        return storageService;
    }

    private StorageService getStorageService(Integer ossType) {
        StorageService storageService;
        switch (OSSTypeEnum.get(ossType)) {
            case ALI:
                storageService = new AliyunStorageServiceImpl(aliyunOssConfig);
                break;
//            case TX:
//                storageService = new TxStorageServiceImpl(txOssConfig);
//                break;
//            case QN:
//                storageService = new QnStorageServiceImpl(qnOssConfig);
//                break;
            default:
                throw new UnsupportedOperationException("不支持的OSS类型");
        }
        return storageService;
    }
}


AliyunOssController


/**
 * @Author Lxq
 * @Date 2020/10/28 11:20
 * @Version 1.0
 */
@RestController
@RequestMapping("/oss")
public class AliyunOssController {

    private static final Logger log = LoggerFactory.getLogger(AliyunOssController.class);


    @Autowired
    private OssFactory ossFactory;


    /**
     * 文件上传
     *
     * @param file 文件
     * @param path 文件夹路径
     * @return
     */
    @GetMapping("/upload")
    public RestResponse upload(@RequestPart(value = "file") MultipartFile file,
                               @RequestParam(value = "path", required = false) String path) {
        String res = null;

        StorageService storageService = ossFactory.build(0);
        try {
            res = storageService.upload(file.getInputStream(), file.getOriginalFilename(), "static");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }

        log.info("==>  文件上传结果:{}", res);
        return RestResponse.success(res);
    }

}

这里做了上传,其他自行编写。

最后加上文件中使用到的类


RestResponse


**
 * @Author Lxq
 * @Date 2020/10/28 11:57
 * @Version 1.0
 */
public class RestResponse extends HashMap<String, Object> {
    /**
     * 状态码
     */
    public static final String CODE_TAG = "code";
    /**
     * 返回内容
     */
    public static final String MSG_TAG = "msg";
    /**
     * 数据对象
     */
    public static final String DATA_TAG = "data";
    private static final long serialVersionUID = 1L;

    /**
     * 初始化一个新创建的 RestResponse 对象,使其表示一个空消息。
     */
    public RestResponse() {
    }

    /**
     * 初始化一个新创建的 RestResponse 对象
     *
     * @param code 状态码
     * @param msg  返回内容
     */
    public RestResponse(int code, String msg) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }

    /**
     * 初始化一个新创建的 RestResponse 对象
     *
     * @param code 状态码
     * @param msg  返回内容
     * @param data 数据对象
     */
    public RestResponse(int code, String msg, Object data) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        super.put(DATA_TAG, data);
    }

    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static RestResponse success() {
        return new RestResponse(200, "成功");
    }

    /**
     * 返回成功数据
     *
     * @return 成功消息
     */
    public static RestResponse success(Object data) {
        return new RestResponse(HttpStatus.SUCCESS, "操作成功", data);
    }

    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @return 成功消息
     */
    /*public static RestResponse success(String msg) {
        return RestResponse.success(msg, null);
    }*/

    /**
     * 返回成功消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static RestResponse success(String msg, Object data) {
        return new RestResponse(HttpStatus.SUCCESS, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @return
     */
    public static RestResponse error() {
        return RestResponse.error("操作失败");
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @return 警告消息
     */
    public static RestResponse error(String msg) {
        return RestResponse.error(msg, null);
    }

    /**
     * 返回错误消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static RestResponse error(String msg, Object data) {
        return new RestResponse(HttpStatus.ERROR, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @param code 状态码
     * @param msg  返回内容
     * @return 警告消息
     */
    public static RestResponse error(int code, String msg) {
        return new RestResponse(code, msg, null);
    }
}


HttpStatus


/**
 * @Author Lxq
 * @Date 2020/10/28 11:59
 * @Version 1.0
 */
public class HttpStatus {
    /**
     * 操作成功
     */
    public static final int SUCCESS = 200;

    /**
     * 对象创建成功
     */
    public static final int CREATED = 201;

    /**
     * 请求已经被接受
     */
    public static final int ACCEPTED = 202;

    /**
     * 操作已经执行成功,但是没有返回数据
     */
    public static final int NO_CONTENT = 204;

    /**
     * 资源已被移除
     */
    public static final int MOVED_PERM = 301;

    /**
     * 重定向
     */
    public static final int SEE_OTHER = 303;

    /**
     * 资源没有被修改
     */
    public static final int NOT_MODIFIED = 304;

    /**
     * 参数列表错误(缺少,格式不匹配)
     */
    public static final int BAD_REQUEST = 400;

    /**
     * 未授权
     */
    public static final int UNAUTHORIZED = 401;

    /**
     * 访问权限受限
     */
    public static final int FORBIDDEN = 403;

    /**
     * 资源,服务未找到
     */
    public static final int NOT_FOUND = 404;

    /**
     * 不允许的http方法
     */
    public static final int BAD_METHOD = 405;

    /**
     * 资源冲突,或者资源被锁
     */
    public static final int CONFLICT = 409;

    /**
     * 不支持的数据,媒体类型
     */
    public static final int UNSUPPORTED_TYPE = 415;

    /**
     * 系统内部错误
     */
    public static final int ERROR = 500;

    /**
     * 接口未实现
     */
    public static final int NOT_IMPLEMENTED = 501;
}


OSSTypeEnum


/**
 * @Author Lxq
 * @Date 2020/10/28 16:35
 * @Version 1.0
 */
public enum OSSTypeEnum {

    /**
     * Ali oss type enum
     */
    ALI(0, "阿里云", "阿里云 OSS 文件上传"),
    /**
     * Tx oss type enum
     */
    TX(1, "腾讯云", "腾讯云 OSS 文件上传"),
    /**
     * Qn oss type enum
     */
    QN(2, "七牛云", "七牛云 OSS 文件上传");


    private static volatile HashMap<Integer, OSSTypeEnum> map;

    private Integer code;
    private String name;
    private String desc;

    OSSTypeEnum() {
    }

    OSSTypeEnum(Integer code, String name, String desc) {
        this.code = code;
        this.name = name;
        this.desc = desc;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public static HashMap<Integer, OSSTypeEnum> getMap() {
        if (map == null) {
            synchronized (OSSTypeEnum.class) {
                if (map == null) {
                    map = new HashMap<>();
                    for (OSSTypeEnum v : OSSTypeEnum.values()) {
                        map.put(v.code, v);
                    }
                }
            }
        }
        return map;
    }

    public static void setMap(HashMap<Integer, OSSTypeEnum> map) {
        OSSTypeEnum.map = map;
    }

    public static String getNameByCode(Integer code) {
        String name = null;
        OSSTypeEnum v = getMap().get(code);
        if (v != null) {
            name = v.getName();
        }
        return name;
    }

    public static OSSTypeEnum get(Integer step) {
        return getMap().get(step);
    }
}


PathUtil


/**
 * @Author Lxq
 * @Date 2020/10/28 12:05
 * @Version 1.0
 */
public class PathUtil {
    /**
     * SLASH
     */
    private final static String SLASH = "/";

    /**
     * Resolve path string
     *
     * @param path     path
     * @param fileName file name
     * @return the string
     */
    public static String resolvePath(String path, String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            throw new NullPointerException("文件名称为空");
        }
        String randomStr = generateUniqueFileName();
        //获取后缀
        String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
        //修改后完整的文件名称
        fileName = randomStr + "." + prefix;


        // 处理 path
        if (!StringUtils.isEmpty(path)) {
            // 去除多余的 /
            path = path.replaceAll(OssConstant.SLASH_PLUS, OssConstant.SLASH);

            // 去前缀
            if (path.startsWith(SLASH)) {
                path = path.substring(1);
            }
            // 去后缀
            if (path.endsWith(OssConstant.SLASH)) {
                path = path.substring(0, path.length() - 1);
            }

            // 拼接 fileName
            fileName = fileName.replaceAll(OssConstant.SLASH_PLUS, OssConstant.SLASH);
            if (fileName.startsWith(SLASH)) {
                path = path.concat(fileName);
            } else {
                path = path.concat(SLASH).concat(fileName);
            }
        } else {
            // 拼接 fileName
            fileName = fileName.replaceAll(OssConstant.SLASH_PLUS, OssConstant.SLASH);
            if (fileName.startsWith(SLASH)) {
                path = fileName.substring(1);
            } else {
                path = fileName;
            }
        }

        return path;
    }

    static String generateUniqueFileName() {
        String filename = "";
        long millis = System.currentTimeMillis();
        String datetime = DateUtils.dateTimeNow();
        String rndchars = RandomStringUtils.randomAlphanumeric(16);
        filename = rndchars + "_" + datetime + "_" + millis;
        return filename;
    }

    /**
     * Create file file
     *
     * @param path path
     * @return the file
     * @throws IOException io exception
     */
    public static File createFile(String path) throws IOException {
        // 创建文件
        File file = new File(path);
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            file.createNewFile();
        }
        return file;
    }
}


DateUtils


/**
 * @Author Lxq
 * @Date 2020/10/28 12:05
 * @Version 1.0
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
        "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
        "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
        "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        //return new SimpleDateFormat(format).format(date);
        return DateFormatUtils.format(date, format);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return DateUtils.parseDate(ts, format);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
}