可以通过获取该文件所在文件夹的绝对路径实现
代码:import java.io.*;
import javax.servlet.*;
import javax.servlet.jsp.PageContext;//导入PageContext类,不要忘了
public class PhysicalPath

protected ServletContext m_application;
private boolean m_denyPhysicalPath;
public PhysicalPath()
{}
public final void initialize(PageContext pageContext)
throws ServletException
{
m_application = pageContext.getServletContext();} public String getPhysicalPath(String filePathName, int option)
throws IOException
{
String path = new String();
String fileName = new String();
String fileSeparator = new String();
boolean isPhysical = false;
fileSeparator=System.getProperty("file.separator");
if(filePathName == null)
throw new IllegalArgumentException("There is no specified destination file (1140).");
if(filePathName.equals(""))
throw new IllegalArgumentException("There is no specified destination file (1140).");
if(filePathName.lastIndexOf("\\") >= 0)
{
path = filePathName.substring(0, filePathName.lastIndexOf("\\"));
fileName = filePathName.substring(filePathName.lastIndexOf("\\") + 1);
}
if(filePathName.lastIndexOf("/") >= 0)
{
path = filePathName.substring(0, filePathName.lastIndexOf("/"));
fileName = filePathName.substring(filePathName.lastIndexOf("/") + 1);
}
path = path.length() != 0 ? path : "/";
java.io.File physicalPath = new java.io.File(path);
if(physicalPath.exists())
isPhysical = true;
if(option == 0)
{
if(isVirtual(path))
{
path = m_application.getRealPath(path);
if(path.endsWith(fileSeparator))
path = path + fileName;
else
path = String.valueOf((new StringBuffer(String.valueOf(path))).append(fileSeparator).append(fileName));
return path;
}
if(isPhysical)
{
if(m_denyPhysicalPath)
throw new IllegalArgumentException("Physical path is denied (1125).");
else
return filePathName;
} else
{
throw new IllegalArgumentException("This path does not exist (1135).");
}
}
if(option == 1)
{
if(isVirtual(path))
{
path = m_application.getRealPath(path);
if(path.endsWith(fileSeparator))
path = path + fileName;
else
path = String.valueOf((new StringBuffer(String.valueOf(path))).append(fileSeparator).append(fileName));
return path;
}
if(isPhysical)
throw new IllegalArgumentException("The path is not a virtual path.");
else
throw new IllegalArgumentException("This path does not exist (1135).");
}
if(option == 2)
{
if(isPhysical)
if(m_denyPhysicalPath)
throw new IllegalArgumentException("Physical path is denied (1125).");
else
return filePathName;
if(isVirtual(path))
throw new IllegalArgumentException("The path is not a physical path.");
else
throw new IllegalArgumentException("This path does not exist (1135).");
} else
{
return null;
}}
private boolean isVirtual(String pathName) //判断是否是虚拟路径
{
if(m_application.getRealPath(pathName) != null)
{
java.io.File virtualFile = new java.io.File(m_application.getRealPath(pathName));
return virtualFile.exists();
} else
{
return false;
}
}
}该文件的路径获取:
//获取txt文件所在绝对路径
PhysicalPath physicalPath=new PhysicalPath();
physicalPath.initialize(pageContext);//初始化
String dir1=physicalPath.getPhysicalPath("/TXT文件夹名/",0);//传参数
String filepath=dir1+fileName;//文件的路径