Java 在linux中使用springboot上载映像

Java 在linux中使用springboot上载映像,java,linux,spring,spring-boot,multipartform-data,Java,Linux,Spring,Spring Boot,Multipartform Data,我尝试使用springboot上传图像。该应用程序应支持Linux和windows。以下代码仅在windows中工作,在Linux中不工作。我试过不同的方法getDefaultFolderPath()正在两个操作系统中创建文件夹(如果不存在)。但是Files.write(路径,字节)仅在windows中工作 String imageWindowsPath = "C:\\myapp\\"; String imageLinuxPath = "user.home"; private Stri

我尝试使用
springboot
上传图像。该应用程序应支持Linux和windows。以下代码仅在windows中工作,在Linux中不工作。我试过不同的方法
getDefaultFolderPath()
正在两个操作系统中创建文件夹(如果不存在)。但是
Files.write(路径,字节)仅在windows中工作

String imageWindowsPath = "C:\\myapp\\";
String imageLinuxPath = "user.home";    

private String uploadImage(MultipartFile file, HttpServletRequest request) {
    String fileName = "blur.png";

    if (!file.isEmpty()) {
        byte[] bytes = file.getBytes(); 
        Path path = Paths.get(getDefaultFolderPath() + fileName);
        Files.write(path, bytes);
    }

    //few other codes
    retrun "";
}

//Creating folder if not exists... This is working for both OS
public String getDefaultFolderPath() {

    String path = "";
    try {
        String os = System.getProperty("os.name");
        if (os.toUpperCase().indexOf("WINDOWS") != -1) {
            File file = new File(imageWindowsPath+"slider");                
            if (!file.exists())
                file.mkdirs();

            path = imageWindowsPath+"/slider/";
        } 
        else if (os.toUpperCase().indexOf("LINUX") != -1) {
            String userHome = System.getProperty(imageLinuxPath);
            String appName = "slider";

            Path linuxpath = Paths.get(userHome, appName);          
                Files.createDirectories(linuxpath); 

            path = imageLinuxPath+"/slider/";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    //few codes 

    return path;
}
如果有人知道,请给我一个解决方案。提前谢谢

String imageLinuxPath = "user.home";    

        String userHome = System.getProperty(imageLinuxPath);
        String appName = "slider";

        Path linuxpath = Paths.get(userHome, appName);          
            Files.createDirectories(linuxpath); 
基于环境变量“user.home”创建目录

返回“user.homeslider/”

由于环境变量user.home的内容可能不是“user.home”,因此路径将不是创建的目录。 这意味着您正试图写入一个不存在的目录


我想补充一点,Windows和Linux之间的区别完全是您自己创造的。如果您有Windows属性和主路径,Java可以完全以相同的方式处理它们。

最先进的技术是将目录定义为代码外部的配置,并且可以轻松地适应您要部署到的环境。不要试图检测您正在哪个操作系统上运行。@kumesana,但这根本不起作用,知道吗?您似乎返回了一条与您创建的路径不同的路径。如果需要在linux中创建路径,您可能需要创建所有路径。您正在创建linuxpath并返回imageLinuxPath+“slider/”。如果它们是相同的,你应该改变你的代码来反映它。我想你没有检查代码,我只是返回一个字符串。但是路径是自动创建的`path linuxpath=path.get(userHome,appName);创建目录(linuxpath)`对不起,我的错误,这是一个错误,而写在这里,我会编辑它。但是它不起作用:'(你不应该发布想象中的代码并期望得到帮助。对不起,我键入时没有检查,我说它正在创建文件夹,所以文件的问题。write()?你读过我的答案吗?根据问题中的代码,我已经解释了write()为什么不起作用。如果代码没有显示你的问题,你希望得到帮助吗?
        path = imageLinuxPath+"slider/";
return path;