问题描述
我的 jar 中有 shapefile,它部署在 OpenShift 中。我可以通过使用“File sourceFile = new ClassPathResource("src/main/resources/CountryBoundaries.shp).getFile();”从我的本地环境访问这些文件。但是,在 OpenShift 中,我的服务抱怨它不能找不到。我已经尝试过 InputStream,但我的 IDE 出现错误,因为该文件不是文本文件。以下是我的代码:
File sourceFile = new ClassPathResource("src/main/resources/CountryBoundaries.shp").getFile();
FileDataStore store = FileDataStoreFinder.getDataStore(sourceFile);
featureSource = store.getFeatureSource();
GeometryDescriptor geomDesc = featureSource.getSchema().getGeometryDescriptor();
attrName = geomDesc.getLocalName();
我错过了什么?我将不胜感激,因为我已经为此苦苦挣扎了一段时间。谢谢。
解决方法
我通过将 URL 更改为 URI 然后执行 toURL() 找到了解决方案。见下文:
URI sourceFile = new ClassPathResource("PoliticalSubdivisionBoundaries.shp").getURI();
FileDataStore store = FileDataStoreFinder.getDataStore(sourceFile.toURL());
featureSource = store.getFeatureSource();
GeometryDescriptor geomDesc = featureSource.getSchema().getGeometryDescriptor();
attrName = geomDesc.getLocalName();
希望这对未来的人有所帮助。 Shapefiles 很复杂,不能像使用 inputStream 等通常的方式访问。干杯!