在 jsp 表单中,用户输入报告文件 URL,即 https://s3.amazonaws.com/cdn.gs.com/live/reports/MVR_Q3_2009.pdf 或者 cdn.gs.com/live/reports/MVR_Q3_2009.pdf
但是当提交表单时,java类(struts2操作)必须通过将cdn.gs.com作为存储桶名称和/reports/来检查其是否是有效的url MVR_Q3_2009.pdf 作为文件路径,然后在 AWS s3 存储桶中搜索。
目前,我们从 url 获取 file_path,但在 checkin S3 之前,将 cdn.gs.com 作为静态变量输入。
我们可以从URL中获取cdn.gs.com作为bucket_name(字符串变量)吗 https://s3.amazonaws.com/cdn.gs.com/live/reports/MVR_Q3_2009.pdf 或者 cdn.gs.com/live/reports/MVR_Q3_2009.pdf
最佳答案
在研究了 org.apache.commons.StringUtils.java 类后,我得到了一个解决方案 这是解决我的问题的代码。
公共(public) boolean isValidReportURL(String url) {
if (StringUtils.startsWith(url, "https://s3.amazonaws.com")) {
bucketName = StringUtils.substringBetween(url, "com/", "/");
filePath = StringUtils.substringAfter(url, bucketName).replaceFirst("/", "");
} else {
bucketName = StringUtils.split(url, "/")[0];
filePath = (StringUtils.substringAfter(url, bucketName).replaceFirst("/", ""));
}
if (AWSFileUtil.doesFileExist(AWSConnectionUtil.getS3Object(null), bucketName,
filePath)) {
return true;
}
return false;
}
url 变量可以是
https://s3.amazonaws.com/cdn.gs.com/live/reports/MVR_Q3_2009.pdf 或 cdn.gs.com/live/reports/MVR_Q3_2009.pdf
关于Java字符串操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9460392/