| | |
| | | import com.aliyun.oss.OSS; |
| | | import com.aliyun.oss.OSSClientBuilder; |
| | | import com.aliyun.oss.OSSException; |
| | | import com.aliyun.oss.model.ObjectMetadata; |
| | | import com.aliyun.oss.model.PutObjectResult; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.InputStream; |
| | | import java.util.Date; |
| | | import java.util.UUID; |
| | | |
| | | @Component |
| | |
| | | AliyunUtils.bucketName = bucketName; |
| | | } |
| | | |
| | | public static String upload(InputStream inputStream, String orginalFileName){ |
| | | // public static String upload(InputStream inputStream, String orginalFileName){ |
| | | // // 创建OSSClient实例。 |
| | | // OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret); |
| | | // // 文件名 |
| | | // String fileName = UUID.randomUUID().toString(); |
| | | // //文件扩展名 |
| | | // String fileExtention = orginalFileName.substring(orginalFileName.lastIndexOf(".")); |
| | | // //最终的路径 类似avatar/2021/12/05/xxxxxxxxx.jpg |
| | | // String objectName = fileName+fileExtention; |
| | | // ossClient.putObject(bucketName, objectName, inputStream); |
| | | // // 关闭OSSClient。 |
| | | // ossClient.shutdown(); |
| | | // return "https://"+bucketName+"."+endpoint+"/"+objectName; |
| | | // } |
| | | |
| | | public String upload(MultipartFile file) { |
| | | |
| | | String accessKeyId = keyId; |
| | | String accessKeySecret = keySecret; |
| | | |
| | | // 创建OSSClient实例。 |
| | | OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret); |
| | | // 文件名 |
| | | String fileName = UUID.randomUUID().toString(); |
| | | //文件扩展名 |
| | | String fileExtention = orginalFileName.substring(orginalFileName.lastIndexOf(".")); |
| | | //最终的路径 类似avatar/2021/12/05/xxxxxxxxx.jpg |
| | | String objectName = fileName+fileExtention; |
| | | ossClient.putObject(bucketName, objectName, inputStream); |
| | | // 关闭OSSClient。 |
| | | ossClient.shutdown(); |
| | | return "https://"+bucketName+"."+endpoint+"/"+objectName; |
| | | OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| | | // 上传文件流 |
| | | try { |
| | | // 获取文件的名称 |
| | | String fileName = file.getOriginalFilename(); |
| | | ObjectMetadata objectMetadata = new ObjectMetadata(); |
| | | objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf(".")))); |
| | | // 调用oss的方法实现长传 |
| | | // 第一个参数 bucketName |
| | | // 第二个参数 上传到oss的文件路径和文件名称 |
| | | ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(file.getBytes()),objectMetadata); |
| | | // 关闭OSSClient。 |
| | | ossClient.shutdown(); |
| | | // 把上传的文件路径返回 (手动拼接) |
| | | // 这里设置图片有效时间 我设置了30年 |
| | | Date expiration = new Date(System.currentTimeMillis() + 946080000 * 1000); |
| | | String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); |
| | | return url; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public static void delete(String link){ |
| | |
| | | } |
| | | |
| | | |
| | | // 实现图片的预览功能 |
| | | public static String getcontentType(String FilenameExtension) { |
| | | if (FilenameExtension.equalsIgnoreCase(".bmp")) { |
| | | return "image/bmp"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".gif")) { |
| | | return "image/gif"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".jpeg") || |
| | | FilenameExtension.equalsIgnoreCase(".jpg") || |
| | | FilenameExtension.equalsIgnoreCase(".png")) { |
| | | return "image/jpg"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".html")) { |
| | | return "text/html"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".txt")) { |
| | | return "text/plain"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".vsd")) { |
| | | return "application/vnd.visio"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".pptx") || |
| | | FilenameExtension.equalsIgnoreCase(".ppt")) { |
| | | return "application/vnd.ms-powerpoint"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".docx") || |
| | | FilenameExtension.equalsIgnoreCase(".doc")) { |
| | | return "application/msword"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".xml")) { |
| | | return "text/xml"; |
| | | } |
| | | return "image/jpg"; |
| | | } |
| | | |
| | | |
| | | |
| | | } |