发表于: 2018-03-21 23:14:54

1 621


今天完成的事情:



今天主要学习了一波阿里云的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方法中测试了一下关于上传文件,但是开始为了方便后面的程序继续运行,就没在上传完成后关闭输出流。导致后面的方法加载了很久也没有运行。

然后中断程序,重新开始运行也一样,程序卡在开始的地方。
后来吃饭的时候,大致查了一下,说是IO流到后面会被虚拟机自动回收,但是在IO流没有被关闭之前,只会导致应为等待而堵塞。
幸好的是吃饭回来,大概时间过长了,重新修改了程序,再没有出现这种情况。



2.从MIAN方法提取工具类。

一开始想着向庆东师兄做的XMamcache的工具类一样,但是研究了一波,并不能成功,只好放弃。
而且在提取工具类的时候,并没有意识到可以提取出一个方法,就用测试类测试一下方法,感觉自己对于程序的运用还停留在一个比较浅显的地方,并没有主动去使用各种技术的意识。



收获:

1.自己在程序的理解和运用还有很多不足。
2.开始学会怎么去编写自己的工具类。



进度:

任务开始时间:2018年03月18日
预计demo时间:2018年03月24日
延期:不知道图片迁移要怎么做,感觉要多花点时间。已经注册了七牛云,等待系统审核通过。



返回列表 返回列表
评论

    分享到