为了加快速度这里用多线程
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);
}