前天做oss服务器文件上传,之前没了解过,做的过程中遇到许多问题,最终通过查阅相关资料解决了,特分享一下。
首先准备工作 必要的jar
![](https://img-blog.csdn.net/20170506102517507?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamF2YV9vc3M=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
这里注意版本一致,不然就会报很多坑爹的错误。。
如果你断点调试在执行putObject方法时报错,99%的是jar包有问题。。
然后是配置文件
![](https://img-blog.csdn.net/20170506102832559?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamF2YV9vc3M=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
现在开通oss,都是免费开通的,包年9块钱。
这是几个必要的参数,既然做这个肯定 要知道的!这里就不多做解释了。
这里,首先需要我们创建一个OSS信息实体类,OSSConfigure.Java,用来读取配置文件的信息,封装成实体。
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
-
-
-
-
-
- public class OSSConfigure {
-
- private String endpoint;
- private String accessKeyId;
- private String accessKeySecret;
- private String bucketName;
- private String accessUrl;
-
- public OSSConfigure() {
-
- }
-
-
-
-
-
-
-
- public OSSConfigure(String storageConfName) throws IOException {
-
- Properties prop = new Properties();
- InputStream is= super.getClass().getClassLoader().getResourceAsStream(storageConfName);
- prop.load(is);
-
- endpoint = prop.getProperty("Endpoint").trim();
- accessKeyId = prop.getProperty("AccessKey").trim();
- accessKeySecret = prop.getProperty("AccessKeySecret").trim();
- bucketName = prop.getProperty("BucketName").trim();
- accessUrl = prop.getProperty("accessUrl").trim();
-
- }
-
- public OSSConfigure(String endpoint, String accessKeyId,
- String accessKeySecret, String bucketName, String accessUrl) {
-
- this.endpoint = endpoint;
- this.accessKeyId = accessKeyId;
- this.accessKeySecret = accessKeySecret;
- this.bucketName = bucketName;
- this.accessUrl = accessUrl;
- }
-
- public String getEndpoint() {
-