Last active
August 29, 2015 14:25
-
-
Save jonashackt/ccdd10b512e210bbd4f3 to your computer and use it in GitHub Desktop.
Loading Files SpringBoot-Fatjar with Java8 NIO.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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