1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 文件(图片 音频)上传OSS并返回上传地址

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

时间:2020-09-06 10:39:03

相关推荐

文件(图片 音频)上传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 blocke.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-";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 {// pngmetadata.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-/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);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。