求指导如何post带文件的表单?multipart/form-data

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

求指导如何post带文件的表单?multipart/form-data

taohuang
This post was updated on .
List<NVPair> headerList = new ArrayList<NVPair>()

headerList.add(new NVPair("Content-Type", "multipart/form-data; charset=utf-8"))

headers = headerList.toArray()

NVPair[] formdata() {
 
        return [
                new NVPair("A", "1"),
                new NVPair("B", "2")
        ];
    }
 
    @Test
    public void test1() {
 
        HTTPResponse result = request.POST(url, formdata(), headers, true)


比如传name='file',filename='upload.txt'

Reply | Threaded
Open this post in threaded view
|

Re: 求指导如何post带文件的表单?multipart/form-data

taohuang
按这个案例方法可以

http://grinder.sourceforge.net/g3/script-gallery.html#form.py

class TestRunner:
    def __call__(self):
 
        files = ( NVPair("self", "form.py"), )
        parameters = ( NVPair("run number", str(grinder.runNumber)), )
 
        # This is the Jython way of creating an NVPair[] Java array
        # with one element.
        headers = zeros(1, NVPair)
 
        # Create a multi-part form encoded byte array.
        data = Codecs.mpFormDataEncode(parameters, files, headers)
        grinder.logger.output("Content type set to %s" % headers[0].value)
 
        # Call the version of POST that takes a byte array.
        result = request1.POST("/upload", data, headers)

groovy版本参考post源码,类似