发表于: 2018-04-01 23:19:32
4 471
今日完成:
1. 使用阿里云oos
在pom中添加依赖
<!--aliyun-oss-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.1.0</version>
</dependency>
编写工具类
public class AliyumOSSUtil {
private Logger logger = Logger.getLogger(AliyumOSSUtil.class);
private OSSClient ossClient = null;
public void setOssClient(OSSClient ossClient) {
this.ossClient = ossClient;
}
private String bucketName = null;
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
public boolean upload(String key, File file) {
try {
this.createBucket();
/*
* Upload an object to your bucket
*/
logger.info("Uploading an object to your bucket");
ossClient.putObject(bucketName, key, file);
return true;
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorCode());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
return false;
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
return false;
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
ossClient.shutdown();
}
}
public Object download(String key) {
try {
return ossClient.getObject(bucketName, key);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorCode());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
return null;
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
return null;
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
ossClient.shutdown();
}
}
public void createBucket() {
/*
* Determine whether the bucket exists
*/
if (!ossClient.doesBucketExist(bucketName)) {
/*
* Create a new OSS bucket
*/
logger.info("Creating bucket " + bucketName + "\n");
ossClient.createBucket(bucketName);
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
ossClient.createBucket(createBucketRequest);
logger.info("Created bucket " + bucketName + "\n");
}
}
}
添加到spring配置文件中,配置参数:
<!--aliyum-oss-->
<bean id="ossClient" class="com.aliyun.oss.OSSClient">
<constructor-arg name="endpoint" value="oss-cn-beijing.aliyuncs.com"/>
<constructor-arg name="accessKeyId" value="LTAIGLQ"/>
<constructor-arg name="secretAccessKey" value="c4exTBASfYJAR"/>
</bean>
<bean id="aliyumOSSUtil" class="com.wyq.taskSeven.util.AliyumOSSUtil">
<property name="ossClient" ref="ossClient"/>
<property name="bucketName" value="taskseven"/>
</bean>
明日计划:
1. 完成任务7,开始任务8
遇到的问题:
1. Failed to instantiate [com.aliyun.oss.OSSClient]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/http/ssl/TrustStrategy
jar包冲突
<!--sendCloud-->
<dependency>
<groupId>com.sendcloud</groupId>
<artifactId>sdk</artifactId>
<version>1.1</version>
<exclusions>
<exclusion>
<artifactId>httpcore</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
</exclusions>
</dependency>
2. Caught an ClientException, which means the client encountered a serious internal problem while trying to communicate with OSS, such as not being able to access the network.
配置的问题,明天解决
收获:
1. 使用邮箱验证码注册登录
2. 配置阿里云OSS
评论