在两台服务器之间传输InputStream

问题描述 投票:1回答:1

我有以下代码,它采用InputStream并将其发送到另一台服务器:

Client client = ClientBuilder.newClient();

MultipartBody mpb = new MultipartBody(
        new Attachment(
                "file",
                uploadedInputStream,
                new ContentDisposition("file=test.pdf")
        )
);

Response response = client.target(url)
        .request(APPLICATION_JSON)
        .post(Entity.entity(mpb, MediaType.MULTIPART_FORM_DATA_TYPE), Response.class);

在第二个服务器我有这个api:

public String uploadFile(
    @Context HttpServletRequest request,
    @PathParam("name") String fileName,
    @PathParam("type") int type,
    @PathParam("userIdentifier") String userId,
    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail
)
{
    return null;
}

我收到错误400。

当我拿出@FormDataParamInputStreamFormDataContentDisposition时。

一切都很好,我得到了成功的回应。

java jersey microservices
1个回答
0
投票

所以,我没有找到任何解决方案,因此我不是将文件作为附件发送,而是将其作为一个字节发送给他。这就是我做到的:

第一台服务器:

byte[] bytes = IOUtils.toByteArray(uploadedInputStream);

Client client = ClientBuilder.newClient();

Invocation.Builder request = client.target(url)
        .request(APPLICATION_JSON);
Response response = request.post(Entity.entity(bytes, APPLICATION_OCTET_STREAM_TYPE), Response.class);

接收文件的第二个:

@POST
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Produces(MediaType.APPLICATION_JSON)
@Path("")
public IBlResult<String> uploadFile(@Context HttpServletRequest request,
                                    @PathParam("name") String fileName,
                                    @PathParam("type") int type,
                                    @PathParam("userIdentifier") int userId,
                                    byte[] file
)
{
    return null;
}
© www.soinside.com 2019 - 2024. All rights reserved.