Commit 9f6d1504 authored by Quxl's avatar Quxl

x

parent c0d63ae5
......@@ -5,6 +5,7 @@ import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -27,6 +28,7 @@ import com.egolm.film.bean.model.Fc_member_film_base_model;
import com.egolm.film.config.WebMvcConfig;
import com.egolm.film.config.XRException;
import com.egolm.film.model.LoginToken;
import com.egolm.film.util.OSSUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
......@@ -256,4 +258,27 @@ public class FilmController {
return Rjx.jsonOk().setData(ext);
}
@Value("${aliyun.sts.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.sts.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Value("${aliyun.oss.bucket}")
private String bucket;
@ResponseBody
@Transactional
@PostMapping("getOssMvPath")
@ApiOperation("获取OSS图片访问路径")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query", dataType="string", required=true, name="relativePath", value="相对路径"),
})
public Object getOssMvPath(String relativePath){
OSSUtils ossUtil = new OSSUtils(endpoint, accessKeyId, accessKeySecret);
String ossPicPath = ossUtil.urlSign(bucket, relativePath, 60*24);
ossUtil.closeOssClient();
return Rjx.jsonOk().setData(ossPicPath);
}
}
......@@ -5,7 +5,6 @@ import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -17,7 +16,6 @@ import com.egolm.common.jdbc.Page;
import com.egolm.film.api.service.OtherUploadService;
import com.egolm.film.bean.Fc_film_upload;
import com.egolm.film.util.FilmContrants;
import com.egolm.film.util.OSSUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
......@@ -183,29 +181,4 @@ public class FilmUploadController {
return Rjx.jsonOk();
}
@Value("${aliyun.sts.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.sts.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Value("${aliyun.oss.bucket}")
private String bucket;
@ResponseBody
@Transactional
@PostMapping("getOssMvPath")
@ApiOperation("获取OSS图片访问路径")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query", dataType="int", required=true, name="film_no", value="影片NO"),
@ApiImplicitParam(paramType="query", dataType="int", required=false, name="number", value="集数"),
@ApiImplicitParam(paramType="query", dataType="String", required=false, name="errorMessage", value="失败原因"),
})
public Object getOssMvPath(String tv_poster){
OSSUtils ossUtil = new OSSUtils(endpoint, accessKeyId, accessKeySecret);
String ossPicPath = ossUtil.urlSign(bucket, tv_poster, 60*24);
ossUtil.closeOssClient();
return Rjx.jsonOk().setData(ossPicPath);
}
}
package com.egolm.film.util;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.AppendObjectRequest;
import com.aliyun.oss.model.AppendObjectResult;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import com.egolm.common.DateUtil;
/**
*
* Title:
* Description:
* Company: www.egolm.com
* @author :张永
* @date :2016年3月20日
*/
public class OSSUtils {
public static String ACCESS_KEY_ID = "";//" ";
public static String ACCESS_KEY_SECRET = "";//" ";
public static String ENDPOINT = "";// //以HTTP协议,通过用户自定义域名(CNAME)访问某个Bucket;
public static String OUT_OF_DATE = "18000";
private final OSSClient client;
// 初始化一个OSSClient
public OSSUtils(){
this.client = new OSSClient(ENDPOINT,ACCESS_KEY_ID, ACCESS_KEY_SECRET);
}
public OSSUtils(String endPoint,String access_key_id,String access_key_secret){
this.client = new OSSClient(endPoint,access_key_id, access_key_secret);
}
/**
* 创建容器
* @param bucketName 容器
*/
public boolean createBucket(String bucketName) {
try {
if(!client.doesBucketExist(bucketName)){
client.createBucket(bucketName);
System.out.println("bucket:不存在,创建:"+bucketName);
}else{
System.out.println("bucket:已存在 "+bucketName);
}
return true;
} catch (OSSException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return false;
}
/**
* @Description: 上传文件到OSS文件服务器
* @bucketName 容器
* @param content 文件流
* @param key 上传为OSS文件服务器的唯一标识
* @param mimeType 文档类型
* @throws Exception
* @ReturnType:String OSSObject的ETag值。
*/
public String uploadFile(String bucketName, String key, String mimeType, InputStream is) {
System.out.println("content==="+is);
//进行初始化
ObjectMetadata meta = new ObjectMetadata();
// 必须设置ContentLength
meta.setContentType(mimeType);
// 上传Object.
System.out.println("*****************上传图片到oss服务器开始*****************" + key);
PutObjectResult result = client.putObject(bucketName, key, is,meta);
System.out.println("*****************上传图片到oss服务器结束*****************" + key);
return result.getETag();
}
/**
* @Description: 上传文件到OSS文件服务器
* @param bucketName 容器
* @param filePath 文件地址
* @param key 上传为OSS文件服务器的唯一标识
* @param mimeType
* @ReturnType:String OSSObject的ETag值。
*/
public String uploadFile(String bucketName, String filePath, String key) {
//进行初始化
System.out.println(client);
// 上传Object.
System.out.println("*****************上传图片到oss服务器开始*****************" + key);
PutObjectResult result = client.putObject(bucketName, key, new File(filePath));
System.out.println("*****************上传图片到oss服务器结束*****************" + key);
return result.getETag();
}
/**
* @Description: 根据key获取oss服务器上的图片地址
* @param key
* @param bucketName
* @return
* @ReturnType:String
*/
public String getImgURl(String key, String bucketName) {
Date expires = new Date (new Date().getTime() + 10*365*24*3600*1000); // 30 minute to expires
GeneratePresignedUrlRequest generatePresignedUrlRequest ;
generatePresignedUrlRequest =new GeneratePresignedUrlRequest(bucketName, key);
generatePresignedUrlRequest.setExpiration(expires);
URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
return url.toString();
}
/**
* @Description:删除文件
* @param bucketName
* @param key OSS文件服务器上文件的唯一标识
* @ReturnType:void
*/
public void deleteObjectFile(String bucketName,String key) {
client.deleteObject(bucketName, key);
}
/**
* 下载到本地文件
* @param bucketName
* @param filePath
*/
public boolean downLoadObjectFile(String bucketName,String key,String filePath){
try {
client.getObject(new GetObjectRequest(bucketName,key), new File(filePath));
return true;
} catch (OSSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
/**
* @Description: 断点上传文件到OSS文件服务器
* @param bucketName
* @param content 文件流
* @param key 上传为OSS文件服务器的唯一标识
* @param position 位置
*/
public String appendObjectFile(String bucketName, InputStream content, String key, int position, String mimeType) {
//进行初始化
// 必须设置ContentLength
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(position);
meta.setContentType(mimeType);
meta.setCacheControl("no-cache");
meta.setContentEncoding("utf-8");
// 上传
System.out.println("*****************断点上传图片到oss服务器开始*****************" + key);
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, content, meta);
appendObjectRequest.setPosition(Long.valueOf(position));
AppendObjectResult appendObjectResult =client.appendObject(appendObjectRequest);
System.out.println("*****************断点上传图片到oss服务器结束*****************" + key);
return appendObjectResult.getNextPosition().toString();
}
/**
* 列出存储空间内所有文件
* @param bucketName
*/
public List<String> getListObject(String bucketName) {
List<String> list = new ArrayList<String>();
// 构造ListObjectsRequest请求
ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
ObjectListing listing = client.listObjects(listObjectsRequest);
// 遍历所有Object
System.out.println("Objects:");
for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) {
System.out.println(objectSummary.getKey());
list.add(objectSummary.getKey());
}
//遍历OBJECT下的文件夹的数据
// 遍历所有CommonPrefix
System.out.println("\nCommonPrefixs:");
for (String commonPrefix : listing.getCommonPrefixes()) {
System.out.println(commonPrefix);
}
return list;
}
public String urlSign(String bucketName,String objectName,int expiryTime) {
// 设置URL过期时间为1小时。
Date expiration = new Date(new Date().getTime() + expiryTime * 60 * 1000);
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL url = client.generatePresignedUrl(bucketName, objectName, expiration);
System.out.println(url);
return url.toString();
}
public static void main(String[] args) throws Exception {
int expiryTime =60;
Date startDate = new Date();
System.out.println(startDate);
Date expiration = new Date(startDate.getTime() + expiryTime * 60 * 1000);
System.out.println(DateUtil.format(expiration, DateUtil.FMT_DATE_SECOND));
OSSUtils ossUtil = new OSSUtils("oss-cn-shanghai.aliyuncs.com", "LTAIOtHCCpDLXYp8", "9XTHW7P9TTRvCsBHBSclOue2tdWOoa");
String ossPosters = ossUtil.urlSign("siff-tv-pic", "oss-h5-upload-js-direct.zip", Integer.valueOf(60)); //过期时间为1小时
System.out.println(ossPosters);
ossUtil.closeOssClient();
}
public void closeOssClient() {
if(client != null){
client.shutdown();
}
}
public String getFileNewName(String fileName){
Calendar c = new GregorianCalendar();
String year = Integer.toString(c.get(1));
String month = Integer.toString(c.get(2) + 1);
String day = Integer.toString(c.get(5));
String minute = Integer.toString(c.get(12));
String hour = Integer.toString(c.get(11));
String second = Integer.toString(c.get(13));
if (Integer.parseInt(month) < 10) {
month = "0" + month;
}
if (Integer.parseInt(day) < 10) {
day = "0" + day;
}
Random random = new Random();
int k = random.nextInt(100000000);
if (Integer.parseInt(hour) < 10) {
hour = "0" + hour;
}
if (Integer.parseInt(minute) < 10) {
minute = "0" + minute;
}
if (Integer.parseInt(second) < 10) {
second = "0" + second;
}
String newFileName = year + "_" + month + "_" + day + "_" + hour + "_" + minute + "_" + second + "_" + k + fileName.substring(fileName.lastIndexOf("."),
fileName.length());
// String filePath = year+"/"+month+"/"+day+"/"+newFileName;
String filePath = newFileName;
return filePath;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment