如何获取文件的完整路径? [英] How do I get the complete path of the file?
问题描述
Possible Duplicate:
How to get the file path from HTML input form in Firefox 3
有什么方法可以从input file tag
获取文件的完整路径而无需使用第三方API?
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>
servlet的
片段:
Snippet from the servlet :
String fileName = request.getParameter("FileTag");
我现在从上面的Java代码中得到的只是所选文件的名称.如何获取文件的完整路径?
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 ?
推荐答案
这个问题已经在其他线程中得到解答,我认为这2个中的任何一个都可以帮助您:
This question was already answered in other threads, I think that any of this 2 can help you:
如何使用< input id ='file'名称=文件" type ="file"尺寸="60" >在jsp中
如何获取使用javascript中的输入类型文件的文件的完整路径?
只是为了进一步完善我的答案,而且由于您已向其中添加了Java标记,所以在这里我将向您提供一些代码片段,这些代码片段展示了一些您可以从文件中获取的最常见信息,但是从Java视角:
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());
}
}
这篇关于如何获取文件的完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!