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 发出成功的请求。

我正在使用播放框架:

玩用 Scala 2.10.3 构建的 2.2.3(运行 Java 1.8.0_05)

AsyncHttpClient 版本:

com.ning:async-http-client:1.7.18

欢迎任何帮助!

4

0 回答 0