package com.szxgl.pars2021.utils; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.common.comm.ResponseMessage; import com.aliyun.oss.model.PutObjectResult; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; public class UploadFileToOssUtil { /** * * @param endpoint 地域节点 * @param accessKeyId ak id * @param accessKeySecret ak密码 * @param bucketName Bucket 名字 * @param bucketDomain Bucket 域名 * @return */ private static final String endpoint = "oss-cn-hangzhou.aliyuncs.com"; private static final String accessKeyId = "LTAI4G88mJUn3YzWo2eFmxVG"; private static final String accessKeySecret = "iWNQskqvXC7Rm0au2W2u3AGNFEgYMl"; private static final String bucketName = "h5buckets"; private static final String bucketDomain = "h5buckets.oss-cn-hangzhou.aliyuncs.com"; /** * oss完成文件上传 * @param inputStream * @param originalName * @return */ public static R uploadFileToOss(InputStream inputStream, String originalName) { // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 生成上传文件的目录 String folderName = new SimpleDateFormat("yyyyMMdd").format(new Date()); // 生成上传文件在OSS服务器上保存时的文件名 // 原始文件名:beautfulgirl.jpg // 生成文件名:wer234234efwer235346457dfswet346235.jpg // 使用UUID生成文件主体名称 String fileMainName = UUID.randomUUID().toString().replace("-", ""); // 从原始文件名中获取文件扩展名 String extensionName = originalName.substring(originalName.lastIndexOf(".")); // 使用目录、文件主体名称、文件扩展名称拼接得到对象名称 (转码的输入路径) //String objectName = "h5-in/"+folderName + "/" + fileMainName + extensionName; //使用目录、文件主体名称、文件扩展名称拼接得到对象名称 (普通输入路径) String objectName = "h5-upload/"+folderName + "/" + fileMainName + extensionName; System.out.println(objectName); try { // 调用OSS客户端对象的方法上传文件并获取响应结果数据 PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, inputStream); // 从响应结果中获取具体响应消息 ResponseMessage responseMessage = putObjectResult.getResponse(); // 根据响应状态码判断请求是否成功 if (responseMessage == null) { // 拼接获取刚上传到oss的文件的路径 // oss的访问地址h5buckets.oss-cn-hangzhou.aliyuncs.com/h5-in/20211110/d0f7779da2c64fc481101ab029deb419.mp4 //String ossFileAccessPath = bucketDomain + "/" + objectName; // oss的访问地址/h5-in/20211110/d0f7779da2c64fc481101ab029deb419.mp4 String ossFileAccessPath = "/" + objectName; System.out.println(ossFileAccessPath); // 当前方法返回成功 //return R.ok(ossFileAccessPath); return R.ok(objectName); } else { // 获取响应状态码 int statusCode = responseMessage.getStatusCode(); // 如果请求没有成功,获取错误消息 String errorMessage = responseMessage.getErrorResponseAsString(); // 当前方法返回失败 return R.error("当前响应状态码=" + statusCode + " 错误消息=" + errorMessage); } } catch (Exception e) { e.printStackTrace(); // 当前方法返回失败 return R.error(e.getMessage()); } finally { if (ossClient != null) { // 关闭O SSClient。 ossClient.shutdown(); } } } /*public static void main(String[] args) throws Exception { File file = new File("D:\\WeChat_20211110092809.mp4"); String canonicalPath = file.getCanonicalPath(); System.out.println(canonicalPath); String name = file.getName(); System.out.println(name); R r = UploadFileToOssUtil.uploadFileToOss(canonicalPath, name); System.out.println(r.get("msg")); String msg = r.get("msg").toString(); }*/ }