-
常見大陸和香港號碼格式驗證
import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; public class PhoneFormatCheckUtils { // 大陸號碼或香港號碼均可 public static boolean isPhoneLegal(String str) throws PatternSyntaxException { return isChinaPhoneLegal(str) || isHKPhoneLegal(str); } /** * 大陸手機號碼11位數,匹配格式:前三位固定格式+後8位任意數 * 此方法中前三位格式有: * 13+任意數 * 15+除4的任意數 * 18+除1和4的任意數 * 17+除9的任意數 * 147 */ public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException { String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$"; Pattern p = Pattern.compile(regExp); Matcher m = p.matcher(str); return m.matches(); } /** * 香港手機號碼8位數,5|6|8|9開頭+7位任意數 */ public static boolean isHKPhoneLegal(String str) throws PatternSyntaxException { String regExp = "^(5|6|8|9)\\d{7}$"; Pattern p = Pattern.compile(regExp); Matcher m = p.matcher(str); return m.matches(); } }
-
對
FASTDFS
的封裝以及使用import org.csource.common.NameValuePair; import org.csource.fastdfs.*; public class FastDFSClient { private TrackerClient trackerClient = null; private TrackerServer trackerServer = null; private StorageServer storageServer = null; private StorageClient1 storageClient = null; public FastDFSClient(String conf) throws Exception { if (conf.contains("classpath:")) { conf = conf.replace("classpath:", this.getClass().getResource("/").getPath()); } ClientGlobal.init(conf); trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = null; storageClient = new StorageClient1(trackerServer, storageServer); } /** * 上傳文件方法 * <p>Title: uploadFile</p> * <p>Description: </p> * * @param fileName 文件全路徑 * @param extName 文件擴展名,不包含(.) * @param metas 文件擴展信息 * @return * @throws Exception */ public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception { String result = storageClient.upload_file1(fileName, extName, metas); return result; } public String uploadFile(String fileName) throws Exception { return uploadFile(fileName, null, null); } public String uploadFile(String fileName, String extName) throws Exception { return uploadFile(fileName, extName, null); } /** * 上傳文件方法 * <p>Title: uploadFile</p> * <p>Description: </p> * * @param fileContent 文件的內容,字節數組 * @param extName 文件擴展名 * @param metas 文件擴展信息 * @return * @throws Exception */ public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception { String result = storageClient.upload_file1(fileContent, extName, metas); return result; } public String uploadFile(byte[] fileContent) throws Exception { return uploadFile(fileContent, null, null); } public String uploadFile(byte[] fileContent, String extName) throws Exception { return uploadFile(fileContent, extName, null); } }
如何使用
FASTDFS
封裝類resources
下配置FASTDFS
信息:fdfs_client.conf
和application.properties
# connect timeout in seconds # default value is 30s connect_timeout=30 # network timeout in seconds # default value is 30s network_timeout=60 # the base path to store log files base_path=/home/fastdfs # tracker_server can ocur more than once, and tracker_server format is # "host:port", host can be hostname or ip address tracker_server=192.168.25.133:22122 #standard log level as syslog, case insensitive, value list: ### emerg for emergency ### alert ### crit for critical ### error ### warn for warning ### notice ### info ### debug log_level=info # if use connection pool # default value is false # since V4.05 use_connection_pool = false # connections whose the idle time exceeds this time will be closed # unit: second # default value is 3600 # since V4.05 connection_pool_max_idle_time = 3600 # if load FastDFS parameters from tracker server # since V4.05 # default value is false load_fdfs_parameters_from_tracker=false # if use storage ID instead of IP address # same as tracker.conf # valid only when load_fdfs_parameters_from_tracker is false # default value is false # since V4.05 use_storage_id = false # specify storage ids filename, can use relative or absolute path # same as tracker.conf # valid only when load_fdfs_parameters_from_tracker is false # since V4.05 storage_ids_filename = storage_ids.conf #HTTP settings http.tracker_server_port=80 #use "#include" directive to include HTTP other settiongs ##include http.conf ===================================================================================== FILE_SERVER_URL=http://192.168.25.133/
代碼演示
@RestController public class UploadController { @Value("${FILE_SERVER_URL}") private String FILE_SERVER_URL; //文件服務器地址 @RequestMapping("/upload") public Result upload(MultipartFile file) { // 獲取文件擴展名稱 String originalFilename = file.getOriginalFilename(); String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1); try { // 創建 fastDFS 客戶端 FastDFSClient fastDFSClient = new FastDFSClient("classpath:config/fdfs_client.conf"); // 上傳文件處理 String path = fastDFSClient.uploadFile(file.getBytes(), extName); // 拼接返回的 url 和 ip 地址,拼裝成完整的 url String url = FILE_SERVER_URL + path; return new Result(true, url); } catch (Exception e) { e.printStackTrace(); return new Result(false, "上傳失敗"); } } }
手機號碼格式驗證和 FASTDFS 工具類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.