首页 技术 正文
技术 2022年11月13日
0 收藏 926 点赞 2,649 浏览 1791 个字

描述:oss比较方便,省去了自己搭建文件服务器的时间,价格比较便宜,下面是java基于oss的简单上传代码

a、添加maven依赖

<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.1.0</version>
</dependency>

b、java代码

public class TestOSSUpload {private static String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
private static String accessKeyId = "你的accessKeyId ";
private static String accessKeySecret = "你的accessKeySecret";
private static String bucketName = "你的bucket";public void putObject(String bucketName, String key, String filePath) throws FileNotFoundException { // 初始化OSSClient
OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret); // 获取指定文件的输入流
File file = new File(filePath);
InputStream content = new FileInputStream(file); // 创建上传Object的Metadata
ObjectMetadata meta = new ObjectMetadata(); // 必须设置ContentLength
meta.setContentLength(file.length()); Date expire = new Date(new Date().getTime() + 30 * 1000);
meta.setExpirationTime(expire); // 上传Object. PutObjectResult result = client.putObject(bucketName, key, content, meta);
// 打印ETag System.out.println("etag--------------->"+result.getETag());
}public static void main(String[] args) throws FileNotFoundException {
TestOSSUpload testOSSUpload = new TestOSSUpload();testOSSUpload.putObject(bucketName, "temp3.xlsx", "D:\\temp.xlsx");
File file = new File("D:\\temp.xlsx");
String md5 = testOSSUpload.getFileMD5(file);System.out.println("md5---------------->"+md5);
}public static String getFileMD5(File file) {
if (!file.isFile()){
return null;
}
MessageDigest digest = null;
FileInputStream in=null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16).toUpperCase();
}}

致此结束……  

阿里云oss,简单上传

关注我的公众号,精彩内容不能错过

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,145
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,617
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,461
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,236
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,871
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,952