如何找到只给定文件名的文件路径?

mhd8tkvw  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(358)

我目前正在通过tcp连接将文件从服务器传输到客户端。该文件存在于服务器的sharedroot目录中的某个位置。这是我的上传服务器上传到客户端方法的示例代码。

public void upload(String filename, DataOutputStream out) throws IOException {
        File fname = null;
        if (filename.contains(sharedroot)) {   //this is if the client provides a proper filepath with the filename
            fname = new File(filename);
        }else {      //if client only provides a filename without path
            fname = new File(filename);  
//"..\\..\\"+   //i was working around with this, but somehow just making the file whether or not it contains the sharedroot seems to give me the "best" output so far...
        }

        System.out.println(fname.getCanonicalPath());

        if (fname.isDirectory()) {
            System.out.println("File is a directory");
            String quit = "404 not found";
            sendOut(quit, out);
            return;
        }

        String path = fname.getAbsolutePath();
        System.out.println(path);

        if (fname.isFile()) {
            String canonpath = fname.getCanonicalPath();
            if (canonpath.contains(sharedroot)) {
                try {
                    FileInputStream in = new FileInputStream(fname);

                    byte[] buffer = new byte[1024];

                    out.writeInt(fname.getName().length());
                    out.write(fname.getName().getBytes(), 0, fname.getName().length()); // writes file name only, not
                                                                                        // including the path

                    long size = fname.length();
                    out.writeLong(size);

                    while (size > 0) {
                        int len = in.read(buffer, 0, buffer.length);
                        out.write(buffer, 0, len);
                        size -= len;
                    }
                } catch (Exception e) {
                    System.out.println("Error occurred in uploading file to client. Please try again");
                }
            }else {
                System.out.println("File not in shared directory");
                String quit = "404 not found";
                sendOut(quit, out);
            }
        }else {
            System.out.println("File not exists");
            String quit = "404 not found";
            sendOut(quit, out);
        }
    }

下面所示的getcanonicalpath()和getabsolutepath()给出的输出是错误的,因为它检查的是我的eclipse目录,而不是sharedroot目录。如何获取文件的文件路径,以便将其与sharedoot进行比较,并确保它存在于sharedoot中?sharedroot将是例如:d:\seant\2unifiles\1。2005年3年级
d:\seant\eclipse workspace\dcn3005\Teach1练习.pdf
d:\seant\eclipse workspace\dcn3005\Teach1练习.pdf
文件不存在

dpiehjr4

dpiehjr41#

您创建的文件没有指定专用目录。有两个构造函数需要(根)目录和文件名—一个作为文件本身,另一个作为字符串。我假设您的一个路径是相对的,但是您的else分支创建文件的方式与完全限定路径相同。应该将sharedoot作为第一个参数传递,将文件名作为第二个参数传递。

File fname = null;
// sharedRoot is more like a constant and startsWith
// avoids reading somewhere else that looks similar
if (filename.startsWith(sharedRoot)) {   
  fname = new File(filename);
} else {      
  fname = new File(sharedRoot, filename);
}

在所有其他情况下,相对路径都是相对于vm进程的根目录的,我指的是进程。例如,如果一个用户在用户的主目录中启动它,它将是相对于这个的。如果一个操作系统任务启动vm,它将与os进程的根相关,这可能是unix cron作业或windows调度任务。
也许您引入了一种sharedroot的配置,这样如果将来发生更改,就不需要重新编译。

相关问题