阿里云oss服务器文件上传JAVA

前天做oss服务器文件上传,之前没了解过,做的过程中遇到许多问题,最终通过查阅相关资料解决了,特分享一下。

首先准备工作 必要的jar 


这里注意版本一致,不然就会报很多坑爹的错误。。

如果你断点调试在执行putObject方法时报错,99%的是jar包有问题。。

然后是配置文件


现在开通oss,都是免费开通的,包年9块钱。

这是几个必要的参数,既然做这个肯定 要知道的!这里就不多做解释了。

这里,首先需要我们创建一个OSS信息实体类,OSSConfigure.Java,用来读取配置文件的信息,封装成实体。

[java]  view plain   copy
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.util.Properties;    
  4.   /** 
  5.    * oss相关参数实体 
  6.    * @author liux 
  7.    *2017/5/5 
  8.    */  
  9. public class OSSConfigure {    
  10.     
  11.     private String endpoint;    
  12.     private String accessKeyId;    
  13.     private String accessKeySecret;    
  14.     private String bucketName;    
  15.     private String accessUrl;    
  16.     
  17.     public OSSConfigure() {    
  18.     
  19.     }    
  20.     
  21.     /**  
  22.      * 通过配置文件.properties文件获取,这几项内容。  
  23.      *   
  24.      * @param storageConfName  
  25.      * @throws IOException  
  26.      */    
  27.     public OSSConfigure(String storageConfName) throws IOException {    
  28.     
  29.         Properties prop = new Properties();   
  30.         InputStream is= super.getClass().getClassLoader().getResourceAsStream(storageConfName);  
  31.         prop.load(is);    
  32.     
  33.         endpoint = prop.getProperty("Endpoint").trim();    
  34.         accessKeyId = prop.getProperty("AccessKey").trim();    
  35.         accessKeySecret = prop.getProperty("AccessKeySecret").trim();    
  36.         bucketName = prop.getProperty("BucketName").trim();    
  37.         accessUrl = prop.getProperty("accessUrl").trim();    
  38.     
  39.     }    
  40.     
  41.     public OSSConfigure(String endpoint, String accessKeyId,    
  42.             String accessKeySecret, String bucketName, String accessUrl) {    
  43.     
  44.         this.endpoint = endpoint;    
  45.         this.accessKeyId = accessKeyId;    
  46.         this.accessKeySecret = accessKeySecret;    
  47.         this.bucketName = bucketName;    
  48.         this.accessUrl = accessUrl;    
  49.     }    
  50.     
  51.     public String getEndpoint() {    
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值