如何获取文件的完整路径?

时间:2013-01-15 13:35:19

标签: java file servlets security

  

可能重复:
  How to get the file path from HTML input form in Firefox 3

有没有办法在不使用第三方API的情况下从input file tag获取文件的完整路径?

<form method="post" action="SendTheFileName">
                <div id="Files_to_be_shared"> 
                      <input type="file" id="File" name="FileTag" />
                      <input type="submit" value="Share" /> 
               </div>
 </form>
来自servlet的

Snippet:

String fileName = request.getParameter("FileTag");

我从上面的java代码得到的只是所选文件的名称。如何获取文件的完整路径?

1 个答案:

答案 0 :(得分:0)

这个问题已在其他主题中得到解答,我认为这2中的任何一个都可以帮助你:

How to get full path using <input id="file" name="file" type="file" size="60" > in jsp

How to get full path of file using input type file in javascript?

为了更多地完成我的答案,因为你添加了java标签,在这里我将包含一些代码片段,显示一些你可以从文件中获得的最常见的信息,但是从java perspective:

package test;

import java.io.File;
import java.io.IOException;

public class FilePaths {

    public static void main(String[] args) throws IOException {

        String[] fileArray = { 
                "C:\\projects\\workspace\\testing\\f1\\f2\\f3\\file5.txt", 
                "folder/file3.txt",
                "../testing/file1.txt", 
                "../testing", 
                "f1/f2"
        };

        for (String f : fileArray) {
            displayInfo(f);
        }

    }

    public static void displayInfo(String f) throws IOException {
        File file = new File(f);
        System.out.println("========================================");
        System.out.println("          name:" + file.getName());
        System.out.println("  is directory:" + file.isDirectory());
        System.out.println("        exists:" + file.exists());
        System.out.println("          path:" + file.getPath());
        System.out.println(" absolute file:" + file.getAbsoluteFile());
        System.out.println(" absolute path:" + file.getAbsolutePath());
        System.out.println("canonical file:" + file.getCanonicalFile());
        System.out.println("canonical path:" + file.getCanonicalPath());
    }

}