1

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 フレームワークを使用しています:

Scala 2.10.3 で構築された play 2.2.3 (Java 1.8.0_05 を実行)

AsyncHttpClient のバージョン:

com.ning:async-http-client:1.7.18

どんな助けでも大歓迎です!

4

0 に答える 0