Skip to content

Instantly share code, notes, and snippets.

@jonashackt
Last active August 29, 2015 14:25
Show Gist options
  • Save jonashackt/ccdd10b512e210bbd4f3 to your computer and use it in GitHub Desktop.
Save jonashackt/ccdd10b512e210bbd4f3 to your computer and use it in GitHub Desktop.
Loading Files SpringBoot-Fatjar with Java8 NIO.2
public static InputStream readFileInClasspath2InputStream(String folderWithEndingSlash, String fileName) throws BusinessException {
InputStream inputStream = null;
try {
Path filePath = buildPath(folderWithEndingSlash, fileName);
inputStream = Files.newInputStream(filePath);
} catch (Exception exception) {
throw new BusinessException(ERROR_MSG_START + exception.getMessage(), exception);
}
return inputStream;
}
private static Path buildPath(String folderWithEndingSlash, String fileName) throws URISyntaxException, BusinessException, IOException {
URI fileUri = buildFileUri(folderWithEndingSlash, fileName);
Map<String, String> env = new HashMap<>();
env.put("create", "true");
FileSystems.newFileSystem(fileUri, env);
return Paths.get(fileUri);
}
private static URI buildFileUri(String folderWithEndingSlash, String fileName) throws URISyntaxException, BusinessException {
URL fileInClasspath = FileUtils.class.getClassLoader().getResource(folderWithEndingSlash + fileName);
if(fileInClasspath == null)
throw new BusinessException("Datei nicht gefunden!");
return fileInClasspath.toURI();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment