简体   繁体   English

Java字符串操作

[英]Java String Operations

In a jsp form the user enter the report file URL ie, https://s3.amazonaws.com/cdn.gs.com/live/reports/MVR_Q3_2009.pdf or cdn.gs.com/live/reports/MVR_Q3_2009.pdf 用户以jsp格式输入报告文件URL,即https://s3.amazonaws.com/cdn.gs.com/live/reports/MVR_Q3_2009.pdf或cdn.gs.com/live/reports/MVR_Q3_2009.pdf

But when the form is submitted, a java class(struts2 action) has to checks whether its a valid url by taking the cdn.gs.com as bucket name and /reports/MVR_Q3_2009.pdf as file path and then searches in the AWS s3 bucket. 但是,在提交表单时,java类(struts2操作)必须通过将cdn.gs.com作为存储区名称并将/reports/MVR_Q3_2009.pdf作为文件路径,来检查其有效URL,然后在AWS s3中进行搜索桶。

For now we are getting the file_path from the url, but the cdn.gs.com is entered as a static variable before checking into S3. 现在,我们从URL获取file_path,但在登录S3之前,将cdn.gs.com作为静态变量输入。

Can we get the cdn.gs.com as bucket_name(string variable) from the URL https://s3.amazonaws.com/cdn.gs.com/live/reports/MVR_Q3_2009.pdf or cdn.gs.com/live/reports/MVR_Q3_2009.pdf 我们是否可以从URL https://s3.amazonaws.com/cdn.gs.com/live/reports/MVR_Q3_2009.pdf或cdn.gs.com/live/中获取cdn.gs.com作为bucket_name(字符串变量)报告/MVR_Q3_2009.pdf

I got a solution for this, after looking into org.apache.commons.StringUtils.java class Here is the code which solves my issue. 在研究org.apache.commons.StringUtils.java类之后,我得到了一个解决方案。这是解决我的问题的代码。

public boolean isValidReportURL(String url) { 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 variable can be url变量可以是

https://s3.amazonaws.com/cdn.gs.com/live/reports/MVR_Q3_2009.pdf or cdn.gs.com/live/reports/MVR_Q3_2009.pdf https://s3.amazonaws.com/cdn.gs.com/live/reports/MVR_Q3_2009.pdfcdn.gs.com/live/reports/MVR_Q3_2009.pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM