我最终通过为我的所有静态文件生成一条获取路径来解决这个问题。完全有可能有更简单的方法直接在 spark 中执行此操作,但编写此代码所花费的时间比了解spark.resource
包的详细信息要少。
首先,我定义了一些帮助函数来让我遍历特定资源目录中的所有文件(需要 Java 8):
/**
* Find all resources within a particular resource directory
* @param root base resource directory. Should omit the leading / (e.g. "" for all resources)
* @param fn Function called for each resource with a Path corresponding to root and a relative path to the resource.
* @throws URISyntaxException
*/
public static void findResources(String root,BiConsumer<Path,Path> fn) throws URISyntaxException {
ClassLoader cl = Main.class.getClassLoader();
URL url = cl.getResource(root);
assert "file".equals(url.getProtocol());
logger.debug("Static files loaded from {}",root);
Path p = Paths.get(url.toURI());
findAllFiles(p, (path) -> fn.accept(p,p.relativize(path)) );
}
/**
* Recursively search over a directory, running the specified function for every regular file.
* @param root Root directory
* @param fn Function that gets passed a Path for each regular file
*/
private static void findAllFiles(Path root, Consumer<Path> fn) {
try( DirectoryStream<Path> directoryStream = Files.newDirectoryStream(root)) {
for(Path path : directoryStream) {
if(Files.isDirectory(path)) {
findAllFiles(path, fn);
} else {
fn.accept(path);
}
}
} catch (IOException ex) {}
}
然后我用它为每个文件定义一个新的 GET 路由
String webroot = "/server1";
findResources("static", (root,path) -> {
String route = webroot+"/"+path;
String resourcePath = "/static/"+path.toString();
logger.debug("Mapping {} to {}",route, resourcePath);
get(webroot+"/"+path, (req,res) -> {
Files.copy(root.resolve(path), res.raw().getOutputStream());
AbstractFileResolvingResource resource = new ExternalResource(resourcePath);
String contentType = MimeType.fromResource(resource);
res.type(contentType );
return "";
} );
});