阿里云oss服务器文件上传JAVA - Go语言中文社区

阿里云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() {    
  52.         return endpoint;    
  53.     }    
  54.     
  55.     public void setEndpoint(String endpoint) {    
  56.         this.endpoint = endpoint;    
  57.     }    
  58.     
  59.     public String getAccessKeyId() {    
  60.         return accessKeyId;    
  61.     }    
  62.     
  63.     public void setAccessKeyId(String accessKeyId) {    
  64.         this.accessKeyId = accessKeyId;    
  65.     }    
  66.     
  67.     public String getAccessKeySecret() {    
  68.         return accessKeySecret;    
  69.     }    
  70.     
  71.     public void setAccessKeySecret(String accessKeySecret) {    
  72.         this.accessKeySecret = accessKeySecret;    
  73.     }    
  74.     
  75.     public String getBucketName() {    
  76.         return bucketName;    
  77.     }    
  78.     
  79.     public void setBucketName(String bucketName) {    
  80.         this.bucketName = bucketName;    
  81.     }    
  82.     
  83.     public String getAccessUrl() {    
  84.         return accessUrl;    
  85.     }    
  86.     
  87.     public void setAccessUrl(String accessUrl) {    
  88.         this.accessUrl = accessUrl;    
  89.     }    
  90.     
  91. }    


然后就是创建一个OSS文件管理的OSSManageUtil工具类。oss文件存储实际上就是对Object的操作,只要写好路径,都会自动创建的,OSSClient是oss的核心,有兴趣的可以多研究下。我做这个 是要上传app,以及压缩图片后上传,contentType这个方法注意,我上传的是apk  所以要用这个类型application/octet-stream,之前因为这个找了半天错。

[java] view plain copy
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.util.Date;  
  7.   
  8. import org.apache.commons.fileupload.disk.DiskFileItem;  
  9. import org.springframework.web.multipart.MultipartFile;  
  10. import org.springframework.web.multipart.commons.CommonsMultipartFile;  
  11. 版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
    原文链接:https://blog.csdn.net/ll666634/article/details/79069312
    站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 23:01:01
  • 阅读 ( 1553 )
  • 分类:职场

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢