发表于: 2019-05-27 23:18:02

1 611


添加jar包

<dependency>
 <groupId>com.aliyun.oss</groupId>
 <artifactId>aliyun-sdk-oss</artifactId>
 <version>2.8.3</version>
</dependency>

测试一下图片是否成功添加

public class AliyunOss {
private static org.apache.log4j.Logger logger = Logger.getLogger(AliyunOss.class);

   public static void main(String[] args) throws IOException {
String endpoint = "oss-cn-shenzhen.aliyuncs.com";
        String accessKeyId = "LTAIPTqJ3SRkUlmT";
        String accessKeySecret = "wdyAH2adTy5miKxc4K21BcKQr9q9N5";

       OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

       // 上传文件。<yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt
       ossClient.putObject("pipiretrak", "new", new File("C:/Users/Shinelon/Desktop/image/74644894_p0_master1200.jpg"));

// 关闭OSSClient
       ossClient.shutdown();
   }
}

成功

封装成工具类

public class AliyunOss {
private static Logger logger = Logger.getLogger(AliyunOss.class);

   public String endpoint;
   public String accessKeyId;
   public String accessKeySecret;
   public String bucketName;

   public AliyunOss(String endpoint, String accessKeyId, String accessKeySecret, String bucketName) {
this.endpoint = endpoint;
       this.accessKeyId = accessKeyId;
       this.accessKeySecret = accessKeySecret;
       this.bucketName = bucketName;
   }

public boolean uploadImage(MultipartFile file) throws IOException {

// 创建OSSClient实例。
       OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

       String key = System.currentTimeMillis() + file.getOriginalFilename();
// 上传文件。<yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt

       ossClient.putObject(bucketName, key, new ByteArrayInputStream(file.getBytes()));
// 关闭OSSClient
       ossClient.shutdown();
       return true;
   }
}

配置阿里云的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
   <bean id="Ali" class="com.jnshu.pojo.tool.AliyunOss">
       <constructor-arg name="endpoint" value="oss-cn-shenzhen.aliyuncs.com"></constructor-arg>
       <constructor-arg name="accessKeyId" value="LTAIPTqJ3SRkUlmT"></constructor-arg>
       <constructor-arg name="accessKeySecret" value="wdyAH2adTy5miKxc4K21BcKQr9q9N5"></constructor-arg>
       <constructor-arg name="bucketName" value="pipiretrak"></constructor-arg>
   </bean>
</beans>

明天:完成图片的转移


收获:对于阿里云对象存储使用缕出了大概步骤


返回列表 返回列表
评论

    分享到