阿里云OSS存储(上传文件)
把文件上传创建的文件夹中,直接返回的地址是可以直接下载的,可以根据需要去设置,文件是否公开
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.8.0</version>
</dependency>
import java.net.URL;
import java.util.Date;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
public class UploadOSS {
/**
* OSS上传文件
*
* @throws Exception
*/
//上传的文件 //文件路径和文件名
public static String uploadFile(File localFile, String filePath) throws Exception {
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建。
String accessKeyId = "accessKeyId ";
String accessKeySecret = "accessKeySecret";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上传文件流。
InputStream inputStream = new FileInputStream(localFile);
ossClient.putObject("文件夹名称", filePath, inputStream);
//Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
// 生成URL
//URL url = ossClient.generatePresignedUrl("文件夹名称", filePath, expiration);
// 关闭OSSClient。
ossClient.shutdown();
return "你的文件夹http地址"+filePath;
//return url.toString();
}
}