Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在java中获取绝对路径的相对路径_Java - Fatal编程技术网

无法在java中获取绝对路径的相对路径

无法在java中获取绝对路径的相对路径,java,Java,我给出了属性文件的图像路径,并在我的pdfGenerating类中获得了图像类的路径。比如: properties.load(DownloadPdf.class.getResourceAsStream("/resource.properties")); System.out.println("--properties----"+properties); System.out.println("-path-"+properties.getProperty("logoPath")); //Strin

我给出了属性文件的图像路径,并在我的pdfGenerating类中获得了图像类的路径。比如:

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"));
我的登录路径如下:

path----------------C:/Users/Home/Documents/workspace-gofindo/.metadata/.plugins/org.eclipse.wst.server.core/.....
但我正在把路弄进去

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

我已尝试使用replace和replaceAll方法将“\”转换为“/” 再次将图像类转换为“\”。
如何将我在属性文件中指定的绝对路径精确地转换为Image.getInstance方法

我重新修改了读取文件路径的方式,如下所示

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

您知道\是windows上的正确字符,对吗?/也支持作为bonusAnd\vs/与绝对路径和相对路径无关。这是哪个图像类?此处图像类对象采用的路径类似于c:\Users\Home\。。。。并表示找不到指定路径并引发FileNotFoundException。当我将其硬编码为c:/Users/Home时。。。我正在将指定路径中的图像导入pdf文档。Im使用com.itextpdf.text.Image class@Ravi Thapliyal
        // 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