Java操作阿里云OSS系统工具类

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import com.fdc.home.dec.service.inter.constant.UploadConstant;
import com.fdc.platform.common.yfutil.PropertyReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
* 阿里云工具类

*/
public class OSSUtils {
private static final Logger LOGGER = LoggerFactory
.getLogger(OSSUtils.class);

 /**
 * 获取阿里云OSS客户端对象 
 * */  
public static final OSSClient getOSSClient(){

    String accessKeyId = PropertyReader.getValue("accessKeyId");
    String accessKeySecret = PropertyReader.getValue("accessKeySecret");
    String bucketName = PropertyReader.getValue("bucketName");
    String ossHost = PropertyReader.getValue("ossHost");
    //com.aliyun.openservices.oss.OSSClient client = new com.aliyun.openservices.oss.OSSClient(accessKeyId, accessKeySecret);
    return new OSSClient(ossHost,accessKeyId, accessKeySecret);
}  
/**
 * 文件下载
 * @param client
 * @param bucketName
 * @param Objectkey 上传到阿里云的文件名称
 * @param filename 下载到本地的文件名称
 * @throws OSSException
 * @throws ClientException
 * @throws IOException
 */
public static void downloadFile(OSSClient client, String bucketName, String Objectkey, String filename)
        throws OSSException, ClientException, IOException {
    File file = new File(filename);
    if(!file.exists()){
        file.createNewFile();
    }
    client.getObject(new GetObjectRequest(bucketName, Objectkey),
            file);
}
/**
 * 向阿里云的OSS存储中存储文件 --file也可以用InputStream替代
 * 
 * @param client
 *            OSS客户端
 * @param is
 *            上传文件
 * @param bucketName
 *            bucket名称
 * @param diskName
 *            上传文件的目录 --bucket下文件的路径
 * @return String 唯一MD5数字签名
 * */
public static final String uploadObject2OSS(OSSClient client,InputStream is, String fileName,
        String bucketName, String diskName) {
    String resultStr=null;
    String aliImgUrl = PropertyReader.getValue("aliImgUrl");
    //String aliImgPath = PropertyReader.getValue("aliImgPath");
    try {
        // 创建上传Object的Metadata
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(is.available());
        metadata.setCacheControl("no-cache");
        metadata.setHeader("Pragma", "no-cache");
        metadata.setContentEncoding("utf-8");
        metadata.setContentType(getFileType(fileName));
        // 上传文件
        PutObjectResult putResult = client.putObject(bucketName, diskName
                + fileName, is, metadata);
        // 解析结果
         resultStr = putResult.getETag();
        LOGGER.info("上传阿里云OSS服务器响应结果." + resultStr);
        return aliImgUrl + diskName + fileName;
    } catch (Exception e) {
        e.printStackTrace();
         LOGGER.error("上传阿里云OSS服务器异常." + e.getMessage(), e);
        return "error";
    }
}

/**
 * 文件上传的类型
 * @param fileName
 * @return
 */
public static final String getFileType(String fileName) {

    String fileExtension = fileName.substring(fileName.lastIndexOf("."));
    String[] imgTypes = UploadConstant.IMG_TYPE;
    String[] fileType = UploadConstant.DOC_TYPE;
    for (String type : imgTypes) {
        if (type.equalsIgnoreCase(fileExtension)) {
            //return "image/" + type;
            return "application/octst-stream";
        }
    }
    for (String file : fileType) {
        if (file.equalsIgnoreCase(".doc") || file.equalsIgnoreCase(".docx")) {
            return "application/msword";
        }
        if (file.equalsIgnoreCase(".xls") || file.equalsIgnoreCase(".xlsx")) {
            return "application/vnd.ms-excel";
        }
        if (file.equalsIgnoreCase("txt") || file.equalsIgnoreCase("TXT"))
            return "application/octst-stream";
    }
    return "文件类型不正确!";

}

/**
 * 得到阿里云的文件读取流对象
 *
 * @param bucketName
 * @param key
 * @throws IOException
 */
public static InputStream getObject(String bucketName, String key)
        throws IOException {

    // 初始化OSSClient
    OSSClient client = OSSUtils.getOSSClient();

    // 获取Object,返回结果为OSSObject对象
    OSSObject object = client.getObject(bucketName, key);

    // 获取ObjectMeta
    ObjectMetadata meta = object.getObjectMetadata();

    // 获取Object的输入流
    InputStream objectContent = object.getObjectContent();

    // 处理Object

    // 关闭流
    // objectContent.close();

    return objectContent;
}

}

你可能感兴趣的:(java操作阿里云OSS)