无法获得java中绝对路径的相对路径

时间:2015-02-27 08:01:47

标签: java

我将图像的路径提供给属性文件,并在我的pdfGenerating类中获取Image类的路径。像:

properties.load(DownloadPdf.class.getResourceAsStream("/resource.properties"));
System.out.println("--properties----"+properties);
System.out.println("-path-"+properties.getProperty("logoPath"));
//String path = properties.getProperty("logoPath");
//URL uri = Paths.get(path).toUri().toURL();
System.out.println("---path-"+properties.getProperty("logoPath"));
Image image = Image.getInstance(properties.getProperty("logoPath"));

这里我的logopath是:

path----------------C:/Users/Home/Documents/workspace-gofindo/.metadata/.plugins/org.eclipse.wst.server.core/.....

但我正在

中找到路径
Image.getInstance(properties.getProperty("logopath"));

C:\Users\Home\Documents\workspace-gofindo\.metadata\.plugins\org.

我尝试过使用replace和replaceAll()方法将'\'转换为'/' 再次将图像类转换为'\'。 如何将我在属性文件中指定的绝对路径完全放入Image.getInstance()方法

1 个答案:

答案 0 :(得分:0)

我已经重新编写了我可以读取文件路径的方式,如下所示。

        // Load path of the Image from Properties file
        Properties properties = new Properties();
        properties.load(this.getClass().getResourceAsStream(
                "/resource.properties"));
        // To get the Path of the Context
        String contextPath = request.getContextPath();
        String split_path[] = contextPath.split("/");
        contextPath = request.getRealPath(split_path[0]);
        Image image = Image.getInstance(contextPath
                    + properties.getProperty("logoPath"));
        image.scaleAbsolute(120f, 60f);// image width,height