我正在尝试使用multipart / form-data将字节数组上传到远程WS!使用Scala的框架。我的代码是:
//create byte array from file
val myFile = new File(pathName)
val in = new FileInputStream(myFile)
val myByteArray = new Array[Byte](audioFile.length.toInt)
in.read(audioByteArray)
in.close()
// create parts
val langPart = new StringPart("lang", "pt")
val taskPart = new StringPart("task","echo")
val audioPart = new ByteArrayPart("sbytes", "myFilename", myByteArray, "default/binary", "UTF-8")
val client: AsyncHttpClient = WS.client
val request = client.preparePost(RemoteWS)
.addHeader("Content-Type", "multipart/form-data")
.addBodyPart(audioPart)
.addBodyPart(taskPart)
.addBodyPart(langPart).build()
val result = Promise[Response]()
// execute request
client.executeRequest(request, new AsyncCompletionHandler[AHCResponse]{
override def onCompleted(response: AHCResponse): AHCResponse = {
result.success(Response(response))
response
}
override def onThrowable(t: Throwable) {
result.failure(t)
}
})
// handle async response
result.future.map(result =>{
Logger.debug("response: " + result.getAHCResponse.getResponseBody("UTF-8"))
})
每次执行此请求时,都会抛出此异常:
java.io.IOException:无法在通道上编写java.nio.channels.SocketChannel
远程服务器没问题,我可以使用例如Postman来发出成功的请求。
我正在使用Play Framework:
播放使用Scala 2.10.3构建的2.2.3(运行Java 1.8.0_05)
AsyncHttpClient的版本:
com.ning:异步-HTTP客户端:1.7.18
欢迎任何帮助!