写的一个jsp程序在本地调试没问题(windows平台 tomcat)
但是把它上传到一个虚拟主机,系统:linux JDK版本 jdk1.6.0_10 TOMCAT版本 tomcat6.0.18
用了三种方法
1)String strAbsPath = new java.io.File(new java.io.File(application.getRealPath(request.getRequestURI())).getParent()).getParent();
2)String strAbsPath = request.getRealPath("/")
3)使用下面的这个类:
public class Path {
protected ServletContext m_application;
private boolean m_denyPhysicalPath;
public final void initialize(PageContext pageContext) throws ServletException
{
m_application = pageContext.getServletContext();
}
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;
}
} 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;
}
}
}最终获得的绝对路径都是/v0/username/bbs/ 这样形式的路径,文件还是找不到。
现在该怎么解决这个问题啊???
但是把它上传到一个虚拟主机,系统:linux JDK版本 jdk1.6.0_10 TOMCAT版本 tomcat6.0.18
用了三种方法
1)String strAbsPath = new java.io.File(new java.io.File(application.getRealPath(request.getRequestURI())).getParent()).getParent();
2)String strAbsPath = request.getRealPath("/")
3)使用下面的这个类:
public class Path {
protected ServletContext m_application;
private boolean m_denyPhysicalPath;
public final void initialize(PageContext pageContext) throws ServletException
{
m_application = pageContext.getServletContext();
}
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;
}
} 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;
}
}
}最终获得的绝对路径都是/v0/username/bbs/ 这样形式的路径,文件还是找不到。
现在该怎么解决这个问题啊???
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货