当前位置: 技术问答>java相关
疑难,高手请进
来源: 互联网 发布时间:2015-06-19
本文导语: 在一个servlet中写了这样一段: public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String download = request.getParameter("download"); HttpSession session=request.getSession(true) ;...
在一个servlet中写了这样一段:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String download = request.getParameter("download");
HttpSession session=request.getSession(true) ;
String user_name = (String)session.getAttribute("user_name");
请注意: String filename = "temp/"+user_name+".zip";
try
{
Data2Dom data2dom = new Data2Dom();
if(download.equals("no"))
{
Dom2Sax dmW = new Dom2Sax(response.getWriter());
response.setContentType(CONTENT_TYPE);
dmW.parse(data2dom.parse(new InputSource(new StringReader(request.getParameter("config") ))));
}
else if(download.equals("yes"))
{
请注意:PrintWriter pW=new PrintWriter(new FileWriter (filename,false));
Dom2Sax dmW = new Dom2Sax(pW);
dmW.parse(data2dom.parse(new InputSource(new StringReader(request.getParameter("config") ))));
response.sendRedirect("/XMLServlet/xml_download.jsp") ;
}
}
catch (Exception ex) { }
}
在jbuilder6中运行正确,请注意这句:
String filename = "temp/"+user_name+".zip";
PrintWriter pW=new PrintWriter(new FileWriter (filename,false));
本意是想在defaultroot下的temp目录下新建一个文件,因为filename
不能用http协议,默认为file协议,所以相对路径不是相对于servlet,
而是相对于当前工作目录(究竟是谁的工作目录我也不清楚),当我在
jbuilder中将project properties中的工作目录设为defaultroot后,
可正确建立文件。
问题是:在tomcat下单独访问这个servlet时(非jbuilder环境),发现
它将文件存到jakarta-tomcat-3.2.3/bin目录下,也就是说认为这是当前
工作目录,找遍配置文件和环境变量,也没找到修改工作目录的地方,
而且我觉得工作目录应该与正在运行的servlet有关,但尝试后发现相对路径
都是/bin目录,怎么办,请教,怎样使得工作目录为defaultroot,当然filename
用绝对路径是可以的,但这样移到其他机子上都要改也太丑了。
如有其他办法可用相对于defaultroot的路径建立文件,也请告知,感激不尽。
期盼答复
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String download = request.getParameter("download");
HttpSession session=request.getSession(true) ;
String user_name = (String)session.getAttribute("user_name");
请注意: String filename = "temp/"+user_name+".zip";
try
{
Data2Dom data2dom = new Data2Dom();
if(download.equals("no"))
{
Dom2Sax dmW = new Dom2Sax(response.getWriter());
response.setContentType(CONTENT_TYPE);
dmW.parse(data2dom.parse(new InputSource(new StringReader(request.getParameter("config") ))));
}
else if(download.equals("yes"))
{
请注意:PrintWriter pW=new PrintWriter(new FileWriter (filename,false));
Dom2Sax dmW = new Dom2Sax(pW);
dmW.parse(data2dom.parse(new InputSource(new StringReader(request.getParameter("config") ))));
response.sendRedirect("/XMLServlet/xml_download.jsp") ;
}
}
catch (Exception ex) { }
}
在jbuilder6中运行正确,请注意这句:
String filename = "temp/"+user_name+".zip";
PrintWriter pW=new PrintWriter(new FileWriter (filename,false));
本意是想在defaultroot下的temp目录下新建一个文件,因为filename
不能用http协议,默认为file协议,所以相对路径不是相对于servlet,
而是相对于当前工作目录(究竟是谁的工作目录我也不清楚),当我在
jbuilder中将project properties中的工作目录设为defaultroot后,
可正确建立文件。
问题是:在tomcat下单独访问这个servlet时(非jbuilder环境),发现
它将文件存到jakarta-tomcat-3.2.3/bin目录下,也就是说认为这是当前
工作目录,找遍配置文件和环境变量,也没找到修改工作目录的地方,
而且我觉得工作目录应该与正在运行的servlet有关,但尝试后发现相对路径
都是/bin目录,怎么办,请教,怎样使得工作目录为defaultroot,当然filename
用绝对路径是可以的,但这样移到其他机子上都要改也太丑了。
如有其他办法可用相对于defaultroot的路径建立文件,也请告知,感激不尽。
期盼答复
|
再tomcat servlet下可以用如下语句
得到你的工作目录路径。
this.getServletContext().getRealPath("")
但在jboss3.0.0中是不行的。
祝好运。
得到你的工作目录路径。
this.getServletContext().getRealPath("")
但在jboss3.0.0中是不行的。
祝好运。
|
String filePath=this.getServletContext().getRealPath("/");
getRealPath用法如下:
===========================================
getRealPath
public java.lang.String getRealPath(java.lang.String path) Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext.. The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).Parameters:path - a String specifying a virtual pathReturns:a String specifying the real path, or null if the translation cannot be performed
getRealPath用法如下:
===========================================
getRealPath
public java.lang.String getRealPath(java.lang.String path) Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext.. The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).Parameters:path - a String specifying a virtual pathReturns:a String specifying the real path, or null if the translation cannot be performed