发表于: 2018-03-21 23:14:54
1 620
今天主要学习了一波阿里云的API,说真的,看的我头昏脑胀。
首先是关于依赖的建立:
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.9</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1</version>
</dependency>
其中aliyun-sdk-oss无法通过Maven的中央仓库进行下载,只能通过maven的命令行进行安装。
配置好依赖以后,大致运行了一下阿里云提供的demo:
着手从阿里云的MAIN方法中提取出工具类:
// 把字符串存入OSS,Object的名称为firstKey。
public void putZF(String filename, String content) {
InputStream is = new ByteArrayInputStream(content.getBytes());
ossClient.putObject(bucketName, filename, is);
ossClient.shutdown();
}
效果如下:
//文件流形式上传文件
public void putFile(String filename) throws IOException {
InputStream inputStreama = new FileInputStream(filename);
ossClient.putObject(bucketName, filename, inputStreama);
// 关闭client
ossClient.shutdown();
}
测试类:
测试结果:
//文件流形式下载文件
public void downFile(String filename) throws IOException {
ossClient.getObject(bucketName, filename);
// 关闭client
ossClient.getObject(new GetObjectRequest(bucketName, filename), new File(filename));
ossClient.shutdown();
}
测试类:
测试结果:
但是上面就有一个问题,那就是下载或者上传的文件,都是在项目的根目录下的。
明天计划的事情:
大致测试一下文件上传和下载的方法,然后搭建任务七的项目,开始做图片迁移。
遇到的问题:
1.下午的时候,在main方法中测试了一下关于上传文件,但是开始为了方便后面的程序继续运行,就没在上传完成后关闭输出流。导致后面的方法加载了很久也没有运行。
2.从MIAN方法提取工具类。
收获:
进度:
评论