oss上传图片、文件功能,oss3.7.0版本和本地服务区图片上传工具类

本文基于
①3.7.0 oss包,使用oss上传文件功能
②没有oss服务,可以使用服务本地上传

在application.yml文件配置

oss:
  endpoint: oss-cn-beijing.aliyuncs.com/
  accessKeyID: 
  accessKeySecret: 
  bucketName: 
  filePath: picture/

file:
  #path: /usr/local/apache-tomcat-9.0.30/
  path: C:\Users\Administrator\Desktop\
  tomcatPicPath: picture/

在这里插入图片描述
controller层代码

package com.example.boot.ztestModule.controller;

import com.example.boot.framework.Result;
import com.example.boot.utils.oss.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author : KimWu
 * @date : 2020-03-27 12:34
 * @description:
 */
@RequestMapping("/file")
@RestController
@Slf4j
public class FileController {


    @RequestMapping("/ossAdd.do")
    public Result add(MultipartFile file) {
        String ossFilePath = FileUtils.ossAdd(file);
        return Result.success(ossFilePath);
    }

    @RequestMapping("/localAdd.do")
    public Result localAdd(MultipartFile file) {
        String ossFilePath = FileUtils.localAdd(file);
        return Result.success(ossFilePath);
    }
}

工具类代码

package com.example.boot.utils.oss;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.example.boot.framework.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author : KimWu
 * @date : 2020-03-27 13:52
 * @description:
 */
@Component
@Slf4j
public class FileUtils {
    private static String endpoint;

    @Value("${oss.endpoint}")
    public void setEndpoint(String endpoint) {
        FileUtils.endpoint = endpoint;
    }

    private static String accessKeyId;

    @Value("${oss.accessKeyID}")
    public void setAccessKeyId(String accessKeyId) {
        FileUtils.accessKeyId = accessKeyId;
    }

    private static String accessKeySecret;

    @Value("${oss.accessKeySecret}")
    public void setAccessKeySecret(String accessKeySecret) {
        FileUtils.accessKeySecret = accessKeySecret;
    }

    private static String bucketName;

    @Value("${oss.bucketName}")
    public void setBucketName(String bucketName) {
        FileUtils.bucketName = bucketName;
    }

    private static String ossFilePath;

    @Value("${oss.filePath}")
    public void setOssFilePath(String ossFilePath) {
        FileUtils.ossFilePath = ossFilePath;
    }

    private static String linuxFilePath;

    @Value("${file.path}")
    public void setLinuxFilePath(String linuxFilePath) {
        FileUtils.linuxFilePath = linuxFilePath;
    }

    private static String tomcatPicPath;

    @Value("${file.tomcatPicPath}")
    public void setTomcatPicPath(String tomcatPicPath) {
        FileUtils.tomcatPicPath = tomcatPicPath;
    }

    public static String ossAdd(MultipartFile file) {
        String ossUrlPath = "";
        OSS ossClient = null;
        try {
            String name = getFileName(file);
            //上传OSS
            // 创建OSSClient实例
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            //判断存储空间是否存在
            boolean exists = ossClient.doesBucketExist(bucketName);
            if (!exists) {
                throw new BusinessException("bucketName is not exists");
            } else {
                String objectName = ossFilePath + name;
                InputStream inputStream = file.getInputStream();
                PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
                ossClient.putObject(putObjectRequest);
                ossUrlPath = "https://" + bucketName + "." + endpoint + objectName;
            }
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error("upload error", e);
            }
            throw new BusinessException("上传失败");
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
        return ossUrlPath;
    }

    public static String localAdd(MultipartFile file) {
        String name = getFileName(file);
        String allPath = linuxFilePath + tomcatPicPath;
        String path = new File(StrUtil.builder(allPath, name).toString()).getAbsolutePath();
        FileUtil.mkParentDirs(path);
        try {
            file.transferTo(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tomcatPicPath + name;
    }


    private static String getFileName(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String name = StrUtil.builder(IdUtil.simpleUUID(), StrUtil.DOT,
                StrUtil.subAfter(originalFilename, StrUtil.DOT, true)).toString();
        return name;
    }
}


在这里插入图片描述
在服务器上便可以查看到上传的文件,接口中返回的是图片oss地址,可以直接访问或存储。

关注公众号
在这里插入图片描述

每周会更新干货知识

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值