【发布时间】:2019-12-27 10:07:58
【问题描述】:
我有一个应该从用户磁盘读取 shapefile 的 Web 应用程序。我使用MultipartFile 类(https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html)来上传它。据我了解,无法从MultipartFile 恢复文件路径。它给我带来了一个问题,因为我使用以下代码来解释 shapefile 内容(我附加了至关重要的构造函数):
private ShapeFile(final File srcFile) {
try {
final Map<String, Serializable> params = ImmutableMap.of("url", srcFile.toURI().toURL());
dataStore = new ShapefileDataStoreFactory().createDataStore(params);
final String typeName = dataStore.getTypeNames()[0];
featureType = dataStore.getSchema(typeName);
featureSource = dataStore.getFeatureSource(typeName);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
shapefile 在构造函数中需要File,而File 与文件的绝对路径严格关联。因此,我不能使用MultipartFile 并基于此在内存中(即不在文件系统中)创建File。我能做的就是从MultipartFile、getInputStream() 或getBytes() 获取InputStream。
有没有办法修改Shapefile 构造函数,使其接受InputStream 或字节表?我想避免创建临时文件。
感谢您的建议。
【问题讨论】:
标签: java spring shapefile geotools