我需要从不在类路径上的绝对路径提供图像文件。当我使用Assets.at(path, file)
时,它只会在/assets
内搜索。我已将url映射到控制器函数,如下所示:
public static Action<AnyContent> getImage(String imageId) {
String path = PICTURE_UPLOAD_DIR; // here this path is absolute
String file = imageId + ".png";
return Assets.at(path, file);
}
我该如何做到这一点?
注意:使用Assets
投放图像的原因是因为自动触发功能可以轻松发送未经修改的http 304。似乎没有自动触发功能可以独立于Assets
答案 0 :(得分:1)
Assets.at()仅适用于在构建时添加到类路径的资产。 请参阅:https://www.playframework.com/documentation/2.4.x/Assets
解决方案是以byte []的形式从磁盘读取文件,然后在响应正文中返回byte []。
将图像转换为byte [] (此解决方案仅适用于小文件,大型文件可查看流):
private static Promise<byte[]> toBytes(final File file) {
return Promise.promise(new Function0<byte[]>() {
@Override
public byte[] apply() throws Throwable {
byte[] buffer = new byte[1024];
ByteArrayOutputStream os = new ByteArrayOutputStream();
FileInputStream is = new FileInputStream(file);
for (int readNum; (readNum = is.read(buffer)) != -1;) {
os.write(buffer, 0, readNum);
}
return os.toByteArray();
}
});
}
使用toBytes()来提供图像的控制器:
public static Promise<Result> img() {
//path is sent as JSON in request body
JsonNode path = request().body().asJson();
Logger.debug("path -> " + path.get("path").asText());
Path p = Paths.get(path.get("path").asText());
File file = new File(path.get("path").asText());
try {
response().setHeader("Content-Type", Files.probeContentType(p));
} catch (IOException e) {
Logger.error("BUMMER!", e);
return Promise.promise(new Function0<Result>() {
@Override
public Result apply() throws Throwable {
return badRequest();
}
});
}
return toBytes(file).map(new Function<byte[], Result>() {
@Override
public Result apply(byte[] bytes) throws Throwable {
return ok(bytes);
}
}).recover(new Function<Throwable, Result>() {
@Override
public Result apply(Throwable t) throws Throwable {
return badRequest(t.getMessage());
}
});
}
路线:
POST /img controllers.YourControllerName.img()
(不添加Date或Last-Modified标头,因为如果使用ETag标头则不需要它们):
获取文件的SHA1:
private static Promise<String> toSHA1(final byte[] bytes) {
return Promise.promise(new Function0<String>() {
@Override
public String apply() throws Throwable {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] digestResult = digest.digest(bytes);
String hexResult = "";
for (int i = 0; i < digestResult.length; i++) {
hexResult += Integer.toString(( bytes[i] & 0xff ) + 0x100, 16).substring(1);
}
return hexResult;
}
});
}
设置ETag标头:
private static boolean setETagHeaders(String etag, String mime) {
response().setHeader("Cache-Control", "no-cache");
response().setHeader("ETag", "\"" + etag + "\"");
boolean ifNoneMatch = false;
if (request().hasHeader(IF_NONE_MATCH)) {
String header = request().getHeader(IF_NONE_MATCH);
//removing ""
if (!etag.equals(header.substring(1, header.length() - 1))) {
response().setHeader(CONTENT_TYPE, mime);
}
ifNoneMatch = true;
} else {
response().setHeader(CONTENT_TYPE, mime);
}
return ifNoneMatch;
}
支持ETag的控制器:
public static Promise<Result> img() {
//path is sent as JSON in request body
JsonNode path = request().body().asJson();
Logger.debug("path -> " + path.get("path").asText());
Path p = Paths.get(path.get("path").asText());
File file = new File(path.get("path").asText());
final String mime;
try {
mime = Files.probeContentType(p);
} catch (IOException e) {
Logger.error("BUMMER!", e);
return Promise.promise(new Function0<Result>() {
@Override
public Result apply() throws Throwable {
return badRequest();
}
});
}
return toBytes(file).flatMap(new Function<byte[], Promise<Result>>() {
@Override
public Promise<Result> apply(final byte[] bytes) throws Throwable {
return toSHA1(bytes).map(new Function<String, Result>() {
@Override
public Result apply(String sha1) throws Throwable {
if (setETagHeaders(sha1, mime)) {
return status(304);
}
return ok(bytes);
}
});
}
}).recover(new Function<Throwable, Result>() {
@Override
public Result apply(Throwable t) throws Throwable {
return badRequest(t.getMessage());
}
});
}
一些缺点(总是有一个但是):
答案 1 :(得分:1)
我设法以更简单的方式解决了这个问题:
public static Result image(String image) {
String basePath = "/opt/myapp/images";
Path path = Paths.get(basePath + File.separator + image);
Logger.info("External image::" + path);
File file = path.toFile();
if(file.exists()) {
return ok(file);
} else {
String fallbackImage = "/assets/images/myimage.jpg";
return redirect(fallbackImage);
}
}
路线示例:
GET /image/:file controllers.ExternalImagesController.image(file: String)
对于大图像文件,您可以使用流式传输。 官方文档可以帮助您。