请问下。我在Struts的action中想加载一个资源文件。怎么给出这个文件的路径。
资源文件的位置在:
工程名/WebRoot/report/资源文件。谢过先!

解决方案 »

  1.   

    URL   =   new   URL(this.getClass().getClassLoader().getResource("/file/1234.gif"));  
       
     有了URL   剩下的,你自己搞吧! 
      

  2.   

    路经可以用 如下的样子的相对路径../../report/资源文件 
      

  3.   

    可以直接这样写啊  /WebRoot/report/资源文件
      

  4.   

    Struts 本身不是有个资源文件吗,在Struts-config.xml中,我们一般直接使用那个资源(包括JSP,ACTION,FORMBEAN..)所以,你只要照着那个文件中的配置方法,路径改成你想要的就OK啦,good luck!
      

  5.   

    用序列化方法试试,Class.getReourceAsStream("//fileName");,这样可以读到你的资源文件。
      

  6.   

    String path = servlet.getServletContext().getRealPath("/report");
      

  7.   

    你的资源文件应该放到classpath中,就可以使用2楼的方法拿出了
      

  8.   

    如果按照3、4楼的做法,你观察一下,报错的原因是在TOMCAT_HOME\bin下面找不到你那个文件,在TOMCAT_HOME\bin下面怎么会有那个文件呢,解决办法是在strtus-config.xml的message-resources元素里面进行配置,在tomcat启动的时候全部读入内存
      

  9.   

    下面这段代吗支持../相对路径,路径是针对这个类的classpath而言的,兼容tomcat和weblogic,相对于web-info/classes你要到/WebRoot/report下应该是
    ../../report/资源文件public class ResourceLoader {
    private ResourceLoader() {
    } /**
     * 
     * Description:用来加载本类所在的ClassPath的路径下的资源文件,可以使用../符号来加载classpath外部的资源。
     * 
     * @param relativePath
     *            相当路径
     * @return URL对象
     */
    public static URL getResource(String relativePath) {
    URL resourceAbsoluteURL = null;
    try {
    relativePath = getStringForNum(ResourceLoader.class.getName()
    .split("\\.").length - 1, "../")
    + relativePath;
    if (relativePath.indexOf("../") < 0) {
    return ResourceLoader.class.getResource(relativePath);
    }
    String classPathAbsolutePath = getAbsolutePathOfClassLoaderClassPath();
    if (relativePath.substring(0, 1).equals("/")) {
    relativePath = relativePath.substring(1);
    }
    String wildcardString = relativePath.substring(0, relativePath
    .lastIndexOf("../") + 3);
    relativePath = relativePath.substring(relativePath
    .lastIndexOf("../") + 3);
    int containSum = containSum(wildcardString, "../");
    classPathAbsolutePath = cutLastString(classPathAbsolutePath, "/",
    containSum);
    String resourceAbsolutePath = classPathAbsolutePath + relativePath;
    resourceAbsoluteURL = new URL(resourceAbsolutePath);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return resourceAbsoluteURL;
    } public File getResourceFile(String relativePath) {
    try {
    return new File(getResource(relativePath).getFile());
    } catch (Exception e) {
    return null;
    }
    } /**
     * 取得本类所在的classpath下面的资源文件,可以使用../符号来加载classpath外部的资源。
     * 
     * @param relativePath
     *            相当路径
     * @return 输入流
     */
    public static InputStream getStream(String relativePath) {
    try {
    return getStream(getResource(relativePath));
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    } /**
     * 
     * Description:取得本类所在的classpath下面的Properties文件,可以使用../符号来加载classpath外部的资源。
     * 
     * @param resource
     *            相当路径
     * @return Properties 对象
     */
    public static Properties getProperties(String resource) {
    Properties properties = new Properties();
    InputStream in = null;
    try {
    in = getStream(resource);
    properties.load(in);
    return properties;
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    } finally {
    try {
    in.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    } /***************************************************************************
     * 得到当前类所在的ClassPath的绝对路径
     * 
     * @return 绝对路径
     */
    private static String getAbsolutePathOfClassLoaderClassPath() {
    return ResourceLoader.class.getResource("").toString();
    } /**
     * 计算字符串source包含dest的数目
     * 
     * @param source
     * @param dest
     * @return source中包含dest的数目
     */
    private static int containSum(String source, String dest) {
    int containSum = 0;
    int destLength = dest.length();
    while (source.indexOf(dest) >= 0) {
    containSum = containSum + 1;
    source = source.substring(destLength);
    }
    return containSum;
    } /**
     * 
     * Description:通过url取得流
     * 
     * @param url
     * @return
     * @throws IOException
     */
    private static InputStream getStream(URL url) {
    try {
    if (url != null)
    return url.openStream();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return null;
    } /**
     * 字符串source从后向前去掉num个字符串dest
     * 
     * @param source
     * @param dest
     * @param num
     * @return
     */
    private static String cutLastString(String source, String dest, int num) {
    for (int i = 0; i < num; i++)
    source = source.substring(0, source.lastIndexOf(dest, source
    .length() - 2) + 1);
    return source;
    } /**
     * 
     * Description:将指定字符串str进行num次连接
     * 
     * @param num
     * @param str
     * @return
     */
    private static String getStringForNum(int num, String str) {
    String ret = "";
    for (; num > 0; num--)
    ret += str;
    return ret;
    } public static void main(String[] args) {
    System.out.println(ResourceLoader.getResource("../web.xml"));
    }
    }