Created
January 28, 2019 13:18
-
-
Save russellhoff/710241b6222c8e0148ff68092a55b5f5 to your computer and use it in GitHub Desktop.
A helper class to load Files located under resources directory in any Spring Boot project
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
import java.io.File; | |
import java.util.Optional; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class SpringResourceFileLoader { | |
/** | |
* Obtiene un fichero desde el directorio resources | |
* @param pFilePath | |
* @return | |
*/ | |
public Optional<File> getFileFromResources(String pFilePath) { | |
try { | |
ClassLoader classLoader = getClass().getClassLoader(); | |
return Optional.ofNullable( | |
new File( | |
classLoader | |
.getResource(pFilePath) | |
.getFile())); | |
}catch(NullPointerException e) { | |
return Optional.empty(); | |
} | |
} | |
/** | |
* Obtiene un fichero de cualquier directorio. | |
* @param pFilePath | |
* @return | |
*/ | |
public Optional<File> getFileFromPath(String pFilePath){ | |
try { | |
return Optional.ofNullable( | |
new File(pFilePath)); | |
}catch(NullPointerException e) { | |
return Optional.empty(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment