SpringBoot上传文件到七牛云

SpringBoot上传文件到七牛云


pom依赖

<dependency>
    <groupId>com.qiniu</groupId>
    <artifactId>qiniu-java-sdk</artifactId>
    <version>7.2.27</version>
</dependency>

application.yml配置

qiniu:
  url: 填你的域名
  accessKey: 你的accesskey
  secretKey: 你的secretKey
  bucketname: 你的存储空间名

上传配置类

@Data
@Component
@ConfigurationProperties(prefix = "qiniu")
public class  QieNiuProperties {
    private  String accessKey;
    private  String secretKey;
    private  String bucketname;
    private  String url;
}

配置工具类

@Component
public class PictureUtile {
    @Resource
    private QieNiuProperties qieNiuProperties;
    public static String accessKey;
    public static String secretKey;
    public static String bucketname;
    public static String url;
    @PostConstruct
    public void init() {
        accessKey = this.qieNiuProperties.getAccessKey();
        secretKey = this.qieNiuProperties.getSecretKey();
        bucketname = this.qieNiuProperties.getBucketname();
        url = this.qieNiuProperties.getUrl();
    }
}

上传工具类

public class QianNiuUpload {
    /**
     * 上传图片,用本地路径名直接上传
     * accessKey secretKey, bucket, url这四个参数在配置文件里,用配置类QieNiuProperties
     * @param filePath
     * @return
     */
    public static String uploadImgByLocalFilePath(String filePath,String  accessKey ,String secretKey,String bucket,String url) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证,然后准备上传
       //如果是Windows情况下,格式是 D:\\qiniu\\test.png
        String localFilePath = filePath;
        //默认不指定key的情况下,以文件内容的hash值作为文件名
        String key = null;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return url+putRet.key;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 将内存中的字节数组上传到空间中 图片base64 不要,号前面的
     * @param imgStr
     * @return
     */
    public static String uploadImgByBytes(String imgStr){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证,然后准备上传
        String accessKey = PictureUtile.accessKey;
        String secretKey =  PictureUtile.secretKey;
        String bucket = PictureUtile.bucketname;
        //默认不指定key的情况下,以文件内容的hash值作为文件名
        String key = null;
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] uploadBytes = decoder.decodeBuffer(imgStr);
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);
            Response response = uploadManager.put(uploadBytes, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            return PictureUtile.url +putRet.key;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 流的形式
     * @param file
     * @return
     */
    public static String uploadFile(MultipartFile file) {
        //构造一个带指定Region对象的配置类  需要设置存储空间
        Configuration cfg = new Configuration(Region.huadong());
        UploadManager uploadManager= new UploadManager(cfg);
        Auth auth = Auth.create(PictureUtile.accessKey, PictureUtile.secretKey);
        String token = auth.uploadToken(PictureUtile.bucketname);
        try {
            String originalFilename = file.getOriginalFilename();
            // 文件后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileKey = UUID.randomUUID().toString() + suffix;
            Response response = uploadManager.put(file.getInputStream(), fileKey, token, null, null);
            return PictureUtile.url + fileKey;}
        catch (Exception e) {
            e.printStackTrace();
            return "null";
        }
    }
}

Region配置,这里代表空间的存取区域。
查看自己的存储区域可在空间概览的最下方查看到。
Region对应的设置:

在这里插入图片描述
Controller调用


 图片base64字符串 String iconImg
 String qiuNiuIconImg = QianNiuUpload.uploadImgByBytes(iconImg);

 MultipartFile file 流上传 
 String qiuNiuIconImg =QianNiuUpload.uploadFile(file);
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值