How to return image/png media type response in Spr

2019-08-06 13:59发布

问题:

I want to return image/PNG as response to calling URL. How can I do it in Spray?

回答1:

This may help:

import java.io._
import spray.http._
import MediaTypes._

sender ! HttpResponse(entity = HttpEntity(`image/png`, HttpData(new File("my.png"))))

See also, HttpMessage, HttpEntity, HttpData and MediaTypes. You can do same for HttpRequest as well. You can use Array[Byte] or ByteString instead of File. Checked for Spray 1.3.x.



标签: spray