|
@@ -0,0 +1,501 @@
|
|
|
+package com.jpsoft.excellent.modules.common.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.codec.Base64Encoder;
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import com.aliyun.oss.HttpMethod;
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
+import com.aliyun.oss.OSSClientBuilder;
|
|
|
+import com.aliyun.oss.model.GeneratePresignedUrlRequest;
|
|
|
+import com.aliyun.oss.model.OSSObject;
|
|
|
+import com.aliyun.oss.model.PutObjectResult;
|
|
|
+import com.jpsoft.excellent.config.OSSConfig;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.*;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class OSSUtil {
|
|
|
+ public static String upload(OSSConfig ossConfig,
|
|
|
+ String subFolder, String fileName,
|
|
|
+ InputStream fileInputStream,String watermark) {
|
|
|
+ String retUrl = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ Image srcImage = ImageIO.read(fileInputStream);
|
|
|
+ int imageWidth = srcImage.getWidth(null);
|
|
|
+ int imageHeight = srcImage.getHeight(null);
|
|
|
+
|
|
|
+ int fontHeight = imageHeight / 20;
|
|
|
+
|
|
|
+ fontHeight = Math.min(30, fontHeight);
|
|
|
+
|
|
|
+ Color color = new Color(128, 128, 128, 200); // 水印颜色
|
|
|
+ Font font = new Font("宋体", Font.ITALIC, fontHeight); //水印字体
|
|
|
+
|
|
|
+ BufferedImage bufferedImage = new BufferedImage(
|
|
|
+ srcImage.getWidth(null), srcImage.getHeight(null),
|
|
|
+ BufferedImage.TYPE_INT_RGB);
|
|
|
+
|
|
|
+ Graphics2D g = bufferedImage.createGraphics();
|
|
|
+
|
|
|
+ g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
|
+ g.drawImage(srcImage.getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH), 0,
|
|
|
+ 0, null);
|
|
|
+
|
|
|
+ g.setColor(color); //水印颜色
|
|
|
+ g.setFont(font); //水印字体
|
|
|
+
|
|
|
+ int markWidth = g.getFontMetrics(g.getFont()).charsWidth(watermark.toCharArray(), 0, watermark.length());
|
|
|
+
|
|
|
+ g.rotate(Math.toRadians(-45), imageWidth / 2, imageHeight / 2);
|
|
|
+
|
|
|
+ for (int i=0;i<imageHeight/3;i++) {
|
|
|
+ g.drawString(watermark, (imageWidth - markWidth) / 2, imageHeight*i/3); //水印位置
|
|
|
+ }
|
|
|
+
|
|
|
+ g.dispose(); //释放资源
|
|
|
+
|
|
|
+ ByteArrayOutputStream outImgStream = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(bufferedImage, "jpg", outImgStream);
|
|
|
+
|
|
|
+ byte[] outImg = outImgStream.toByteArray();
|
|
|
+
|
|
|
+ ByteArrayInputStream inputImgStream = new ByteArrayInputStream(outImg);
|
|
|
+
|
|
|
+ retUrl = upload(ossConfig,subFolder,fileName,inputImgStream);
|
|
|
+ }
|
|
|
+ catch (Exception ex){
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ }
|
|
|
+
|
|
|
+ return retUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String upload(OSSConfig ossConfig,
|
|
|
+ String subFolder, String fileName,
|
|
|
+ InputStream fileInputStream) {
|
|
|
+ Date now = new Date();
|
|
|
+
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(now);
|
|
|
+
|
|
|
+ String savePath = ossConfig.getObjectPre();
|
|
|
+
|
|
|
+ if (!subFolder.startsWith("/")) {
|
|
|
+ savePath += "/";
|
|
|
+ }
|
|
|
+
|
|
|
+ savePath += subFolder;
|
|
|
+
|
|
|
+ savePath = savePath + "/" + cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/";
|
|
|
+
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
|
|
+
|
|
|
+ int index = fileName.indexOf(".");
|
|
|
+
|
|
|
+// String prefix = fileName.substring(0,index);
|
|
|
+
|
|
|
+ String ext = fileName.substring(index);
|
|
|
+
|
|
|
+ String newFileName = DateTime.now().toString("ddHHmmssSSS") + ext;
|
|
|
+
|
|
|
+ String retFileUrl = savePath + newFileName;
|
|
|
+
|
|
|
+ // 上传文件流
|
|
|
+ PutObjectResult result = ossClient.putObject(ossConfig.getBucketName(), retFileUrl, fileInputStream);
|
|
|
+
|
|
|
+ // 关闭OSSClient
|
|
|
+ ossClient.shutdown();
|
|
|
+
|
|
|
+ return ossConfig.getUrlPrefix() + "/" + retFileUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean download(String fileUrl,String filePath){
|
|
|
+ boolean result;
|
|
|
+
|
|
|
+ try {
|
|
|
+ FileOutputStream output = new FileOutputStream(filePath);
|
|
|
+
|
|
|
+ URL url = new URL(encodeFileName(fileUrl));
|
|
|
+ URLConnection conn = url.openConnection();
|
|
|
+ InputStream input = conn.getInputStream();
|
|
|
+
|
|
|
+ byte[] buffs = new byte[1024 * 10];
|
|
|
+
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(input, 1024 * 10);
|
|
|
+
|
|
|
+ int read;
|
|
|
+ while ((read = bis.read(buffs, 0, 1024 * 10)) != -1) {
|
|
|
+ output.write(buffs, 0, read);
|
|
|
+ }
|
|
|
+
|
|
|
+ input.close();
|
|
|
+ output.close();
|
|
|
+
|
|
|
+ result = true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ result = false;
|
|
|
+ log.error(e.getMessage(),e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ public static String downloadToBase64(String fileUrl){
|
|
|
+ ByteArrayOutputStream data = new ByteArrayOutputStream();;
|
|
|
+ try {
|
|
|
+ // FileOutputStream output = new FileOutputStream();
|
|
|
+ // OutputStream outputStream =null;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ URL url = new URL(encodeFileName(fileUrl));
|
|
|
+ // URLConnection conn = url.openConnection();
|
|
|
+ // InputStream input = conn.getInputStream();
|
|
|
+
|
|
|
+ byte[] buffs = new byte[1024 * 10];
|
|
|
+ //创建连接
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn.setRequestMethod("GET");
|
|
|
+ conn.setConnectTimeout(5000);
|
|
|
+ InputStream input = conn.getInputStream();
|
|
|
+ //将内容读到内存中
|
|
|
+ int len = -1;
|
|
|
+ while ((len =input.read(buffs))!=-1){
|
|
|
+ data.write(buffs,0,len);
|
|
|
+ }
|
|
|
+ input.close();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ log.error(e.getMessage(),e);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return Base64Encoder.encode(data.toByteArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean deleteFile(OSSConfig ossConfig,String filePath) {
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
|
|
+
|
|
|
+ String key = "";
|
|
|
+
|
|
|
+ if (filePath.startsWith(ossConfig.getUrlPrefix())) {
|
|
|
+ key = filePath.substring(ossConfig.getUrlPrefix().length());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (key.startsWith("/")) {
|
|
|
+ key = key.substring(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean exist = ossClient.doesObjectExist(ossConfig.getBucketName(), key);
|
|
|
+
|
|
|
+ if (!exist) {
|
|
|
+ log.error("文件不存在,key={}", key);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("删除文件,key={}", key);
|
|
|
+ ossClient.deleteObject(ossConfig.getBucketName(), key);
|
|
|
+
|
|
|
+ ossClient.shutdown();
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void batchDownload(List<Map<String,Object>> fileList, OutputStream output){
|
|
|
+ try{
|
|
|
+ ZipOutputStream zos = new ZipOutputStream(output);
|
|
|
+
|
|
|
+ for (Map<String,Object> map : fileList) {
|
|
|
+ String fileUrl = (String)map.get("fileUrl");
|
|
|
+ String filePath = (String)map.get("filePath");
|
|
|
+ String fileName = (String)map.get("fileName");
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (StringUtils.isEmpty(fileName)) {
|
|
|
+ fileName = fileUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (fileName.indexOf("?") != -1) {
|
|
|
+ fileName = fileName.substring(0, fileName.indexOf("?"));
|
|
|
+ }
|
|
|
+
|
|
|
+ fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
|
|
|
+
|
|
|
+ String zipFile = fileName;
|
|
|
+
|
|
|
+ if(StringUtils.isNotEmpty(filePath)){
|
|
|
+ zipFile = filePath + fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(StringUtils.isNotEmpty(fileUrl)) {
|
|
|
+ ZipEntry zipEntry = new ZipEntry(zipFile);
|
|
|
+ zos.putNextEntry(zipEntry);
|
|
|
+
|
|
|
+ URL url = new URL(encodeFileName(fileUrl));
|
|
|
+
|
|
|
+ HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
|
|
+ // 设置连接主机超时(单位:毫秒)
|
|
|
+ conn.setConnectTimeout(5000);
|
|
|
+ // 设置从主机读取数据超时(单位:毫秒)
|
|
|
+ conn.setReadTimeout(5000);
|
|
|
+
|
|
|
+ if(conn.getResponseCode()==200) {
|
|
|
+ InputStream inputStream = conn.getInputStream();
|
|
|
+
|
|
|
+ byte[] buffs = new byte[1024 * 10];
|
|
|
+
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(inputStream, 1024 * 10);
|
|
|
+
|
|
|
+ int read;
|
|
|
+ while ((read = bis.read(buffs, 0, 1024 * 10)) != -1) {
|
|
|
+ zos.write(buffs, 0, read);
|
|
|
+ }
|
|
|
+
|
|
|
+ bis.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(map.containsKey("localPath")){
|
|
|
+ ZipEntry zipEntry = new ZipEntry(zipFile);
|
|
|
+ zos.putNextEntry(zipEntry);
|
|
|
+
|
|
|
+ String localPath = (String)map.get("localPath");
|
|
|
+ byte[] buffs = new byte[1024 * 10];
|
|
|
+
|
|
|
+ InputStream bis = new BufferedInputStream(new FileInputStream(localPath));
|
|
|
+
|
|
|
+ int read;
|
|
|
+ while ((read = bis.read(buffs, 0, 1024 * 10)) != -1) {
|
|
|
+ zos.write(buffs, 0, read);
|
|
|
+ }
|
|
|
+
|
|
|
+ bis.close();
|
|
|
+ }
|
|
|
+ else if(map.containsKey("fileData")){
|
|
|
+ ZipEntry zipEntry = new ZipEntry(zipFile);
|
|
|
+ zos.putNextEntry(zipEntry);
|
|
|
+
|
|
|
+ byte[] data = (byte[])map.get("fileData");
|
|
|
+
|
|
|
+ zos.write(data, 0, data.length);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch(Exception ex){
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ zos.close();
|
|
|
+ }
|
|
|
+ catch(Exception ex){
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String encodeFileName(String fileUrl) throws Exception{
|
|
|
+ String[] segements = fileUrl.split("\\?");
|
|
|
+
|
|
|
+ String[] arr = segements[0].split("/");
|
|
|
+
|
|
|
+ //文件名可能是中文,用utf-8编码
|
|
|
+ arr[arr.length - 1] = URLEncoder.encode(arr[arr.length - 1],"UTF-8");
|
|
|
+
|
|
|
+ String encFileUrl = Arrays.stream(arr).collect(Collectors.joining("/"));
|
|
|
+
|
|
|
+ if (segements.length>1){
|
|
|
+ encFileUrl += "?" + segements[1];
|
|
|
+ }
|
|
|
+
|
|
|
+ return encFileUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String generatePresignedUrl(OSSConfig ossConfig, String imageUrl,int expireSeconds,int maxSize){
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
|
|
+ Date expiration = new Date(System.currentTimeMillis() + expireSeconds * 1000);
|
|
|
+
|
|
|
+ if (imageUrl.startsWith(ossConfig.getUrlPrefix())) {
|
|
|
+ imageUrl = imageUrl.substring(ossConfig.getUrlPrefix().length());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (imageUrl.indexOf("?") != -1) {
|
|
|
+ imageUrl = imageUrl.substring(0, imageUrl.indexOf("?"));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (imageUrl.startsWith("/")) {
|
|
|
+ imageUrl = imageUrl.substring(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(ossConfig.getBucketName(), imageUrl, HttpMethod.GET);
|
|
|
+
|
|
|
+ // 设置过期时间。
|
|
|
+ request.setExpiration(expiration);
|
|
|
+
|
|
|
+ //设置缩放
|
|
|
+ request.setProcess("image/resize,l_" + maxSize + ",limit_1");
|
|
|
+
|
|
|
+ // 生成签名URL(HTTP GET请求)。
|
|
|
+ URL signedUrl = ossClient.generatePresignedUrl(request);
|
|
|
+
|
|
|
+ return signedUrl.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void presignedDownload(OSSConfig ossConfig, List<Map> fileList, OutputStream output){
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+
|
|
|
+ try{
|
|
|
+ ZipOutputStream zos = new ZipOutputStream(output);
|
|
|
+
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
|
|
+
|
|
|
+ for (Map<String,String> map : fileList) {
|
|
|
+ try {
|
|
|
+ String fileUrl = map.get("fileUrl");
|
|
|
+ String filePath = map.get("filePath");
|
|
|
+
|
|
|
+ Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
|
|
|
+
|
|
|
+ if (fileUrl.startsWith(ossConfig.getUrlPrefix())) {
|
|
|
+ fileUrl = fileUrl.substring(ossConfig.getUrlPrefix().length());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (fileUrl.indexOf("?") != -1) {
|
|
|
+ fileUrl = fileUrl.substring(0, fileUrl.indexOf("?"));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (fileUrl.startsWith("/")) {
|
|
|
+ fileUrl = fileUrl.substring(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(ossConfig.getBucketName(), fileUrl, HttpMethod.GET);
|
|
|
+
|
|
|
+ // 设置过期时间。
|
|
|
+ request.setExpiration(expiration);
|
|
|
+
|
|
|
+ //设置缩放
|
|
|
+ request.setProcess("image/resize,l_1024,limit_1");
|
|
|
+
|
|
|
+ // 生成签名URL(HTTP GET请求)。
|
|
|
+ URL signedUrl = ossClient.generatePresignedUrl(request);
|
|
|
+
|
|
|
+ // 使用签名URL发送请求。
|
|
|
+ OSSObject ossObject = ossClient.getObject(signedUrl, new HashMap<>());
|
|
|
+
|
|
|
+ if (ossObject != null) {
|
|
|
+ InputStream inputStream = ossObject.getObjectContent();
|
|
|
+ byte[] buffs = new byte[1024 * 10];
|
|
|
+ String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
|
|
|
+ String zipFile = fileName;
|
|
|
+
|
|
|
+ if (StringUtils.isNotEmpty(filePath)){
|
|
|
+ zipFile = filePath + fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ ZipEntry zipEntry = new ZipEntry(zipFile);
|
|
|
+ zos.putNextEntry(zipEntry);
|
|
|
+ bis = new BufferedInputStream(inputStream, 1024 * 10);
|
|
|
+
|
|
|
+ int read;
|
|
|
+ while ((read = bis.read(buffs, 0, 1024 * 10)) != -1) {
|
|
|
+ zos.write(buffs, 0, read);
|
|
|
+ }
|
|
|
+
|
|
|
+ ossObject.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch(Exception ex){
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ zos.close();
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ catch(Exception ex){
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ if(bis!=null){
|
|
|
+ try {
|
|
|
+ bis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static String uploadOfferImg(OSSConfig ossConfig,
|
|
|
+ String subFolder, String fileName,
|
|
|
+ String outImgUrl,String offerCode,String personName,
|
|
|
+ String userName,String schoolName) {
|
|
|
+ String retUrl = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ File _file = new File(outImgUrl);
|
|
|
+ Image srcImage = ImageIO.read(_file);
|
|
|
+ int imageWidth = srcImage.getWidth(null);
|
|
|
+ int imageHeight = srcImage.getHeight(null);
|
|
|
+
|
|
|
+ int fontHeight = imageHeight / 20;
|
|
|
+
|
|
|
+ fontHeight = Math.min(30, fontHeight);
|
|
|
+
|
|
|
+ Color color = new Color(128, 128, 128, 200); // 水印颜色
|
|
|
+ Font font = new Font("宋体", Font.ITALIC, fontHeight); //水印字体
|
|
|
+
|
|
|
+ BufferedImage bufferedImage = new BufferedImage(
|
|
|
+ srcImage.getWidth(null), srcImage.getHeight(null),
|
|
|
+ BufferedImage.TYPE_INT_RGB);
|
|
|
+
|
|
|
+ Graphics2D g = bufferedImage.createGraphics();
|
|
|
+
|
|
|
+ g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
|
+ g.drawImage(srcImage.getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH), 0,
|
|
|
+ 0, null);
|
|
|
+
|
|
|
+ g.setColor(color); //水印颜色
|
|
|
+ g.setFont(font); //水印字体
|
|
|
+
|
|
|
+ int markWidth = g.getFontMetrics(g.getFont()).charsWidth(offerCode.toCharArray(), 0, offerCode.length());
|
|
|
+
|
|
|
+ g.rotate(Math.toRadians(-45), imageWidth / 2, imageHeight / 2);
|
|
|
+
|
|
|
+ for (int i=0;i<imageHeight/3;i++) {
|
|
|
+ g.drawString(offerCode, (imageWidth - markWidth) / 2, imageHeight*i/3); //水印位置
|
|
|
+ }
|
|
|
+
|
|
|
+ g.dispose(); //释放资源
|
|
|
+
|
|
|
+ ByteArrayOutputStream outImgStream = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(bufferedImage, "jpg", outImgStream);
|
|
|
+
|
|
|
+ byte[] outImg = outImgStream.toByteArray();
|
|
|
+
|
|
|
+ ByteArrayInputStream inputImgStream = new ByteArrayInputStream(outImg);
|
|
|
+
|
|
|
+ retUrl = upload(ossConfig,subFolder,fileName,inputImgStream);
|
|
|
+ }
|
|
|
+ catch (Exception ex){
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ }
|
|
|
+
|
|
|
+ return retUrl;
|
|
|
+ }
|
|
|
+}
|