Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
d4c3bce80f
@ -0,0 +1,47 @@
|
||||
package com.suisung.mall.common.utils;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
public class FilePathUtils {
|
||||
/**
|
||||
* 使用lastIndexOf分割路径和文件名
|
||||
*/
|
||||
public FilePath splitPathWithString(String fileUrl) {
|
||||
// 去除域名
|
||||
String noDomain = fileUrl.replaceFirst("^https?://[^/]+/", "");
|
||||
|
||||
// 分割路径和文件名
|
||||
int lastSlash = noDomain.lastIndexOf('/');
|
||||
String path, filename;
|
||||
|
||||
if (lastSlash != -1) {
|
||||
path = noDomain.substring(0, lastSlash + 1);
|
||||
filename = noDomain.substring(lastSlash + 1);
|
||||
} else {
|
||||
path = "";
|
||||
filename = noDomain;
|
||||
}
|
||||
FilePath filePath=new FilePath();
|
||||
filePath.path=path;
|
||||
filePath.filename=filename;
|
||||
return filePath;
|
||||
// System.out.println("原始URL: " + fileUrl);
|
||||
// System.out.println("去除域名: " + noDomain);
|
||||
// System.out.println("文件路径: " + path);
|
||||
// System.out.println("文件名称: " + filename);
|
||||
}
|
||||
@Data
|
||||
public class FilePath{
|
||||
private String path;//路径,不包含文件名称如media/media/plantform/20250906/
|
||||
private String filename;//文件名称
|
||||
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
String cosUrl = "https://media-mall-prod-1259811287.cos.ap-guangzhou.myqcloud.com/media/media/plantform/20250906/b93a9751b35a49fca6cf979829230868.png";
|
||||
|
||||
// 方法1:基础分割
|
||||
FilePath filePath= new FilePathUtils().splitPathWithString(cosUrl);
|
||||
System.out.println("文件路径: " + filePath.getPath());
|
||||
System.out.println("文件名称: " + filePath.getFilename());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user