Java上传图片到阿里云OSS并回显URL

Java上传图片到阿里云OSS并回显URL

关键代码

OSSClientHandler.java

import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;

@Component
public class OSSClientHandler {

	@Value("${aliyun.endpoint}")
	private String endpoint;

	@Value("${aliyun.accessKeyId}")
	private String accessKeyId;

	@Value("${aliyun.accessKeySecret}")
	private String accessKeySecret;

	@Value("${aliyun.bucket}")
	private String bucket;

	private OSS ossClient;

	private static final SimpleDateFormat DF = new SimpleDateFormat("yyyy/MM/dd");

	private static final Long expirationTime = 3600 * 1000 * 24 * 7L;// 图片过期时长(7天)

	public static void main(String[] args) throws Exception {
		OSSClientHandler aa = new OSSClientHandler();
		System.out.println("hao");
	}

	// 上传图片+获取URL
	public ImageInfo uploadImgAndGetUrl(MultipartFile file) throws Exception {
		getOSS();
		String orgFileName = file.getOriginalFilename();// 获取原始文件名
		String suffix = orgFileName.substring(orgFileName.lastIndexOf("."));
		String newName = UUID.randomUUID().toString().replace("-","") + suffix;
		String filePath = DF.format(new Date()) + "/" + newName;
		ObjectMetadata objectMetadata = new ObjectMetadata();
		objectMetadata.setContentType(getcontentType(suffix));
		ossClient.putObject(bucket, filePath, file.getInputStream(), objectMetadata);
		ImageInfo imageInfo = new ImageInfo(filePath, System.currentTimeMillis(),
				System.currentTimeMillis() + expirationTime);
		Date expiration = new Date(imageInfo.getExpireTime());
		URL url = ossClient.generatePresignedUrl(bucket, filePath, expiration);
		imageInfo.setUrl(url.toString());
		return imageInfo;
	}

	// 获取URL
	public String getUrl(String filePath) {
		getOSS();
		Date expiration = new Date(new Date().getTime() + expirationTime);
		URL url = ossClient.generatePresignedUrl(bucket, filePath, expiration);
		return url.toString();
	}

	private static String getcontentType(String FilenameExtension) {
		if (FilenameExtension.equalsIgnoreCase(".bmp")) {
			return "image/bmp";
		}
		if (FilenameExtension.equalsIgnoreCase(".gif")) {
			return "image/gif";
		}
		if (FilenameExtension.equalsIgnoreCase(".jpeg") || FilenameExtension.equalsIgnoreCase(".jpg")
				|| FilenameExtension.equalsIgnoreCase(".png")) {
			return "image/jpg";
		}
		return "image/jpg";
	}

	private OSS getOSS() {
		if (null == ossClient) {
			ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
		}
		return ossClient;
	}

	public void upload(String fileUrl, InputStream inputStream) {
		try {
			getOSS();
			ossClient.putObject(bucket, fileUrl, inputStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public InputStream download(String fileUrl) {
		try {
			getOSS();
			OSSObject ossObject = ossClient.getObject(bucket, fileUrl);
			return ossObject.getObjectContent();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

ImageInfo.java

import java.io.Serializable;

import lombok.Data;

@Data
public class ImageInfo implements Serializable {

	private static final long serialVersionUID = 6448908249255948262L;

	String filePath;
	Long createTime;
	Long expireTime;
	String url;

	public ImageInfo() {
	}

	public ImageInfo(String filePath, Long createTime, Long expireTime) {
		this.filePath = filePath;
		this.createTime = createTime;
		this.expireTime = expireTime;
	}
}

你可能感兴趣的:(图片上传OSS,上传OSS并回显)