本地window系统上传下载没有问题,服务器linux下文件上传到相应的文件夹,但是下载报报java.io.FileNotFoundException异常,是因为在windows操作系统中,文件路径的分隔符是:"\ “,在linux操作系统中,文件路径的分隔符是:”/"。
在文件上传中
String rootPath = application.getRealPath("/");
if(!rootPath.endsWith("/")){
rootPath+=”/”;
}
改为
String rootPath = application.getRealPath("/");
if(!rootPath.endsWith("/")){
rootPath+=File.separator;//转义后面的“/”
}
-
file.separator 和 / 区别
在windows中的文件分隔符是 \ 和 /都可以,但是在Linux中,文件分隔符只能是/,所以用了\的程序在Linux下会出问题。而File.separator是系统默认的文件分割符号,屏蔽了这些系统的区别。
用File.separator保证了在任何系统下不会出错。