前段時間正好作個項目,就是用的oss,普通上傳和斷點續傳,代碼貼出來,看是否是大家須要的
java
public class OssManage {
public static String ACCESS_KEY_ID = null;
public static String ACCESS_KEY_SECRET = null;
public static String ENDPOINT = null;
public static String BUCKETNAME = null;
public static String BUCKETNAME_VIDEO = null;
public static String OUT_OF_DATE = null;
private OSSClient client = null;
private ObjectMetadata meta = null;
static{
try {
ACCESS_KEY_ID = SysConst.getProperty("OSS_ACCESS_KEY_ID");
ACCESS_KEY_SECRET = SysConst.getProperty("OSS_ACCESS_KEY_SECRET");
ENDPOINT = SysConst.getProperty("OSS_ENDPOINT");
BUCKETNAME = SysConst.getProperty("OSS_BUCKETNAME");
BUCKETNAME_VIDEO = SysConst.getProperty("OSS_BUCKETNAME_VIDEO");
OUT_OF_DATE = SysConst.getProperty("OSS_OUT_OF_DATE");
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
public void init(){
// 初始化一個OSSClient
client = new OSSClient(ENDPOINT,ACCESS_KEY_ID, ACCESS_KEY_SECRET);
meta = new ObjectMetadata();
}
/**
* @Description: 上傳文件到OSS文件服務器
* @param content 文件流
* @param key 上傳爲OSS文件服務器的惟一標識
* @param mimeType 文檔類型
* @throws Exception
* @ReturnType:String OSSObject的ETag值。
*/
public String uploadFile(InputStream content,String key,String mimeType) throws Exception{
//進行初始化
init();
// 必須設置ContentLength
meta.setContentType(mimeType);
// 上傳Object.
LogUtil.printConsoleLog("*****************上傳圖片到oss服務器開始*****************" + key);
PutObjectResult result = client.putObject(BUCKETNAME, key, content, meta);
LogUtil.printConsoleLog("*****************上傳圖片到oss服務器結束*****************" + key);
return result.getETag();
}
/**
* @Description: 根據key獲取oss服務器上的圖片地址
* @param key
* @return
* @ReturnType:String
*/
public String getImgURl(String key){
init();
Date expires = new Date (new Date().getTime() + Integer.parseInt(OUT_OF_DATE)); // 30 minute to expire
GeneratePresignedUrlRequest generatePresignedUrlRequest ;
if(key.startsWith("msplatform")){
generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME, key);
}else {
generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME_VIDEO, key);
}
generatePresignedUrlRequest.setExpiration(expires);
URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
return url.toString();
}
/**
* @Description:根據key獲取oss服務器上的ipa文件地址
* @param key
* @return
* @ReturnType:String
*/
public String getIpaURl(String key){
init();
Date expires = new Date(new Date().getTime()+ 10*365*24*3600*1000);
GeneratePresignedUrlRequest generatePresignedUrlRequest ;
if(key.startsWith("msplatform")){
generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME, key);
}else {
generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME_VIDEO, key);
}
generatePresignedUrlRequest.setExpiration(expires);
URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
return url.toString();
}
/**
* @Description: 根據key獲取oss服務器上的圖片地址
* @param key
* @return
* @ReturnType:String
*/
public InputStream getObject(String key){
init();
OSSObject object = null;
if(key.startsWith("msplatform")){
object= client.getObject(BUCKETNAME, key);
}else{
object= client.getObject(BUCKETNAME_VIDEO, key);
}
object.getObjectMetadata().getContentType();
// 獲取Object的輸入流
InputStream objectContent = object.getObjectContent();
return objectContent;
}
/**
* @Description:刪除文件
* @param key OSS文件服務器上文件的惟一標識
* @ReturnType:void
*/
public void deleteFile(String key){
init();
client.deleteObject(BUCKETNAME, key);
}
/**
* @Description: 斷點上傳文件到OSS文件服務器
* @param content 文件流
* @param key 上傳爲OSS文件服務器的惟一標識
* @param position 位置
*/
public String appendObjectFile(InputStream content,String key,int position,String mimeType) throws Exception{
//進行初始化
init();
// 必須設置ContentLength
meta.setContentLength(position);
meta.setContentType(mimeType);
meta.setCacheControl("no-cache");
meta.setContentEncoding("utf-8");
// 上傳
LogUtil.printConsoleLog("*****************斷點上傳圖片到oss服務器開始*****************" + key);
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(BUCKETNAME, key, content, meta);
appendObjectRequest.setPosition(Long.valueOf(position));
AppendObjectResult appendObjectResult =client.appendObject(appendObjectRequest);
LogUtil.printConsoleLog("*****************斷點上傳圖片到oss服務器結束*****************" + key);
return appendObjectResult.getNextPosition().toString();
}
public static void main(String[] args) {
OssManage ossManage = new OssManage();
}
}
詳細的配置信息能夠參考這篇文章:http://blog.ncmem.com/wordpress/2019/08/12/java%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6%e5%a4%b9%e5%88%b0oss-2/服務器