简体   繁体   中英

Transfer InputStream between 2 servers

I have the following code that takes an InputStream and sends it to another server:

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

and in the second server i have this 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;
}

and I am getting error 400.

when I am taking out the @FormDataParam of the InputStream and the FormDataContentDisposition .

everything is ok and I am getting a success response.

So, I didn't found any solution, so instead of sending the file as an attachment, I send him as a byte. this is how I did it:

the first server:

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

the second that receives the file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM