Java判断文件后缀名是否有效

String docUrl = "http://img.aaa.com/111++新建 Microsoft Excel 工作表-20180320163427.xlsx";
String[] allowTypes = new String[] { "docx","doc","xls","xlsx","ppt","pptx","pdf" };
int splitIndex = docUrl.lastIndexOf(".");
String fileType = docUrl.substring(splitIndex + 1);//doc、xls等
 
 
//判断文件类型是否在以上内容之内
if(DataCheck.isHasSuffix(fileType ,allowTypes)) {
   //do something...
}

 
 
/**
 * 判断类型是否包含这些
 * @param fileType
 * @return
 */
public static boolean isHasSuffix(String fileType,String... allowTypes) {
    Boolean CanUploaded = isValid(fileType, allowTypes);
    if (CanUploaded) {
        System.out.println("允许上传!");
        return true;
    } else {
        System.out.println("禁止上传!");
        return false;
    }
}

public static boolean isValid(String contentType, String... allowTypes) {
    if (null == contentType || "".equals(contentType)) {
        return false;
    }
    for (String type : allowTypes) {
        if (contentType.indexOf(type) > -1) {
            return true;
        }
    }
    return false;
}

猜你喜欢

转载自blog.csdn.net/ccmedu/article/details/79666495
今日推荐