3.2 文件上传 - java版SDK
- 1.引入依赖
```xmlcom.easytec.oss sdk 1.3.3-SNAPSHOT
- 2.使用
> 初始化运行环境
```java
/**指定文件服务器地址,domain参考【3.1】**/
//domain: efs文件服务部署地址
OssClient ossClient = new OssClient("domain");
/**默认环境 -- 废弃,不建议使用**/
OssClient ossClient = new OssClient(EnvType.prod);
//EnvType.dev 开发环境
//EnvType.test 测试环境
//EnvType.prod 线上环境
上传
//参数
EfsRequest efsRequest = new EfsRequest();
efsRequest.setAppId("应用id");
efsRequest.setFileName("文件名称");
efsRequest.setLiveDay(1L); //文件存在时间(单位天),默认0无期限
/**同步上传(随机路径)**/
RequestResult<UploadRes> result = ossClient.syncUpload(file,efsRequest);
String fileId = result.getData().getFile_id(); //文件id
/**同步上传(固定路径,存在文件则更新)**/
String fileUri = "/aa/bb/cc.xx"; //文件路径
RequestResult<UploadRes> result = ossClient.syncUpload(file,fileUri,efsRequest);
String fileId = result.getData().getFile_id(); //文件id
/**异步上传(随机路径)**/
ossClient.upload(file, efsRequest, new Callback<RequestResult<UploadRes>>() {
@Override
public RequestResult<UploadRes> parseNetworkResponse(Response response, int i) throws Exception {
return null;
}
@Override
public void onError(Call call, Exception e, int i) {
}
@Override
public void onResponse(RequestResult<UploadRes> res, int i) {
String fileId = res.getData().getFile_id();
}
});
/**异步上传(固定路径,存在文件则更新)**/
String fileUri = "/aa/bb/cc.xx"; //文件路径
ossClient.upload(file,fileUri,efsRequest, new Callback<RequestResult<UploadRes>>() {
@Override
public RequestResult<UploadRes> parseNetworkResponse(Response response, int i) throws Exception {
return null;
}
@Override
public void onError(Call call, Exception e, int i) {
}
@Override
public void onResponse(RequestResult<UploadRes> res, int i) {
String fileId = res.getData().getFile_id();
}
});
删除
RequestResult<Boolean> result= ossClient.delete(fileId);
boolean success = result.getData();
下载
/**异步下载(直接本地文件存储)**/
ossClient.donwloadByFileId(fileId, new FileCallBack("文件存储目录路径","文件名") {
@Override
public void onError(Call call, Exception e, int i) {
}
@Override
public void onResponse(File file, int i) {
}
});
/**同步下载**/
InputStream ins = ossClient.syncDonwloadByFileId(fileId);