文件(图片,音频)上传OSS并返回上传地址

为了加快速度这里用多线程

package cn.oss.test;

import java.io.InputStream;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.log4j.Logger;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;

public class OSSUpload {

	static ExecutorService pool = Executors.newFixedThreadPool(2);;
	static Logger logger = Logger.getLogger(OSSUpload.class);

	public static String startUpload(String fileName, InputStream input, int isMusic) {

		try {
			Future<String> re = pool.submit(new doUpload(fileName, input, isMusic));
			return re.get();
		} catch (InterruptedException | ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}


	static class doUpload implements Callable<String> {

		String fileName = "";
		InputStream input;
		int isMusic = 0;// 1音频2图片

		public doUpload(String fileName, InputStream input, int isMuisc) {
			this.fileName = fileName;
			this.input = input;
			this.isMusic = isMuisc;
		}

		public String call() throws Exception {
			String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
			String accessKeyId = "****************************";
			String accessKeySecret = "****************";
			String bucketName = "bucketName";
			// 创建OSSClient实例。
			OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
			// 上传文件流。
			// InputStream inputStream = file.getInputStream();
			ObjectMetadata metadata = new ObjectMetadata();
			metadata.setContentLength(input.available());
			metadata.setContentEncoding("utf-8");
			if (fileName.toLowerCase().endsWith("mp3")) {
				metadata.setContentType("audio/mp3");
			} else if (fileName.toLowerCase().endsWith("mp4")) {
				metadata.setContentType("video/mpeg4");
			} else if (fileName.toLowerCase().endsWith("wav")) {
				metadata.setContentType("audio/wav");
			} else if (fileName.toLowerCase().endsWith("jpg")) {
				metadata.setContentType("image/jpeg");
			} else if (fileName.toLowerCase().endsWith("jpeg")) {
				metadata.setContentType("image/jpeg");
			} else if (fileName.toLowerCase().endsWith("bmp")) {
				metadata.setContentType("image/bmp");
			} else if (fileName.toLowerCase().endsWith("gif")) {
				metadata.setContentType("image/gif");
			} else {// png
				metadata.setContentType("image/png");
			}
			if (isMusic == 1) {
				ossClient.putObject(bucketName, "mp3/" + fileName, input, metadata);
			} else {
				ossClient.putObject(bucketName, "images/" + fileName, input, metadata);
			}
			// ossClient.getObjectExtranetUrl
			// 关闭OSSClient。
			ossClient.shutdown();
			int bucketIndex = endpoint.indexOf("//") + 2;
			String httpFile = endpoint.substring(0, bucketIndex);
			// String fileUrl =
			// "http://testbank-file.oss-cn-shanghai.aliyuncs.com/mp3/"+fileName;
			String fileUrl = "";
			if (isMusic == 1) {
				fileUrl = httpFile + bucketName + "." + endpoint.substring(bucketIndex, endpoint.length()) + "/mp3/"
						+ fileName;
			} else {
				fileUrl = httpFile + bucketName + "." + endpoint.substring(bucketIndex, endpoint.length()) + "/images/"
						+ fileName;
			}
			return fileUrl;

		}
	}
}

测试方法

public static void main(String[] args) throws Exception {
		File file = new File("images/" + "imageName");
		InputStream input;
		input = new FileInputStream(file);
		String string = OSSUpload.startUpload("imageName", input, 0);
		System.out.println(string);
	}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值