Commit 391cbb85 by songxiang

胡老板的需求改的方法

parent 1cbb0669
package com.pcloud.common.utils.aliyun;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
......@@ -50,19 +63,6 @@ import com.pcloud.common.utils.rsa.MD5;
import com.pcloud.common.utils.string.StringTools;
import com.pcloud.common.utils.string.StringUtil;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
/**
* @描述:
* @作者:songx
......@@ -934,11 +934,12 @@ public class OssUtils {
*/
public static UploadResultInfo getOSSResultByRegion(String transcodeJobId, String buckectName)
throws FileException {
if (AliyunConstant.LIVE_BUCKET.equals(buckectName))
if (AliyunConstant.LIVE_BUCKET.equals(buckectName)) {
return getOSSResult(transcodeJobId, AliyunConstant.LIVE_BUCKET);
else
} else {
return getOSSResult(transcodeJobId, AliyunConstant.FILE_BUCKET);
}
}
/**
* 组装转换后的地址
......@@ -1102,12 +1103,15 @@ public class OssUtils {
throw new FileException(FileException.FILE_DOWNLOAD_FAILURE, "下载文件失败");
} finally {
try {
if (is != null)
if (is != null) {
is.close();
if (os != null)
}
if (os != null) {
os.close();
if (ossClient != null)
}
if (ossClient != null) {
ossClient.shutdown();
}
} catch (Exception e) {
LOGGER.error("【aliOSS】下载文件,关闭文件流失败:" + e.getMessage(), e);
}
......@@ -1134,10 +1138,11 @@ public class OssUtils {
LOGGER.error("【aliOSS】下载文件失败:" + e.getMessage(), e);
throw new FileException(FileException.FILE_DOWNLOAD_FAILURE, "下载文件失败");
} finally {
if (ossClient != null)
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
* 组装返回的文件信息 <br> isPrivateKey=true,表示地址可下载,但需要加上密钥,密钥生产使用方法getHashValue或者addUrlKey <br>
......@@ -1224,12 +1229,7 @@ public class OssUtils {
* @return
*/
private static String getInObjectKey(AliyunEnum aliyunEnum, String fileName, String fileType) {
fileName = StringUtil.isEmpty(fileName) ? UUIDUitl.taskName()
: (FileUtils.formatName(fileName) + "_" + LocalDateUtils.getYmdhmss());
String gatherName = FileUtils.getGatherName(fileType);
gatherName = StringUtil.isEmpty(gatherName) ? "other" : gatherName.toLowerCase();
return new StringBuilder("oss/").append(aliyunEnum.value).append("/").append(gatherName).append("/").append(fileType)
.append("/").append(fileName).append(".").append(fileType).toString();
return getObjectKey(aliyunEnum, fileName, fileType);
}
/**
......@@ -1241,12 +1241,24 @@ public class OssUtils {
* @return
*/
private static String getOutObjectKey(AliyunEnum aliyunEnum, String fileName, String fileType) {
return getObjectKey(aliyunEnum, fileName, fileType);
}
/**
* 组装ObjectKey
*
* @param aliyunEnum
* @param fileName
* @param fileType
* @return
*/
private static String getObjectKey(AliyunEnum aliyunEnum, String fileName, String fileType) {
fileName = StringUtil.isEmpty(fileName) ? UUIDUitl.taskName()
: (FileUtils.formatName(fileName) + "_" + LocalDateUtils.getYmdhmss());
String gatherName = FileUtils.getGatherName(fileType);
gatherName = StringUtil.isEmpty(gatherName) ? "other" : gatherName.toLowerCase();
return new StringBuilder("oss/").append(aliyunEnum.value).append("/").append(gatherName).append("/").append(fileType)
.append("/").append(fileName).append(".").append(fileType).toString();
return new StringBuilder("oss/").append(aliyunEnum.value).append("/").append(gatherName).append("/")
.append(fileType).append("/").append(fileName).append(".").append(fileType).toString();
}
/**
......@@ -1537,13 +1549,13 @@ public class OssUtils {
* @return
* @throws FileException
*/
public static OssImageCropDTO imageCropSaveas(String fileUrl, int width, int height, int x, int y) throws FileException {
public static OssImageCropDTO imageCropSaveas(String fileUrl, int width, int height, int x, int y)
throws FileException {
LOGGER.info("【aliOSS】图片裁剪持久化,<START>.[fileUrl]=" + fileUrl);
ImageUtils.checkIsImage(fileUrl);
if (!(width >= 0 && width <= 4096 && height >= 0 && height <= 4096 && x >= 0 && y >= 0)) {
throw new FileException(FileException.PARAM_ERROR, "裁剪的长宽或者坐标数值必须大于0且长宽必须小于4096");
}
String fileName = FileUtils.getFileName(fileUrl);
String fileType = FileUtils.getFileType(fileUrl);
String bucketName = AliyunConstant.getBucketName(fileUrl);
String sourceKey = splitObjectKey(fileUrl);
......@@ -1553,7 +1565,8 @@ public class OssUtils {
style.append(",x_").append(x);
style.append(",y_").append(y);
style.append("|sys/saveas");
style.append(",o_").append(BinaryUtil.toBase64String(getOutObjectKey(AliyunEnum.CUT, fileName, fileType).getBytes()));
String outObjectKey = getOutObjectKey(AliyunEnum.CUT, null, fileType);
style.append(",o_").append(BinaryUtil.toBase64String(outObjectKey.getBytes()));
style.append(",b_").append(BinaryUtil.toBase64String(bucketName.getBytes()));
ProcessObjectRequest request = new ProcessObjectRequest(bucketName, sourceKey, style.toString());
// 创建OSSClient实例
......@@ -1774,7 +1787,7 @@ public class OssUtils {
* @param objectKey
* @return
*/
private static boolean doesObjectExist(String bucketName, String objectKey) {
private static boolean objectExist(String bucketName, String objectKey) {
OSSClient ossClient = getOSSClient(bucketName);
boolean found = ossClient.doesObjectExist(bucketName, objectKey);
// 关闭client
......@@ -1789,12 +1802,8 @@ public class OssUtils {
* @return true:001,false:002
*/
public static boolean fileUrlExist001(String fileUrl) {
boolean isUploadfe = false;
String objectKey = splitObjectKey(fileUrl);
if (doesObjectExist(AliyunConstant.INPUT_BUCKET, objectKey)) {
isUploadfe = true;
}
return isUploadfe;
return objectExist(AliyunConstant.INPUT_BUCKET, objectKey);
}
/**
......@@ -1804,12 +1813,8 @@ public class OssUtils {
* @return true:001,false:002
*/
public static boolean fileUrlExist002(String fileUrl) {
boolean isUploadfe = false;
String objectKey = splitObjectKey(fileUrl);
if (doesObjectExist(AliyunConstant.FILE_BUCKET, objectKey)) {
isUploadfe = true;
}
return isUploadfe;
return objectExist(AliyunConstant.FILE_BUCKET, objectKey);
}
}
package com.pcloud.common.utils.httpclient;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pcloud.common.utils.rsa.MD5;
import com.pcloud.common.utils.string.StringUtil;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
......@@ -15,8 +14,10 @@ import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.charset.Charset;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pcloud.common.utils.rsa.MD5;
import com.pcloud.common.utils.string.StringUtil;
/**
* @author:songx
......@@ -40,6 +41,11 @@ public class UrlUtils {
private static final String REQUEST_URL2 = "https://rays.5rs.me/convert/v1.0/url/shorten";
/**
* 请求地址(OWN)
*/
private static final String REQUEST_URL3 = "https://rays.5rs.me/convert/v1.0/url/expand?hex=";
/**
* 自定义生成 MD5 加密字符传前的混合 KEY
*/
public static final String PRIVATE_KEY = "lgsc1205";
......@@ -51,7 +57,7 @@ public class UrlUtils {
* @return
*/
public static String getShortUrl(String long_url) {
String result = callHttp(long_url);
String result = callHttp(REQUEST_URL + long_url);
if (StringUtil.isEmpty(result)) {
return null;
}
......@@ -64,13 +70,13 @@ public class UrlUtils {
/**
* 发送请求(新浪)
*
* @param long_url
* @param request
* @return
*/
private static String callHttp(String long_url) {
LOGGER.info("【URL】短链接转换(新浪),<START>.[long_url]=" + long_url);
private static String callHttp(String request) {
LOGGER.info("【URL】短链接转换(新浪),<START>.[request]=" + request);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(REQUEST_URL + long_url);
HttpGet httpGet = new HttpGet(request);
String resContent = null;
try {
CloseableHttpResponse response = httpclient.execute(httpGet);
......@@ -87,6 +93,22 @@ public class UrlUtils {
}
/**
* 获取长链接(OWN)
*
* @param hex
* @return
*/
public static String getUrl4Own(String hex) {
String result = callHttp(REQUEST_URL3 + hex);
if (StringUtil.isEmpty(result)) {
return null;
}
JSONObject resultObject = JSONObject.parseObject(result);
JSONObject dataObject = resultObject.getJSONObject("data");
return dataObject.getString("originUrl");
}
/**
* 获取短链接(OWN)
*
* @param long_url
......@@ -153,15 +175,15 @@ public class UrlUtils {
/**
* 缩短链接
*
* @param url
* @param hex
* @return
*/
public static String[] shortenUrlMd5(String hex) {
// 要使用生成 URL 的字符
String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z" };
"W", "X", "Y", "Z"};
String[] resUrl = new String[4];
for (int i = 0; i < 4; i++) {
......
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