0

我一直在尝试使用 @MultipartForm 测试发送 byte[] 和 InputStream 对象,但是我得到了这两种类型的空对象,有什么想法吗?我正在使用 RestEasy 2.3.5.Final

public class DocumentForm {
    private byte[] bytes;
    private InputStream stream;

    public byte[] getBytes() {
        return bytes;
    }

    @FormParam("bytes")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public InputStream getStream() {
        return stream;
    }

    @FormParam("stream")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    public void setStream(InputStream stream) {
        this.stream = stream;
    }

}

public interface DocumentService {
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response store(@MultipartForm DocumentForm documentForm);

}


@Path("/document")
public class DocumentServiceEndpoint implements DocumentService {

    public Response store(DocumentForm documentForm) {
        System.out.println(documentForm.getBytes());
        System.out.println(documentForm.getStream());
        return Response.status(200).entity("OK").build();
    }
}

public class DocumentServiceTest {
    public static void main(String[] args) {
        DocumentForm documentForm = new DocumentForm();
        byte[] data = "TEST".getBytes();
        documentForm.setBytes(data);
        documentForm.setStream(new ByteArrayInputStream(data));
        DocumentService documentService = ProxyFactory.create(DocumentService.class, "http://localhost:8080/document-rs/document");
        documentService.store(documentForm);
    }
}

在客户端登录时,我得到:

DEBUG BasicClientConnectionManager - Get connection for route {}->http://localhost:8080
DEBUG DefaultClientConnectionOperator - Connecting to localhost:8080
DEBUG RequestAddCookies - CookieSpec selected: best-match
DEBUG RequestAuthCache - Auth cache not set in the context
DEBUG RequestTargetAuthentication - Target auth state: UNCHALLENGED
DEBUG RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
DEBUG DefaultHttpClient - Attempt 1 to execute request
DEBUG DefaultClientConnection - Sending request: POST /document-rs/document HTTP/1.1
DEBUG wire - >> "POST /document-rs/document HTTP/1.1[\r][\n]"
DEBUG wire - >> "Accept-Encoding: gzip, deflate[\r][\n]"
DEBUG wire - >> "Content-Type: multipart/form-data; boundary=37e8b375-affb-47ec-94b0-f69f0eff1d36[\r][\n]"
DEBUG wire - >> "Content-Length: 40[\r][\n]"
DEBUG wire - >> "Host: localhost:8080[\r][\n]"
DEBUG wire - >> "Connection: Keep-Alive[\r][\n]"
DEBUG wire - >> "User-Agent: Apache-HttpClient/4.2.3 (java 1.5)[\r][\n]"
DEBUG wire - >> "[\r][\n]"
DEBUG headers - >> POST /document-rs/document HTTP/1.1
DEBUG headers - >> Accept-Encoding: gzip, deflate
DEBUG headers - >> Content-Type: multipart/form-data; boundary=37e8b375-affb-47ec-94b0-f69f0eff1d36
DEBUG headers - >> Content-Length: 40
DEBUG headers - >> Host: localhost:8080
DEBUG headers - >> Connection: Keep-Alive
DEBUG headers - >> User-Agent: Apache-HttpClient/4.2.3 (java 1.5)
DEBUG wire - >> "--37e8b375-affb-47ec-94b0-f69f0eff1d36--"
DEBUG wire - << "HTTP/1.1 200 OK[\r][\n]"
DEBUG wire - << "Server: Apache-Coyote/1.1[\r][\n]"
DEBUG wire - << "X-Powered-By: Servlet 2.5; JBoss-5.0/JBossWeb-2.1[\r][\n]"
DEBUG wire - << "Content-Type: */*[\r][\n]"
DEBUG wire - << "Content-Length: 2[\r][\n]"
DEBUG wire - << "Date: Wed, 24 Apr 2013 16:32:40 GMT[\r][\n]"
DEBUG wire - << "[\r][\n]"
DEBUG DefaultClientConnection - Receiving response: HTTP/1.1 200 OK
DEBUG headers - << HTTP/1.1 200 OK
DEBUG headers - << Server: Apache-Coyote/1.1
DEBUG headers - << X-Powered-By: Servlet 2.5; JBoss-5.0/JBossWeb-2.1
DEBUG headers - << Content-Type: */*
DEBUG headers - << Content-Length: 2
DEBUG headers - << Date: Wed, 24 Apr 2013 16:32:40 GMT
DEBUG DefaultHttpClient - Connection can be kept alive indefinitely

在服务器端:

13:32:40,380 INFO  [STDOUT] null
13:32:40,381 INFO  [STDOUT] null
4

1 回答 1

2

我遇到了同样的问题并想出了解决办法。@MultipartForm当您的bean 将注释放在 getter 而不是属性本身上时,似乎存在一个简单的错误,这会导致 http 请求无法正确构建。我相信无论您使用的是ApacheHttpClient4Executor还是URLConnectionClientExecutor

在您的情况下,正确的 bean 如下所示:

public class DocumentForm {
    @FormParam("bytes")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    private byte[] bytes;
    @FormParam("stream")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    private InputStream stream;

    public byte[] getBytes() {
        return bytes;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public InputStream getStream() {
        return stream;
    }

    public void setStream(InputStream stream) {
        this.stream = stream;
    }

}

希望这可以帮助。

于 2013-11-19T22:14:20.447 回答