上传图片到阿里云并生成url的工具类

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
 
public class PictureUploadUtils {
    
    
	private static String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
	private static String accessKeyId = "<这里是公司的accessKeyId>";
	private static String accessKeySecret = "<这里是公司的accessKeySecret>";
	private static String bucketName = "<这里是公司的bucketName>";
	private static OSSClient ossClient;
 
	/** 上传文件到阿里云,并生成url
	 * @param filedir (key)文件名(不包括后缀)
	 * @param in 	文件字节流
	 * @param suffix 文件后缀名
	 * @return String 生成的文件url
	 */
	public static String UploadToAliyun(String filedir, InputStream in,String suffix) {
    
    
		System.out.println("------------>文件名称为:  " + filedir + "." + suffix);
		ossClient  = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		URL url = null;
		try {
    
    
			// 创建上传Object的Metadata
			ObjectMetadata objectMetadata = new ObjectMetadata();
			objectMetadata.setContentLength(in.available());
			objectMetadata.setCacheControl("no-cache");//设置Cache-Control请求头,表示用户指定的HTTP请求/回复链的缓存行为:不经过本地缓存
			objectMetadata.setHeader("Pragma", "no-cache");//设置页面不缓存
			objectMetadata.setContentType(getcontentType(suffix));
			objectMetadata.setContentDisposition("inline;filename=" + filedir + "." + suffix);
			// 上传文件
			ossClient.putObject(bucketName, filedir, in, objectMetadata);
			
			Date expiration = null;//过期时间
			String[] split = filedir.split("/");
			if(split[0].equals("circle")){
    
    // 朋友圈图片,设置URL过期时间为3个月 
				expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 90);
			}else{
    
    // 头像,设置URL过期时间为10年
				expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
			}
			// 生成URL
			url = ossClient.generatePresignedUrl(bucketName, filedir, expiration);
			
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			ossClient.shutdown();
			try {
    
    
				if (in != null) {
    
    
					in.close();
				}
			} catch (IOException e) {
    
    
				e.printStackTrace();
			}
		}
		return url.toString();
	}
 
	/**删除图片
	 * @param key 
	 */
	public static void deletePicture(String key){
    
    
		ossClient  = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		ossClient.deleteObject(bucketName, key);
		ossClient.shutdown();
	}
 
	/**
	* Description: 判断OSS服务文件上传时文件的contentType
	* @param suffix 文件后缀
	* @return String HTTP Content-type
	*/
	public static String getcontentType(String suffix) {
    
    
		System.out.println("------------>文件格式为:  " + suffix);
		if (suffix.equalsIgnoreCase("bmp")) {
    
    
			return "image/bmp";
		} else if (suffix.equalsIgnoreCase("gif")) {
    
    
			return "image/gif";
		} else if (suffix.equalsIgnoreCase("jpeg") || suffix.equalsIgnoreCase("jpg")) {
    
    
			return "image/jpeg";
		} else if (suffix.equalsIgnoreCase("png")) {
    
    
			return "image/png";
		} else if (suffix.equalsIgnoreCase("html")) {
    
    
			return "text/html";
		} else if (suffix.equalsIgnoreCase("txt")) {
    
    
			return "text/plain";
		} else if (suffix.equalsIgnoreCase("vsd")) {
    
    
			return "application/vnd.visio";
		} else if (suffix.equalsIgnoreCase("pptx") || suffix.equalsIgnoreCase("ppt")) {
    
    
			return "application/vnd.ms-powerpoint";
		} else if (suffix.equalsIgnoreCase("docx") || suffix.equalsIgnoreCase("doc")) {
    
    
			return "application/msword";
		} else if (suffix.equalsIgnoreCase("xml")) {
    
    
			return "text/xml";
		} else if (suffix.equalsIgnoreCase("mp3")) {
    
    
			return "audio/mp3";
		} else if (suffix.equalsIgnoreCase("amr")) {
    
    
			return "audio/amr";
		} else {
    
    
			return "text/plain";
		}
	}
}

参考:
阿里云开放平台: https://help.aliyun.com/document_detail/32012.html?spm=5176.doc32013.6.654.6y7YbO

API手册: http://aliyun_portal_storage.oss.aliyuncs.com/oss_api/oss_javahtml/index.html
整理未验证

猜你喜欢

转载自blog.csdn.net/bj_ameng/article/details/112170005
今日推荐