转载

java中的OssUtil工具类

OssUtil工具类可以少写一些代码,挺方便的一个工具包

版权声明:本文由 低调小熊猫 发表于 低调小熊猫的博客

转载声明:自由转载-非商用-非衍生-保持署名,非商业转载请注明作者及出处,商业转载请联系作者本人qq:2696284032

文章链接: https://aodeng.cc/archives/ossutil

代码

oss配置信息,配置在properties文件里面

oss.accessKeyId = 
oss.accessKeySecret = 
oss.endpoint = 
oss.bucketName =

OssUtil工具类代码

package com.account.web.service.common;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Component
public class OssUtil {
    @Value("${oss.accessKeyId}")
    public String accessKeyId;

    @Value("${oss.accessKeySecret}")
    public String accessKeySecret;

    @Value("${oss.endpoint}")
    public String endpoint;

    @Value("${oss.bucketName}")
    public String bucketName;
    public static OSSClient client;
    public OSSClient initClient(){
        if(null==client){
            ClientConfiguration conf = new ClientConfiguration();
            conf.setConnectionTimeout(5000);
            conf.setMaxErrorRetry(10);
            client = new OSSClient("http://" + endpoint, accessKeyId, accessKeySecret,conf);
        }
        return client;
    }
    /**
     * 上传文件 
     * 
     * @param bucketName 文件夹地址
     * @param key 对应文件名称
     * @param file  文件
     * @throws FileNotFoundException
     */
    public boolean putObjectForInputStream(String fileKey, InputStream content) {
        if (null == fileKey || "" == fileKey.trim())
            return false;
        try {
            initClient();
            ObjectMetadata meta = new ObjectMetadata();
            client.putObject(bucketName, fileKey, content, meta);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 上传图片
     * @param bucketName 文件夹地址
     * @param key 对应文件名称
     * @param file  文件
     * @throws FileNotFoundException
     */
    public String putObjectForImage(String imageName, InputStream content, boolean if400) {
        if (null == imageName || "" == imageName.trim())
            return null;
        try {
            String file = "images/" + imageName.substring(0, imageName.indexOf(".")) + "/" + imageName;
            putObjectForInputStream(file,content);
            return "http://"+bucketName+"."+endpoint+"/"+file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public String getObject(String key) {
        try {
            OSSObject object = client.getObject(bucketName, key);
            InputStream objectContent = object.getObjectContent();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int i = -1;
            while ((i = objectContent.read()) != -1) {
                baos.write(i);
            }
            objectContent.close();
            return baos.toString("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    public static void main(String[] args) {
        try {
            //String text = "<p><br/><br/>雪纺印花百褶裙半身裙,简约的线条和版型上身很好看,整体更有看点。时尚真皮女凉鞋 牛筋底中跟欧美潮鞋 2015夏个性森系女鞋子罗马鞋。</p><p><img src=/"/manager/plugins/ueditor/jsp/upload1/20160129/99731454031866641.png/"/></p>";
            //String s = OssUtil.uploadTextImg(text);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
原文  https://aodeng.cc/archives/ossutil
正文到此结束
Loading...