java.io.file反斜杠保存在weblogic';根

java.io.file反斜杠保存在weblogic';根,java,file,weblogic,java-io,fileoutputstream,Java,File,Weblogic,Java Io,Fileoutputstream,我有一个运行在weblogic服务器上的java应用程序 应用程序必须将文件写入路径\bla\john doe(例如) 为此,我使用java.io.File库: 1. Verify if the path exists 2. If not, create it. 3. Verify if the file exists 4. if not, create it 5. Write the bytes into the file. 正确的行为是将目录bla创建到weblogic当前域的根目录中,然

我有一个运行在weblogic服务器上的java应用程序

应用程序必须将文件写入路径
\bla\john doe
(例如)

为此,我使用
java.io.File
库:

1. Verify if the path exists
2. If not, create it.
3. Verify if the file exists
4. if not, create it
5. Write the bytes into the file.
正确的行为是将目录
bla
创建到weblogic当前域的根目录中,然后在其中创建一个
john doe

问题是:< /强>在我当前的环境中,它像一个符咒,但是在客户端的应用程序中,应用程序不把“强>反斜杠< /强>作为路径的一个元素,而不是创建两个目录,该应用程序只创建一个,字面上命名为<代码> \ BLA \约翰做< /代码> ./P> 因此,不是:

-domain_root
    -bla
        -john does
我得到以下信息:

-domain_root
    -\bla\john does
(如果我逃避它,则发生相同的情况,但有两个反斜杠)

奇怪的是,如果我使用普通斜杠(
/bla/john doe
),它会工作

-domain_root
    -bla
        -john does
有人知道会发生什么吗


用于检查路径的脚本

public File checkPath(String path) {
    File f = new File(cls_Util.NeutralizeFilePath(path));
    if (!(f.exists() && f.isDirectory())) {
        try {
            f.mkdirs();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    return f;
}
用于检查文件的脚本:

public File checkFile(String path){
    File f = new File(path);
    return checkFile(f);
}

public File checkFile(File f) {
    if (!(f.exists() && f.isFile())) {
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    return f;
}
String filePathPub = pathPub + newName;
File FilePathPub = writeFile(filePathPub, p_Arquivo);
用于创建文件的脚本

public File writeFile(String path, byte[] binaryfile) {
    File file = checkFile(path);

    if (file != null) {
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(path);
            try {
                fos.write(binaryfile);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return file;
    }
    return null;
}
要创建文件,请执行以下操作:

public File checkFile(String path){
    File f = new File(path);
    return checkFile(f);
}

public File checkFile(File f) {
    if (!(f.exists() && f.isFile())) {
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    return f;
}
String filePathPub = pathPub + newName;
File FilePathPub = writeFile(filePathPub, p_Arquivo);

在Windows上,
\
启动一个绝对路径;在Unix/Linux上,反斜杠是一个有效的文件名字符(因此会启动一个相对路径)

如果您不熟悉以下语义,我建议您尽量避免使用文件名连接平台特定的分隔符:

File current = new File();
File bla = new File(current, "bla");

(或者只需坚持使用
/
(Unix使用的正斜杠)来分隔路径组件)。Java会自动将其转换为Windows字符)。

只需使用/。它适用于所有环境。注意:第3步和第4步是多余的。新的FileOutputStream()已经做到了这一点。不要重新发明轮子。也为自己节省16行代码。NB File.mkdirs()不会抛出异常,除了NPE.Ok,但其核心目的是访问(并将文件写入)网络路径,例如
\\server\directory
,我如何实现这一点?您仍然可以尝试。发布一些代码。客户端是否运行Windows?客户端中运行的O.S.是
soselinux
。。。(我将尝试一下你的建议)那么,如果客户端没有运行Windows,为什么要提供带有反斜杠的文件名?当然不行了。