有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

两台服务器之间的java传输InputStream

我有以下代码,它接受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

当我取出InputStreamFormDataContentDisposition@FormDataParam

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


共 (1) 个答案

  1. # 1 楼答案

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

    第一台服务器:

    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;
    }