|
@@ -7,7 +7,9 @@ import cn.com.qmth.examcloud.support.fss.FssHelper;
|
|
|
import cn.com.qmth.examcloud.support.fss.FssProperty;
|
|
|
import cn.com.qmth.examcloud.support.fss.FssService;
|
|
|
import cn.com.qmth.examcloud.support.fss.model.FssFileInfo;
|
|
|
+import cn.com.qmth.examcloud.support.fss.model.FssSignInfo;
|
|
|
import cn.com.qmth.examcloud.support.fss.model.FssType;
|
|
|
+import okhttp3.*;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
import org.apache.logging.log4j.Level;
|
|
|
import org.apache.logging.log4j.core.config.Configurator;
|
|
@@ -15,6 +17,7 @@ import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
import java.nio.file.Files;
|
|
|
|
|
|
public class OssClientTest {
|
|
@@ -45,29 +48,54 @@ public class OssClientTest {
|
|
|
// FssProperty.FSS_ACCESS_KEY_SECRET = "xxx";
|
|
|
// FssProperty.FSS_URL_PREFIX = "https://examcloud-1252178304.cos.ap-guangzhou.myqcloud.com";
|
|
|
|
|
|
- final String testDir = "D:/home/test";
|
|
|
- File testFile = new File(testDir + "/abc.png");
|
|
|
-
|
|
|
- final String filePath = "/test/abc.png";
|
|
|
- final String fileMd5 = "80bf42bd4d12b2a38eff3b32249b17fd";
|
|
|
+ final String testDir = "D:/home";
|
|
|
+ final String filePath = "test/abc.png";
|
|
|
+ File testFile = new File(testDir + "/" + filePath);
|
|
|
+ final String testFileMd5 = "80bf42bd4d12b2a38eff3b32249b17fd";
|
|
|
|
|
|
FssService fssService = FssFactory.getInstance(false);
|
|
|
- System.out.println(new JsonMapper().toJson(fssService.buildSign(filePath, fileMd5)));
|
|
|
+ // this.upload(fssService, filePath, testFile, testFileMd5);
|
|
|
+ // this.signUpload(fssService, filePath, testFile, testFileMd5);
|
|
|
+ // this.download(fssService, filePath, testDir);
|
|
|
+ // fssService.refreshFile(FssProperty.FSS_URL_PREFIX + "/" + filePath);
|
|
|
+ }
|
|
|
|
|
|
- // FssFileInfo result = fssService.writeFile(filePath, testFile, fileMd5);
|
|
|
- byte[] testBytes = IOUtils.toByteArray(Files.newInputStream(testFile.toPath()));
|
|
|
- FssFileInfo result = fssService.writeFile(filePath, testBytes, fileMd5);
|
|
|
+ public void upload(FssService fssService, String filePath, File testFile, String testFileMd5) throws Exception {
|
|
|
+ byte[] testFileBytes = IOUtils.toByteArray(Files.newInputStream(testFile.toPath()));
|
|
|
+ FssFileInfo result = fssService.writeFile(filePath, testFileBytes, testFileMd5);
|
|
|
System.out.println(result.getFileUrl());
|
|
|
+ }
|
|
|
|
|
|
- File toFile = new File(testDir + "/abc-new.png");
|
|
|
- // fssService.readFile(filePath, toFile);
|
|
|
- byte[] bytes = fssService.readFile(filePath);
|
|
|
- FileUtil.saveToFile(bytes, toFile);
|
|
|
-
|
|
|
- System.out.println("exist:" + fssService.existFile(filePath));
|
|
|
- fssService.refreshFile(result.getFileUrl());
|
|
|
+ public void signUpload(FssService fssService, String filePath, File testFile, String testFileMd5) {
|
|
|
+ FssSignInfo signInfo = fssService.buildSign(filePath, testFileMd5);
|
|
|
+ System.out.println(new JsonMapper().toJson(signInfo));
|
|
|
+
|
|
|
+ MultipartBody.Builder form = new MultipartBody.Builder().setType(MultipartBody.FORM);
|
|
|
+ RequestBody reqBody = RequestBody.create(MediaType.parse("application/octet-stream"), testFile);
|
|
|
+ form.addFormDataPart("key", signInfo.getFormParams().get("key"));
|
|
|
+ form.addFormDataPart("OSSAccessKeyId", signInfo.getFormParams().get("OSSAccessKeyId"));
|
|
|
+ form.addFormDataPart("policy", signInfo.getFormParams().get("policy"));
|
|
|
+ form.addFormDataPart("Signature", signInfo.getFormParams().get("Signature"));
|
|
|
+ form.addFormDataPart("file", testFile.getName(), reqBody);
|
|
|
+
|
|
|
+ Request.Builder request = new Request.Builder().url(signInfo.getFormUrl()).post(form.build());
|
|
|
+ try (Response response = new OkHttpClient.Builder().build().newCall(request.build()).execute();
|
|
|
+ ResponseBody body = response.body();) {
|
|
|
+ String bodyStr = body != null ? body.string() : null;
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ System.out.println("上传成功!" + bodyStr);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ System.out.println("上传失败!" + bodyStr);
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println("上传异常!" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // fssService.deleteFile(filePath);
|
|
|
+ public void download(FssService fssService, String filePath, String testDir) {
|
|
|
+ File newFile = new File(testDir + "/test/abc-new.png");
|
|
|
+ byte[] newFileBytes = fssService.readFile(filePath);
|
|
|
+ FileUtil.saveToFile(newFileBytes, newFile);
|
|
|
}
|
|
|
|
|
|
// @Test
|