Encoding groovy中用于多部分/表单数据的编码器函数

Encoding groovy中用于多部分/表单数据的编码器函数,encoding,groovy,multipartform-data,rest-client,httpbuilder,Encoding,Groovy,Multipartform Data,Rest Client,Httpbuilder,我需要使用jpeg图像和JSON文件作为内容形成一个“多部分/表单数据”REST请求。我一直坚持将“多部分/表单数据”编码为zip文件 有人能告诉我,如何使用groovy RESTClient实现这一点吗?我找不到与此相关的任何文档 可以在RESTClient扩展HTTPBuilder中看到HTTPBuilder有一个getEncoder方法,可用于添加专用编码器(带有类型和方法)。请参阅以下代码: import org.codehaus.groovy.runtime.MethodClosure

我需要使用jpeg图像和JSON文件作为内容形成一个“多部分/表单数据”REST请求。我一直坚持将“多部分/表单数据”编码为zip文件

有人能告诉我,如何使用groovy RESTClient实现这一点吗?我找不到与此相关的任何文档

可以在
RESTClient
扩展
HTTPBuilder
中看到
HTTPBuilder
有一个
getEncoder
方法,可用于添加专用编码器(带有类型和方法)。请参阅以下代码:

import org.codehaus.groovy.runtime.MethodClosure
import javax.ws.rs.core.MediaType

//this part adds a special encoder    
def client = new RESTClient('some host')
client.encoder.putAt(MediaType.MULTIPART_FORM_DATA, new MethodClosure(this, 'encodeMultiPart'))

//here is the method for the encoder added above
HttpEntity encodeMultiPart(MultipartBody body) {
    MultipartEntityBuilder.create()
    .addBinaryBody(
        'file', 
        body.file, 
        ContentType.MULTIPART_FORM_DATA, 
        body.filename
    ).build()
}

//here's how MultipartBody class looks:
class MultipartBody {
   InputStream file
   String filename
}

现在要创建一个多部分请求,您需要将
MultipartBody
的一个实例作为一个body参数传递给请求。

意识到这是一个老生常谈的问题,但可能对其他人有所帮助,尽管这个问题从初学者的角度回答了这个问题,但很难完全理解如何正确地重用上述所有内容

首先,对问题的最后评论指出:

试图错误地重复使用答案。它把上面的答案和来自美国的答案混为一谈

以上这些都是行不通的,我让它像这样工作:

def http = new RESTClient('http://localhost:8080')
http.encoder.putAt(MediaType.MULTIPART_FORM_DATA, new MethodClosure(this, 'encodeMultiPart'))
def body1 = new MultipartBody()   //This is that MultipartBody class in the first answer example not the one from your imports......
body1.file=file.getInputStream()
body1.filename=file.name
def response = http.put( path: url, body:body1, query:['query':action, ], requestContentType: 'multipart/form-data' )

您还有encodeMultiPart2和encodeMultiPart1,我认为这是一个误解,只是在这两种情况下都重用了该方法的1声明。。您不需要执行示例中的任何附件等。

编码器注册在以前的响应中非常混乱,下面是我的工作示例:

import org.apache.cxf.jaxrs.ext.multipart.Attachment
import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody
import org.apache.http.entity.ContentType
import org.apache.http.entity.mime.MultipartEntityBuilder
import javax.ws.rs.core.MediaType 

...

def filenameToUpload = "doggo.jpg"
def expectedRequestParamName = "file"

def static uploadFile() {
    // create attachment
    def fileToUpload = new File(filenameToUpload)
    def attachment = new Attachment(expectedRequestParamName, new ByteArrayInputStream(fileToUpload.getBytes()), new ContentDisposition("filename=" + filenameToUpload))
    def body = new MultipartBody(attachment)

    // create REST client
    def httpClient = new RESTClient('http://localhost:8080')

    // register encoder
    httpClient.encoder.putAt(MediaType.MULTIPART_FORM_DATA, customMultipartEncoder)

    // call REST
    httpClient.post(
        path: "upload",
        body: body,
        requestContentType: MediaType.MULTIPART_FORM_DATA)
}

// register multipart encoder
private def static customMultipartEncoder = { body ->
    def builder = MultipartEntityBuilder.create()

    body.allAttachments.collect {
        builder.addBinaryBody(
            it.contentId,
            it.dataHandler.inputStream,
            ContentType.MULTIPART_FORM_DATA,
            it.contentId) }

    return builder.build()
}

这是我正在使用的代码。::我收到一个错误“没有为请求内容类型多部分/表单数据找到编码器”我的回答有用吗?@opal:谢谢你的代码。但我对在多部分正文中添加两个文件(图像文件和json)作为单个输入流文件感到困惑。这应该是两个单独的请求。@Opal:我仍然遇到一些错误。抛出错误“找不到匹配的构造函数:FileUploadSpec$MultipartBody”请通过链接:()您可以使用上述代码创建一个带有示例的类吗?@TalyssondeCastro,您的意思是什么?
import org.apache.cxf.jaxrs.ext.multipart.Attachment
import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody
import org.apache.http.entity.ContentType
import org.apache.http.entity.mime.MultipartEntityBuilder
import javax.ws.rs.core.MediaType 

...

def filenameToUpload = "doggo.jpg"
def expectedRequestParamName = "file"

def static uploadFile() {
    // create attachment
    def fileToUpload = new File(filenameToUpload)
    def attachment = new Attachment(expectedRequestParamName, new ByteArrayInputStream(fileToUpload.getBytes()), new ContentDisposition("filename=" + filenameToUpload))
    def body = new MultipartBody(attachment)

    // create REST client
    def httpClient = new RESTClient('http://localhost:8080')

    // register encoder
    httpClient.encoder.putAt(MediaType.MULTIPART_FORM_DATA, customMultipartEncoder)

    // call REST
    httpClient.post(
        path: "upload",
        body: body,
        requestContentType: MediaType.MULTIPART_FORM_DATA)
}

// register multipart encoder
private def static customMultipartEncoder = { body ->
    def builder = MultipartEntityBuilder.create()

    body.allAttachments.collect {
        builder.addBinaryBody(
            it.contentId,
            it.dataHandler.inputStream,
            ContentType.MULTIPART_FORM_DATA,
            it.contentId) }

    return builder.build()
}