How do I get the complete path of the file? [dupli

2019-07-25 13:05发布

问题:

Possible Duplicate:
How to get the file path from HTML input form in Firefox 3

Is there any way I can get the complete path of the file from the input file tag without using the 3rd party API ?

<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>

Snippet from the servlet :

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

What I get now from the above java code is just the name of the file selected. How do I get the complete path of the file ?

回答1:

This question was already answered in other threads, I think that any of this 2 can help you:

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?

Just to complete my answer a bit more and since you added the java tag to it, here I will include you a little snippet of code that shows some of the most common information you can get out of a file, but from the 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());
    }

}