Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RestEasy@MultipartForm空字节[]和接收到的InputStream_Java_Rest_Resteasy_Multipartform Data - Fatal编程技术网

Java RestEasy@MultipartForm空字节[]和接收到的InputStream

Java RestEasy@MultipartForm空字节[]和接收到的InputStream,java,rest,resteasy,multipartform-data,Java,Rest,Resteasy,Multipartform Data,我一直在尝试使用@MultipartForm测试发送byte[]和InputStream对象,但这两种类型都有一个空对象,你知道为什么吗?我正在使用RestEasy 2.3.5.Final public class DocumentForm { private byte[] bytes; private InputStream stream; public byte[] getBytes() { return bytes; } @For

我一直在尝试使用@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

我遇到了同样的问题,并找到了解决办法。当
@MultipartForm
bean将注释放在getter而不是属性本身上时,似乎存在resteasy错误,这导致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;
    }

}

希望这能有所帮助。

谢谢,我会尝试一下的——我也遇到了同样的问题,真的很沮丧。谢谢你的修复!
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;
    }

}