private String getUrl(File file, String excelName) {
String url=null;
try {
url = OssUploadController.uploadInputSteam(new FileInputStream(file), excelName+".rar");
System.out.println(url);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return url;
}
public void test(){
File file = ZipUtil.zip(PathKit.getWebRootPath() + "/" + fileUuid + "/workModel");
String url = getUrl(file, excelName);
if (StrKit.isBlank(url)){
re("失败");
return;
}
rsPara("成功",Kv.by("url",url));
}
public class OssUploadController extends BaseController{
@Clear
@Before(AllowCrossDomainInterceptor.class)
public void getSign(){
String endpoint = get("OSS-ENDPOINT");
String accessId = get("OSS-ACCESSID");
String accessKey = get("OSS-ACCESSKEY");
String bucket = get("OSS-BUCKET");
String dir = get("OSS-DIR");
String host = "https://" + bucket + "." + endpoint;
try {
OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey);
long expireTime = 30;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
String postPolicy = client.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes("utf-8");
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = client.calculatePostSignature(postPolicy);
Map<String, String> respMap = new LinkedHashMap<String, String>();
respMap.put("code", "0");
respMap.put("accessid", accessId);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", dir);
respMap.put("host", host);
respMap.put("expire", String.valueOf(expireEndTime / 1000));
renderJson(respMap);
} catch (Exception e) {
e.printStackTrace();
rw(e.getMessage());
}
}
public static String uploadInputSteam(InputStream file, String fileName) {
String endpoint = get("OSS-ENDPOINT");
String accessId = get("OSS-ACCESSID");
String accessKey = get("OSS-ACCESSKEY");
String bucket = get("OSS-BUCKET");
OSS ossClient = new OSSClientBuilder().build(endpoint, accessId, accessKey);
ossClient.putObject(bucket, fileName, file);
Date expiration = new Date(System.currentTimeMillis() + 24 * 3600 * 1000);
ossClient.setBucketAcl(bucket, CannedAccessControlList.PublicRead);
URL url = ossClient.generatePresignedUrl(bucket, fileName, expiration);
ossClient.shutdown();
return url.toString();
}
public class AllowCrossDomainInterceptor implements Interceptor {
@Override
public void intercept(Invocation inv) {
inv.getController().getResponse().addHeader("Access-Control-Allow-Origin", "*");
inv.getController().getResponse().addHeader("Access-Control-Allow-Credentials", "true");
inv.getController().getResponse().addHeader("Access-Control-Allow-Methods", "OPTIONS,HEAD,GET,PUT,POST,DELETE,PATCH");
inv.getController().getResponse().addHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, login_code,x-token,platform_code");
inv.getController().getResponse().addHeader("Access-Control-Max-Age", "3600");
if (StrKit.notBlank(inv.getController().getRequest().getMethod()) && inv.getController().getRequest().getMethod().equals("OPTIONS")) {
inv.getController().renderText("");
return;
}
String json = HttpKit.readData(inv.getController().getRequest());
if (StrKit.notBlank(json)) {
if (PropKit.getBoolean("devMode", false)) {
System.out.println(json);
}
JSONObject obj = JSONObject.parseObject(json);
inv.getController().setAttr("obj", obj);
}
inv.invoke();
}