Skip to content

Instantly share code, notes, and snippets.

@russellhoff
Created January 28, 2019 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save russellhoff/710241b6222c8e0148ff68092a55b5f5 to your computer and use it in GitHub Desktop.
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
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