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

本文介绍了一个用于上传图片到阿里云OSS的服务工具类,该工具支持不同类型的文件上传,并能根据文件类型自动设置内容类型。同时,还提供了删除图片的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值