package com.hx.util; import com.aliyun.oss.OSSClient; import com.aliyun.oss.model.OSSObject; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; public class OSSUtil { private static OSSClient ossClient; public static OSSClient getOSSClient(String keyId, String keySecret, String endPoint) { if(ossClient == null) { ossClient = new OSSClient(endPoint, keyId, keySecret); } return ossClient; } /** 后端调用上传图片 */ public static String uploadImg(String fileName, File imgFile, String keyId, String keySecret, String endPoint, String bucket) throws IOException { // // 压缩到文件夹 // Thumbnails.of(imgFile).scale(1f).outputQuality(0.8f).toFile(SimpleToof.getRealpath("/upload/image/") // + imgName); // File file = new File(SimpleToof.getRealpath("/upload/image/") + imgName); // 创建OSSClient实例。 OSSClient ossClient = new OSSClient("https://" + endPoint, keyId, keySecret); // 上传文件。由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。 // 判断压缩后的大小 // if (file.length() > imgFile.length()) { ossClient.putObject(bucket, fileName, imgFile); // } else { // ossClient.putObject(BUCKET, fileName, file); // } // 关闭OSSClient。 ossClient.shutdown(); return "https://" + bucket + "." + endPoint + "/" + fileName; } /** 后端调用上传图片 */ public static String uploadImg(String fileName, MultipartFile imgFile, String keyId, String keySecret, String endPoint, String bucket) { try { // 创建OSSClient实例。 OSSClient ossClient = new OSSClient("https://" + endPoint, keyId, keySecret); ossClient.putObject(bucket, fileName, imgFile.getInputStream()); ossClient.shutdown(); return "https://" + bucket + "." + endPoint + "/" + fileName; }catch (Exception e) { e.printStackTrace(); return null; } } /** 后端调用上传图片 */ public static String uploadImg(String fileName, InputStream is, String keyId, String keySecret, String endPoint, String bucket) { try { // 创建OSSClient实例。 OSSClient ossClient = new OSSClient("https://" + endPoint, keyId, keySecret); ossClient.putObject(bucket, fileName, is); ossClient.shutdown(); return "https://" + bucket + "." + endPoint + "/" + fileName; }catch (Exception e) { e.printStackTrace(); return null; } } public static byte[] getObject(String key, String keyId, String keySecret, String endPoint, String bucket) { byte[] resultByte = null; InputStream is = null; ByteArrayOutputStream swapStream = null; try { OSSObject ossObject = getOSSClient(keyId, keySecret, endPoint).getObject(bucket, key); is = ossObject.getObjectContent(); swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; //buff用于存放循环读取的临时数据 int rc = 0; while ((rc = is.read(buff, 0, 1024)) > 0) { swapStream.write(buff, 0, rc); } resultByte = swapStream.toByteArray(); //in_b为转换之后的结果 } catch (Exception e) { e.printStackTrace(); } finally { if(is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } is = null; } if(swapStream != null) { try { swapStream.close(); } catch (IOException e) { e.printStackTrace(); } swapStream = null; } } return resultByte; } }