Linux文件路徑分隔符為 / ,windows的文件路徑分隔符為 \ ,在開發項目過程中不確定用戶使用何種操作系統,就需要自動適配路徑
public class FilePathUtil { public static final String FILE_SEPARATOR = System.getProperty("file.separator"); public static String getRealFilePath(String path) { return path.replace("/", FILE_SEPARATOR).replace("\\", FILE_SEPARATOR); } public static String getHttpURLPath(String path) { return path.replace("\\", "/"); } }