阿里雲伺服器 雲物件儲存OOS(二) ---圖片上傳與讀取demo
上一篇講解了無需程式碼操作的阿里OOS雲物件儲存
http://blog.csdn.net/u014520797/article/details/53945912
1、SDK下載,不下載也可以,文章最後有demo,demo裡面有jar包
https://help.aliyun.com/document_detail/31947.html?spm=5176.8041989.303605.24.voFC4A
2、demo結構
3、前期準備工作
如果你選擇的也是華南1 ,那麼endpoint不需要變動,後面三個accessKeyId,accessKeySecret,bucketName需要你自己準備。
4、endpoint
5、accessKeyId,accessKeySecret位置的在個人中心頁的右上角,將滑鼠放置賬號上即可看到
建立accessKeyId
7、主要程式碼
-
package com.kp.alioos;
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.InputStream;
-
import java.net.HttpURLConnection;
-
import java.net.URL;
-
import com.aliyun.oss.OSSClient;
-
import com.aliyun.oss.model.ObjectMetadata;
-
import com.aliyun.oss.model.PutObjectResult;
-
/**
-
* @author: py
-
* @version:2016年12月29日 下午1:54:35
-
* com.kp.alioos.OOSManager.java
-
* @Desc
-
*/
-
public class OOSManager {
-
/***下載媒體檔案*/
-
public static String DOWNLOAD_MEDIA_URL = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
-
/**
-
* @date 2016年12月29日下午3:12:33
-
* @param accessToken
-
* @param mediaId
-
* @param subPath askid提問id
-
* @throws Exception String
-
* @Des:上傳微信圖片至阿里OOS
-
*/
-
public static String uploadWXImage(String accessToken,String mediaId,String subPath) throws Exception{
-
String key = OSSConfigure.remoteFilePath+subPath+"/"+subPath+"_"+mediaId;
-
if (subPath==null) {
-
key = OSSConfigure.remoteFilePath+OSSConfigure.subPath_null+"/"+"_"+mediaId;
-
}
-
String requestUrl = DOWNLOAD_MEDIA_URL;
-
requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("MEDIA_ID", mediaId);
-
System.out.println(requestUrl);
-
try {
-
URL url = new URL(requestUrl);
-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
conn.setDoInput(true);
-
conn.setRequestMethod("GET");
-
String contentType = conn.getHeaderField("Content-Type");
-
// 根據內容型別獲取副檔名
-
String fileExt = MediaUtil.getFileEndWitsh(contentType);
-
// 將mediaId作為檔名
-
InputStream fileContent=conn.getInputStream();
-
//建立OSSClient例項
-
OSSClient ossClient=new OSSClient(OSSConfigure.endpoint, OSSConfigure.accessKeyId, OSSConfigure.accessKeySecret);
-
key=key+fileExt;
-
PutObjectResult putObject = ossClient.putObject(OSSConfigure.bucketName, key, fileContent);
-
System.err.println("putObjectgetETag:"+putObject.getETag());
-
System.err.println("putObjectgetRequestId:"+putObject.getRequestId());
-
System.err.println("putObjectgetClientCRC:"+putObject.getClientCRC());
-
System.err.println("putObjectgetServerCRC:"+putObject.getServerCRC());
-
return OSSConfigure.accessUrl+"/" +key;
-
} catch (Exception e) {
-
String error = String.format("下載媒體檔案失敗:%s", e);
-
System.out.println(error);
-
return null;
-
}
-
}
-
/**
-
* @date 2016年12月29日上午11:17:37
-
* @param ossConfigure
-
* @param file
-
* @param remotePath
-
* @throws Exception String
-
* @Des:上傳OSS伺服器檔案
-
*/
-
public static String uploadFile(File file,String remotePath) throws Exception{
-
InputStream fileContent=null;
-
fileContent=new FileInputStream(file);
-
OSSClient ossClient=new OSSClient(OSSConfigure.endpoint, OSSConfigure.accessKeyId, OSSConfigure.accessKeySecret);
-
String remoteFilePath = remotePath.substring(0, remotePath.length()).replaceAll("\\\\","/")+"/";
-
System.err.println("remoteFilePath:"+remoteFilePath);
-
//建立上傳Object的Metadata
-
ObjectMetadata objectMetadata=new ObjectMetadata();
-
objectMetadata.setContentLength(fileContent.available());
-
objectMetadata.setCacheControl("no-cache");
-
objectMetadata.setHeader("Pragma", "no-cache");
-
objectMetadata.setContentEncoding("utf-8");
-
objectMetadata.setContentType(contentType(file.getName().substring(file.getName().lastIndexOf("."))));
-
objectMetadata.setContentDisposition("inline;filename=" + file.getName());
-
//上傳檔案
-
PutObjectResult putObject = ossClient.putObject(OSSConfigure.bucketName, remoteFilePath + file.getName(), fileContent, objectMetadata);
-
System.err.println("putObjectgetETag:"+putObject.getETag());
-
System.err.println("putObjectgetRequestId:"+putObject.getRequestId());
-
System.err.println("putObjectgetClientCRC:"+putObject.getClientCRC());
-
System.err.println("putObjectgetServerCRC:"+putObject.getServerCRC());
-
return OSSConfigure.accessUrl+"/" +remoteFilePath + file.getName();
-
}
-
/**
-
* @date 2016年12月29日上午11:17:22
-
* @param ossConfigure
-
* @param filePath void
-
* @Des:根據key刪除OSS伺服器上的檔案
-
*/
-
public static void deleteFile(OSSConfigure ossConfigure,String filePath){
-
OSSClient ossClient=new OSSClient(OSSConfigure.endpoint, OSSConfigure.accessKeyId, OSSConfigure.accessKeySecret);
-
ossClient.deleteObject(OSSConfigure.bucketName, filePath);
-
}
-
/**
-
* @date 2016年12月29日上午11:17:11
-
* @param FilenameExtension
-
* @return String
-
* @Des:判斷OSS服務檔案上傳時檔案的contentType
-
*/
-
public static String contentType(String FilenameExtension){
-
if(FilenameExtension.equals("BMP")||FilenameExtension.equals("bmp")){return "image/bmp";}
-
if(FilenameExtension.equals("GIF")||FilenameExtension.equals("gif")){return "image/gif";}
-
if(FilenameExtension.equals("JPEG")||FilenameExtension.equals("jpeg")||
-
FilenameExtension.equals("JPG")||FilenameExtension.equals("jpg")||
-
FilenameExtension.equals("PNG")||FilenameExtension.equals("png")){return "image/jpeg";}
-
if(FilenameExtension.equals("HTML")||FilenameExtension.equals("html")){return "text/html";}
-
if(FilenameExtension.equals("TXT")||FilenameExtension.equals("txt")){return "text/plain";}
-
if(FilenameExtension.equals("VSD")||FilenameExtension.equals("vsd")){return "application/vnd.visio";}
-
if(FilenameExtension.equals("PPTX")||FilenameExtension.equals("pptx")||
-
FilenameExtension.equals("PPT")||FilenameExtension.equals("ppt")){return "application/vnd.ms-powerpoint";}
-
if(FilenameExtension.equals("DOCX")||FilenameExtension.equals("docx")||
-
FilenameExtension.equals("DOC")||FilenameExtension.equals("doc")){return "application/msword";}
-
if(FilenameExtension.equals("XML")||FilenameExtension.equals("xml")){return "text/xml";}
-
return "text/html";
-
}
-
}
測試類
-
package com.kp.alioos;
-
import java.io.File;
-
/**
-
* @author: py
-
* @version:2016年12月29日 上午11:34:50
-
* qwy.wx.uitl.alioos.TestOOS.java
-
* @Desc
-
*/
-
public class TestOOS {
-
public static void main(String[] args) {
-
// testWX();
-
test1();
-
}
-
/**
-
* @date 2016年12月30日下午8:29:22 void
-
* @Des: 測試上傳微信圖片
-
*/
-
private static void testWX() {
-
String accessToken="";
-
String mediaId="";
-
String subPath="";
-
try {
-
OOSManager.uploadWXImage(accessToken, mediaId, subPath);
-
} catch (Exception e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
/**
-
* @date 2016年12月30日下午8:29:44 void
-
* @Des:測試上傳本地圖片
-
*/
-
private static void test1() {
-
try {
-
File file = new File("E:/n2/wx_img/head.png");
-
String uploadFile = OOSManager.uploadFile(file , "test");
-
System.err.println("uploadFile:"+uploadFile);
-
} catch (Exception e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
8、阿里官方案例https://help.aliyun.com/document_detail/32013.html?spm=5176.doc32009.6.659.aTGNmi
9、demo下載 http://download.csdn.net/detail/u014520797/9725893
原文地址:https://blog.csdn.net/u014520797/article/details/53946121?utm_source=itdadao&utm_medium=referral