有没有办法Accept
为图像处理选择最好的 mimetype?
我有一个资源看起来像这样。
@GET
@Produces({"image/jpeg", "image/png"})
public Response readResizedImage(
@Context Request request,
@Context HttpHeaders httpHeaders,
@QueryParam("width") final int width,
@QueryParam("height") final int height) {
final List<Variant> variants = Variant.mediaTypes(
new MediaType("image", "jpeg"), new MediaType("image", "png")).build();
// Why on earth variants is empty?
if (!variants.isEmpty()) {
final Variant variant = request.selectVariant(variants);
LOGGER.log(Level.INFO, "{0}", variant.getMediaType().toString());
}
final List<MediaType> acceptableMediaTypes =
httpHeaders.getAcceptableMediaTypes();
for (MediaType acceptableMediaType : acceptableMediaTypes) {
LOGGER.log(Level.INFO, "acceptableMediaType:{0}/{1}",
new Object[]{acceptableMediaType.getType(),
acceptableMediaType.getSubtype()});
}
return null;
}
我用以下命令尝试了这个资源。
$ curl -v -H "Accept: */*" \
-H "Accept: image/*;q=0.2" \
-H "Accept: image/jpeg;q=0.5" \
-H "Accept: image/png;q=1.0" \
http://.............
和服务器打印
acceptableMediaType:image/png
acceptableMediaType:*/*
acceptableMediaType:image/jpeg
acceptableMediaType:image/*
问题:
如何选择正确的(非通配符)mime 类型?
我必须有一个用于操作图像字节以重新调整大小。