Scala 如何在游戏中发送多部分/相关请求
我正在将scala 2.11.6、playframework 2.3.8与开放式ocr(tesseract)集成,它需要发送多部分/相关数据 我正在尝试这样做,手动生成多部分请求Scala 如何在游戏中发送多部分/相关请求,scala,http,tesseract,playframework-2.3,multipart,Scala,Http,Tesseract,Playframework 2.3,Multipart,我正在将scala 2.11.6、playframework 2.3.8与开放式ocr(tesseract)集成,它需要发送多部分/相关数据 我正在尝试这样做,手动生成多部分请求 val postBody = s"""--separator-- |Content-Type: application/json; | | { "engine": "tesseract" }
val postBody = s"""--separator--
|Content-Type: application/json;
|
| { "engine": "tesseract" }
|
|--separator--
| Content-Type: image/png;
|
| ${Base64.getEncoder().encodeToString(image)}
|--separator--
""".stripMargin
val parseResult = WS.
url("http://127.0.0.1:9292/ocr-file-upload").
withMethod("POST").
withHeaders(
"Content-Type" -> "multipart/related",
"boundary" -> "separator").
withBody(postBody).
execute()
但它不起作用。Open ocr无法读取请求的标题
我如何才能做到这一点?我使用的解决方案是使用ning Multipart response generator手动生成body 对于旧版本1.8.0:
import com.ning.http.client.FluentCaseInsensitiveStringsMap
import com.ning.http.client.multipart._
...
// Step 1. Generating tesseract json configuration
val json = new StringPart("config", """{ "engine": "tesseract" }""", "utf-8")
json.setContentType("application/json")
// Step 2. Generating file part
val filePart = new FilePart("file", new ByteArrayPartSource("image.png", image), "image/png", null)
// Step 3. Build up the Multiparts
val reqE = new MultipartRequestEntity(
Array(filePart, json),
new FluentCaseInsensitiveStringsMap().add("Content-Type", "multipart/related")
)
// Step 3.1. Streaming result to byte array
val bos = new ByteArrayOutputStream()
reqE.writeRequest(bos)
// Step 4. Performing WS request upload request
val parseResult = WS
.url("http://127.0.0.1:9292/ocr-file-upload")
.withHeaders("Content-Type" -> reqE.getContentType()).
post(bos.toByteArray());
// Step 5. Mapping result to parseResult
parseResult.map(_.body)
import com.ning.http.client.FluentCaseInsensitiveStringsMap
import com.ning.http.client.multipart._
...
// Step 1. Generating tesseract json configuration
val json = new StringPart("config", """{ "engine": "tesseract" }""", "application/json", Charset.forName("utf-8"))
// Step 2. Generating file part
val filePart = new ByteArrayPart("image.png", scaledImage, "image/png")
// Step 3. Build up the Multiparts
val reqE = MultipartUtils.newMultipartBody(
util.Arrays.asList(filePart, json),
new FluentCaseInsensitiveStringsMap().add("Content-Type", "multipart/related")
)
// Step 3.1. Streaming result to byte array
val bos = ByteBuffer.allocate(scaledImage.length + 1024)
reqE.read(bos)
// Step 4. Performing WS request upload request
val parseResult = WS
.url("http://127.0.0.1:9292/ocr-file-upload")
.withHeaders("Content-Type" -> reqE.getContentType())
.post(bos.array());
// Step 5. Mapping result to parseResult
parseResult.map(_.body)
最新版本1.9.31:
import com.ning.http.client.FluentCaseInsensitiveStringsMap
import com.ning.http.client.multipart._
...
// Step 1. Generating tesseract json configuration
val json = new StringPart("config", """{ "engine": "tesseract" }""", "utf-8")
json.setContentType("application/json")
// Step 2. Generating file part
val filePart = new FilePart("file", new ByteArrayPartSource("image.png", image), "image/png", null)
// Step 3. Build up the Multiparts
val reqE = new MultipartRequestEntity(
Array(filePart, json),
new FluentCaseInsensitiveStringsMap().add("Content-Type", "multipart/related")
)
// Step 3.1. Streaming result to byte array
val bos = new ByteArrayOutputStream()
reqE.writeRequest(bos)
// Step 4. Performing WS request upload request
val parseResult = WS
.url("http://127.0.0.1:9292/ocr-file-upload")
.withHeaders("Content-Type" -> reqE.getContentType()).
post(bos.toByteArray());
// Step 5. Mapping result to parseResult
parseResult.map(_.body)
import com.ning.http.client.FluentCaseInsensitiveStringsMap
import com.ning.http.client.multipart._
...
// Step 1. Generating tesseract json configuration
val json = new StringPart("config", """{ "engine": "tesseract" }""", "application/json", Charset.forName("utf-8"))
// Step 2. Generating file part
val filePart = new ByteArrayPart("image.png", scaledImage, "image/png")
// Step 3. Build up the Multiparts
val reqE = MultipartUtils.newMultipartBody(
util.Arrays.asList(filePart, json),
new FluentCaseInsensitiveStringsMap().add("Content-Type", "multipart/related")
)
// Step 3.1. Streaming result to byte array
val bos = ByteBuffer.allocate(scaledImage.length + 1024)
reqE.read(bos)
// Step 4. Performing WS request upload request
val parseResult = WS
.url("http://127.0.0.1:9292/ocr-file-upload")
.withHeaders("Content-Type" -> reqE.getContentType())
.post(bos.array());
// Step 5. Mapping result to parseResult
parseResult.map(_.body)
什么不起作用?你能说得更准确些吗?对不起,我的错。Open ocr无法读取请求的标题感谢您。你知道如何解析请求吗?