|
|
@@ -0,0 +1,117 @@
|
|
|
+package cn.seecoder.fdroidrepository.utils;
|
|
|
+
|
|
|
+import cn.seecoder.fdroidrepository.config.OssConfig;
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.model.*;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class AliyunOssUtil {
|
|
|
+
|
|
|
+ private Logger logger= LoggerFactory.getLogger(AliyunOssUtil.class);
|
|
|
+ @Autowired
|
|
|
+ private OssConfig config;
|
|
|
+
|
|
|
+
|
|
|
+ public String upload(File file) {
|
|
|
+ if (file == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String endPoint = config.getEndPoint();
|
|
|
+ String keyId = config.getAccessKeyId();
|
|
|
+ String keySecret = config.getAccessKeySecret();
|
|
|
+ String bucketName = config.getBucketName();
|
|
|
+ String fileHost = config.getFileHost();
|
|
|
+
|
|
|
+ //定义子文件的格式
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String dateStr = format.format(new Date());
|
|
|
+ //阿里云文件上传客户端
|
|
|
+ OSSClient client = new OSSClient(endPoint, keyId, keySecret);
|
|
|
+ try {
|
|
|
+ //判断桶是否存在
|
|
|
+ if (!client.doesBucketExist(bucketName)) {
|
|
|
+ //创建桶
|
|
|
+ client.createBucket(bucketName);
|
|
|
+ CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
|
|
|
+ //设置访问权限为公共读
|
|
|
+ createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
|
|
|
+ //发起创建桶的请求
|
|
|
+ client.createBucket(createBucketRequest);
|
|
|
+ }
|
|
|
+ //当桶存在时,进行文件上传
|
|
|
+ //设置文件路径和名称
|
|
|
+ String fileUrl = fileHost + "/" + (dateStr + "/" + UUID.randomUUID().toString().replace("-", "") + "-" + file.getName());
|
|
|
+ ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
+ objectMetadata.setContentType(getcontentType(file.getName().substring(file.getName().lastIndexOf("."))));
|
|
|
+ objectMetadata.setContentDisposition("inline; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
|
|
|
+ PutObjectResult result = client.putObject(new PutObjectRequest(bucketName, fileUrl, file,objectMetadata));
|
|
|
+ client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
|
|
|
+ //文件上传成功后,返回当前文件的路径
|
|
|
+ if (result != null) {
|
|
|
+ return "http://" + bucketName + "." + endPoint.substring(7) + "/" + fileUrl;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (client != null) {
|
|
|
+ client.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|