groovy 中的 http-builder 在服务器中上传多个文件

http-builder in groovy uploading multiple files in server

我正在尝试使用 http builder-groovy.The 在服务器中上传多个文件,下面的代码没有 works.Getting 错误“请求实体太大”。我已经导入了所有包并定义了所有变量。

有没有其他方法可以使用 restclient-groovy?

谁能给个理由?

提前致谢。

def file = new File("resources/IMG.JPG")
def file1 = new File("resources/aa.json")

http = new HTTPBuilder( url )

http.request (POST, JSON) { multipartRequest ->

      uri.path = '/server/upload'
      uri.query = [param1:value, param2:value, param3:value, param4:value]

      requestContentType = 'multipart/form-data'

      MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
      mpe.addPart( "jpeg", new FileBody(( File ) file , 'image/jpeg' ))
      mpe.addPart( "json", new FileBody(( File ) file1 , 'application/json' ))

      multipartRequest.setEntity(mpe)

      response.success = { resp, json->
          println "POST response status: ${resp.statusLine}"
          println "Query response: ${json}"
      }

      response.failure = {  resp ->
          println "POST response statusline: ${resp.statusLine}"
      }
}

根据维基百科:

413 Request Entity Too Large

The request is larger than the server is willing or able to process.

检查你的服务器

您实际上可以使用 RestAssured 框架轻松地形成多部分请求。

下面是一个例子,

@Test
public void multiExample()
{
    given().
        multiPart("image", new File("resources/a.jpg"), "image/jpeg").
        multiPart("lgdata", new File("resources/myfile.json"), "application/json").
    expect().
        body("result", is("OK")).
    when().
        post(url);

}