前天做oss服务器文件上传,之前没了解过,做的过程中遇到许多问题,最终通过查阅相关资料解决了,特分享一下。
首先准备工作 必要的jar
这里注意版本一致,不然就会报很多坑爹的错误。。
如果你断点调试在执行putObject方法时报错,99%的是jar包有问题。。
然后是配置文件
现在开通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() {
-
return endpoint;
-
}
-
-
public void setEndpoint(String endpoint) {
-
this.endpoint = endpoint;
-
}
-
-
public String getAccessKeyId() {
-
return accessKeyId;
-
}
-
-
public void setAccessKeyId(String accessKeyId) {
-
this.accessKeyId = accessKeyId;
-
}
-
-
public String getAccessKeySecret() {
-
return accessKeySecret;
-
}
-
-
public void setAccessKeySecret(String accessKeySecret) {
-
this.accessKeySecret = accessKeySecret;
-
}
-
-
public String getBucketName() {
-
return bucketName;
-
}
-
-
public void setBucketName(String bucketName) {
-
this.bucketName = bucketName;
-
}
-
-
public String getAccessUrl() {
-
return accessUrl;
-
}
-
-
public void setAccessUrl(String accessUrl) {
-
this.accessUrl = accessUrl;
-
}
-
-
}
然后就是创建一个OSS文件管理的OSSManageUtil工具类。oss文件存储实际上就是对Object的操作,只要写好路径,都会自动创建的,OSSClient是oss的核心,有兴趣的可以多研究下。我做这个 是要上传app,以及压缩图片后上传,contentType这个方法注意,我上传的是apk 所以要用这个类型application/octet-stream,之前因为这个找了半天错。
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileNotFoundException;
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.util.Date;
-
-
import org.apache.commons.fileupload.disk.DiskFileItem;
-
import org.springframework.web.multipart.MultipartFile;
-
import org.springframework.web.multipart.commons.CommonsMultipartFile;
-
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ll666634/article/details/79069312
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
-
发表于 2020-03-07 23:01:01
- 阅读 ( 1553 )
- 分类:职场