网上搜的方法都不好用,不是在jsp和servelet中,是在后台类中。
大家帮帮忙。

解决方案 »

  1.   

    String path=request.getRealPath("/");
    System.out.println(path);
      

  2.   

    在类中也可以获取到servelet,剩下的就不都一样了吗
      

  3.   

    String path =getServlet().getServletContext.getRealPath("/"); 
      

  4.   

    物理路径?System.getProperty( "user.dir" )
      

  5.   

    String path = getServletConfig().getServletContext().getRealPath("");
    這個方式是抓到你WebApp所在的路徑
      

  6.   

     楼上诸公!getServletConfig()这是哪个类中的??直接拿来用?
      

  7.   

    上面的都是没看清题,都是只能在WEB应用中用的方法。另外TOMCAT的绝对路径一般都是固定的,LZ直接引用不行吗?
      

  8.   

    如果不想用 Servlet 就用 5 楼的方法,那个可以获得 tomcat/bin 的路径。
      

  9.   

    5楼正解,不过还有一种办法,如果用wepapp发布的话可以通过这种方法/**
     * 得到本类物理路径所在文件夹
     * @return
     */
    private String getClassPath(){ 
            String strClassName = getClass().getName(); 
            String strPackageName = ""; 
            if(getClass().getPackage() != null) { 
                strPackageName = getClass().getPackage().getName(); 
            } 
            String strClassFileName = ""; 
            if(!"".equals(strPackageName)){
                strClassFileName = strClassName.substring(strPackageName.length() + 1,strClassName.length()); 
            } 
            else { 
                strClassFileName = strClassName; 
            } 
            URL url = null; 
            url = getClass().getResource(strClassFileName + ".class"); 
            String strURL = url.toString();
            String middleString = System.getProperty("file.separator"); // 取得操作系统路径分割符
            strURL = strURL.substring(strURL.indexOf( "/" ) + 1,strURL.lastIndexOf( "/" ));
            return strURL; 
        }
      

  10.   

    直接引用servlet获取的路径啥!不用这么麻烦!
      

  11.   

    我最喜欢玩路径问题了.^_^
    一般来说
    java.io.tmpdir不会被改变
    方法1.
    System.out.println(new File(System.getProperty("java.io.tmpdir")).getParentFile().getAbsolutePath());
    方法2.取得Tomcat 的类,然后利用我如下文章的方法
    http://blog.csdn.net/sunyujia/archive/2008/09/21/2957481.aspx
    方法3.
    在启动tomcat 的时候将%CD% 传递到jvm里面
      

  12.   

    另外说一句利用 批处理的启动目录是不安全的也就是new File(.) 这样是不安全的,遇到像我这样喜欢玩批处理的,就不太好了.呵呵.
      

  13.   

    package com.sterning;import java.io.File;import javax.servlet.ServletContext;import org.apache.commons.io.FileUtils;
    import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionSupport;public class StrutsFileUpload extends ActionSupport implements
    ServletContextAware { private File upload;// 实际上传文件 private String uploadContentType; // 文件的内容类型 private String uploadFileName; // 上传文件名 private String fileCaption;// 上传文件时的备注 private ServletContext context; public String execute() throws Exception { try {

    String targetDirectory = context.getRealPath("/upload");
    String targetFileName = uploadFileName;
    File target = new File(targetDirectory, targetFileName);
    FileUtils.copyFile(upload, target);

    setUploadFileName(target.getPath());//保存文件的存放路径
    } catch (Exception e) { addActionError(e.getMessage()); return INPUT;
    } return SUCCESS; } public String getFileCaption() {
    return fileCaption;
    } public void setFileCaption(String fileCaption) {
    this.fileCaption = fileCaption;
    } public File getUpload() {
    return upload;
    } public void setUpload(File upload) {
    this.upload = upload;
    } public String getUploadContentType() {
    return uploadContentType;
    } public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
    } public String getUploadFileName() {
    return uploadFileName;
    } public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
    } public void setServletContext(ServletContext context) {
    this.context = context;
    }}
      

  14.   

    只是在我联系的时候用到过的,也是别人写的,给你弄过来了,你就直接在后台可以显示,<%@ page language="java" contentType="text/html; charset=GB2312"%>   
    <%@ taglib prefix="s" uri="/struts-tags" %>   
    <html>
    <head>
    <title>文件上传示例</title>
    <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
    type="text/css" /> </head> <body> <s:actionerror />
    <s:fielderror />
    <s:form action="doUpload" method="POST" enctype="multipart/form-data">
    <tr>
    <td colspan="2">
    <h1>
    文件上传示例
    </h1>
    </td>
    </tr> <s:file name="upload" label="上传的文件" />
    <s:textfield name="fileCaption" label="备注" />
    <s:submit value="上   传"/>
    </s:form>
    </body>
    </html><%@ page language="java" contentType="text/html; charset=GB2312"%>  
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <html>
    <head>
    <title>上传成功</title>
    <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
    type="text/css" />
    </head> <body>
    <table class="wwFormTable">
    <tr> <td colspan="2">
    <h1>
    上传成功
    </h1>
    </td>
    </tr> <tr>
    <td class="tdLabel">
    <label for="doUpload_upload" class="label">
    内容类型:
    </label>
    </td>
    <td>
    <s:property value="uploadContentType" />
    </td>
    </tr> <tr>
    <td class="tdLabel">
    <label for="doUpload_upload" class="label">
    文件路径:
    </label>
    </td>
    <td>
    <s:property value="uploadFileName" />
    </td>
    </tr>
    <tr>
    <td class="tdLabel">
    <label for="doUpload_upload" class="label">
    临时文件:
    </label>
    </td>
    <td>
    <s:property value="upload" />
    </td>
    </tr> <tr>
    <td class="tdLabel">
    <label for="doUpload_upload" class="label">
    备注:
    </label>
    </td>
    <td>
    <s:property value="fileCaption" />
    </td>
    </tr>
    </table> </body>
    </html>
    运行看看,不行,我这里有能运行的包,你要就给你传传
      

  15.   

    大部分人没看懂题目,我现在已经解决了,基本方法同chris9999 的答复,因为我不用servelet而很多人给的方法都是用servelet,没办法,把分给看懂题的人吧,虽然我是自己解决的,呵呵。
      

  16.   

    //获得上下文   
    DirContext dirContext = DirContextURLStreamHandler.get();   
    //查询templates这个目录资源   
    Object obj = dirContext.lookup("/templates");   
    FileDirContext fdc;   
    String webrootPath;   
    // 判断取出来的对象是不是FileDirContext   
    // 也就是判断这个资源名对应的资源是不是目录资源   
    if (obj instanceof FileDirContext) {   
        fdc = (FileDirContext) obj;   
        // 获取web项目根目录的绝对路径   
        webrootPath = fdc.getDocBase();   
    }   
      
    String templatesPath = webrootPath + "/tempaltes";  楼上很多答案都是牛头不对马嘴的,上面代码是网上查找的,具体结果还没测试 :)
      

  17.   

    补充:
    参考文章的地址是http://mysun.javaeye.com/blog/453907
      

  18.   

    继续补充:http://blog.csdn.net/sunyujia/archive/2008/09/21/2957481.aspx