在用java下载的时候,直接访问文件地址可以下载文件,但如果使用接口,将地址作为参数下载,可能会下载失败的问题。
修正路径
String appRoot = ClassUtil.getClassLoader().getResource("").getPath();
String filePath = path(appRoot + "/assets/template/Template.xls");
/**
*
* 修正路径,将 \\ 或 / 等替换为 File.separator
* path 待修正的路径
* 修正后的路径
*/
public static String path(String path) {
String p = StringUtils.replace( path, "\\", "/" );
p = StringUtils.join( StringUtils.split( p, "/" ), "/" );
if (!StringUtils.startsWithAny( p, "/" ) && StringUtils.startsWithAny( path, "\\", "/" )) {
p += "/";
}
if (!StringUtils.endsWithAny( p, "/" ) && StringUtils.endsWithAny( path, "\\", "/" )) {
p = p + "/";
}
if (path != null && path.startsWith( "/" )) {
p = "/" + p; // linux下路径
}
return p;
}