gpt4 book ai didi

java - RestEasy @MultipartForm null byte[] 和 InputStream 收到

转载 作者:行者123 更新时间:2023-12-02 07:05:08 25 4
gpt4 key购买 nike

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

最佳答案

我也遇到了同样的问题并找到了解决办法。当您的 @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;
}

}

希望这有帮助。

关于java - RestEasy @MultipartForm null byte[] 和 InputStream 收到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16197841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com