Java 如何获取文件的完整路径?
可能重复:Java 如何获取文件的完整路径?,java,file,servlets,security,Java,File,Servlets,Security,可能重复: 是否有任何方法可以在不使用第三方API的情况下从输入文件标签获取文件的完整路径 <form method="post" action="SendTheFileName"> <div id="Files_to_be_shared"> <input type="file" id="File" name="FileTag" /> &
是否有任何方法可以在不使用第三方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>
我现在从上面的java代码中得到的只是所选文件的名称。如何获取文件的完整路径?这个问题已经在其他线程中得到了回答,我认为这2个问题中的任何一个都可以帮助您: 为了进一步完成我的回答,由于您在其中添加了java标记,这里我将为您提供一段代码,其中显示了您可以从文件中获得的一些最常见的信息,但从java的角度来看:
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());
}
}
“如何获取文件的完整路径?”完整路径不是您的web应用程序。业务,是一般视图。不,您实际上需要文件内容。另请参见重复链接。哦,您因此没有阅读重复链接。好了,到此为止。因为P2P/Torrent协议被发明并被证明更有效。HTTP/HTML不支持此功能。如果你真的很固执;在web开发方面,最好在网页中嵌入客户端应用程序(Applet、Silverlight等)。如果使用小程序(Swing),则可以使用
JFileChooser
获取完整路径。最后,像BitTorrent/uTorrent/Emule这样的桌面应用程序要优雅得多。比方说,它是一个错误的工具。
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());
}
}