简体   繁体   English

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

[英]unable to get relative path to absolute path in java

Im giving the path of the image to properties file and getting the path to Image class in my pdfGenerating class. 我将图像的路径提供给属性文件,并在我的pdfGenerating类中获取Image类的路径。 like: 喜欢:

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"));

Here my logopath is: 这里我的logopath是:

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

But i'm getting the path in 但我正在进入这条道路

Image.getInstance(properties.getProperty("logopath"));

like 喜欢

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

I have tried with replace and replaceAll() methods to convert '\\' to '/' again the image class converting into '\\'. 我尝试使用replace和replaceAll()方法将'\\'再次转换为'/',将图像类转换为'\\'。 How to get my absolute path which i have specified in properties file exactly into Image.getInstance() method 如何将我在属性文件中指定的绝对路径完全放入Image.getInstance()方法中

I have remodified the way i can read the path of the file like below. 我已经重新编写了我可以读取文件路径的方式,如下所示。

        // 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM