Solution to Linux throwing file not found exception

王林
Release: 2019-11-14 11:05:25
Original
2980 people have browsed it

Solution to Linux throwing file not found exception

场景:

项目在windows下访问正常,linux下抛异常,找不到文件。

分析:

如果访问的项目文件是这样的:abc/bcd/aa.jpg ,而系统中访问文件的路径是:abc/Bcd/aa.jpg,两者的区别是二级目录bcd写成了Bcd。

如果在windows下开发的小伙伴们,是发现不了错误的,windows文件名大小写不敏感,因此,按照 abc/Bcd/aa.jpg依然能访问到文件,但是linux文件目录大小写敏感,因此会找不到文件。

解决方法:

在平时的编程中,一定要严格的书写。

还需要注意一点:linux目录分隔符是 /,windows下是\,因此有时在windows存到数据库里的文件路径搬到linux下就会不适用,需要在系统中做处理,从数据库取出路径后,要经过加工,代码如下:

    /**
     * 修正路径,将 \\ 或 / 等替换为 File.separator
     * @param path 待修正的路径
     * @return 修正后的路径
     */
    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;
    }
Copy after login

推荐教程:Linux教程

The above is the detailed content of Solution to Linux throwing file not found exception. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!