我正在尝试使用akka-http提供图像,但在浏览器中不断收到有关MIME类型的错误:
Resource interpreted as Document but transferred with MIME type image/png: "http://localhost/banners/ci.png".
这是服务器端代码:
pathPrefix("banners"){
get {
entity(as[HttpRequest]) { requestData =>
complete {
val fullPath = requestData.uri.path.toString match {
case _ => Paths.get(workingDirectory + requestData.uri.path.toString)
}
val contentType = getExtensions(fullPath.toString) match {
case "jpg" => ContentType(MediaTypes.`image/jpeg`)
case "png" => ContentType(MediaTypes.`image/png`)
case "html" => ContentTypes.`text/html(UTF-8)`
}
val h = RawHeader("Access-Control-Allow-Origin","*")
val byteArray = Files.readAllBytes(fullPath)
HttpResponse(OK, entity = HttpEntity(contentType,byteArray)).withHeaders(h)
}
}
}
} ~
以下是从chrome开发者控制台复制的响应标头:
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Server: akka-http/10.0.7
Date: Fri, 26 May 2017 13:08:04 GMT
Content-Type: image/png
Content-Length: 581
任何想法我该如何解决?