| | |
| | | package com.ycl.service.oss.impl; |
| | | |
| | | import com.aliyun.oss.ClientBuilderConfiguration; |
| | | import com.aliyun.oss.OSS; |
| | | import com.aliyun.oss.OSSClientBuilder; |
| | | import com.aliyun.oss.common.comm.Protocol; |
| | | import com.aliyun.oss.model.ObjectMetadata; |
| | | import com.ycl.service.oss.OssService; |
| | | import com.ycl.utils.ConstantPropertiesUtils; |
| | | import com.ycl.utils.common.RandomUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.InputStream; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.Date; |
| | | import java.util.UUID; |
| | | |
| | | @Service |
| | | public class OssServiceImpl implements OssService { |
| | | @Override |
| | | public String uploadImages(MultipartFile file) { |
| | | if (file == null) { |
| | | return "上传文件为空"; |
| | | } |
| | | String endpoint = ConstantPropertiesUtils.END_POINT; |
| | | String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID; |
| | | String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET; |
| | |
| | | // 上传文件流 |
| | | try { |
| | | // 获取文件的名称 |
| | | String fileName = file.getOriginalFilename(); |
| | | String fileName = "sczf/" + 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.putObject(bucketName, fileName, new ByteArrayInputStream(file.getBytes()), objectMetadata); |
| | | // 关闭OSSClient。 |
| | | ossClient.shutdown(); |
| | | // 把上传的文件路径返回 (手动拼接) |
| | | // 这里设置图片有效时间 我设置了30年 |
| | | Date expiration = new Date(System.currentTimeMillis() + 946080000 * 1000); |
| | | Date expiration = new Date(System.currentTimeMillis() + (long) 946080000 * 1000); |
| | | String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); |
| | | |
| | | return url; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | System.out.println("uploadImages上传图片失败:"); |
| | | // e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | private byte[] readInputStream(InputStream inStream) throws Exception { |
| | | ByteArrayOutputStream outStream = new ByteArrayOutputStream(); |
| | | //创建一个Buffer字符串 |
| | | byte[] buffer = new byte[1024]; |
| | | //每次读取的字符串长度,如果为-1,代表全部读取完毕 |
| | | int len = 0; |
| | | //使用一个输入流从buffer里把数据读取出来 |
| | | while ((len = inStream.read(buffer)) != -1) { |
| | | //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 |
| | | outStream.write(buffer, 0, len); |
| | | } |
| | | //关闭输入流 |
| | | inStream.close(); |
| | | //把outStream里的数据写入内存 |
| | | return outStream.toByteArray(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public String uploadImages(InputStream inputStream, String ext) { |
| | | String endpoint = ConstantPropertiesUtils.END_POINT; |
| | | String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID; |
| | | String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET; |
| | | String bucketName = ConstantPropertiesUtils.BUCKET_NAME; |
| | | |
| | | // 创建OSSClient实例。 |
| | | OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| | | // 上传文件流 |
| | | try { |
| | | // 获取文件的名称 |
| | | LocalDate date = LocalDate.now(); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| | | String fileName = "sczf/" + date.format(formatter) + RandomUtils.generateRandomInt(8) + "." + ext; |
| | | System.out.println("文件地址:" + fileName); |
| | | ObjectMetadata objectMetadata = new ObjectMetadata(); |
| | | objectMetadata.setContentType(getcontentType(ext)); |
| | | // 调用oss的方法实现长传 |
| | | // 第一个参数 bucketName |
| | | // 第二个参数 上传到oss的文件路径和文件名称 |
| | | ossClient.putObject(bucketName, fileName, inputStream, objectMetadata); |
| | | // 关闭OSSClient。 |
| | | ossClient.shutdown(); |
| | | // 把上传的文件路径返回 (手动拼接) |
| | | // 这里设置图片有效时间 我设置了30年 |
| | | Date expiration = new Date(System.currentTimeMillis() + (long) 946080000 * 1000); |
| | | String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); |
| | | System.out.println("oss上传成功:" + url); |
| | | |
| | | // URL urlO = new URL(url); |
| | | // HttpURLConnection conn = (HttpURLConnection) urlO.openConnection(); |
| | | // conn.setRequestMethod("GET"); |
| | | // conn.setConnectTimeout(10 * 1000); |
| | | // conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); |
| | | // |
| | | // InputStream inputStream2 = conn.getInputStream();// 通过输入流获取图片数据 |
| | | // |
| | | // byte[] data = readInputStream(inputStream2); |
| | | // |
| | | // System.out.println("获取图片成功:" + url); |
| | | return url; |
| | | } catch (Exception e) { |
| | | System.out.println("oss异常:" + e.getMessage()); |
| | | // e.printStackTrace(); |
| | | return this.uploadImages(inputStream, ext); |
| | | } |
| | | } |
| | | |
| | | public static String getcontentType(String FilenameExtension) { |
| | | if (FilenameExtension.equalsIgnoreCase(".mp4")||FilenameExtension.equalsIgnoreCase(".mov")) { |
| | | return "video/mp4"; |
| | | } |
| | | if (FilenameExtension.equalsIgnoreCase(".bmp")) { |
| | | return "image/bmp"; |
| | | } |