I have a web application that is supposed to read a shapefile from a user's disk. I use a MultipartFile
class (https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html) to upload it. As far as I understand, it is not possible to recover a file path from the MultipartFile
. It causes a problem for me because I use the following code to interpret the shapefile content (I attach the constructor which is crucial):
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);
}
}
The shapefile needs the File
in the constructor and the File
is strictly associated with an absolute path of a file. Therefore, I cannot use the MultipartFile
and create the File
in a memory (i.e. not in a file system) based on that. What I can do, is getting the InputStream
from the MultipartFile
, getInputStream()
or getBytes()
.
Is there a way to modify the Shapefile
constructor such that it accepts the InputStream
or a table of bytes? I would like to avoid creating a temporary file.
Thank you for suggestions.