话不多说,先上代码~~
/**
* 删除服务上的文件
* @param filePath 路径
* @param fileName 文件名
* @return
*/
@PostMapping("/deleteServerFile")
@ResponseBody
public static boolean deleteServerFile(String filePath){
boolean delete_flag = false;
File file = new File(filePath);
if (!file.exists()) {
return delete_flag;
}
try{
delete_flag = file.delete();
}catch (Exception e){
e.printStackTrace();
}
return delete_flag;
}
在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常。
比如说要在temp目录下建立一个test.txt文件,在Windows下应该这么写:
File file1 = new File ("C:\tmp\test.txt");
在linux下则是这样的:
File f