Jersey REST client - multipart creation - not from File object
Asked Answered
S

2

7

I guess there is no other way for creating a FileDateBodyPart than providing a File object:

public FileDataBodyPart(String name, File fileEntity)

But in my case what I have is a byte[] and I don't want to convert it to a file and store on a filesystem.

Is there any other way of generating a multipart (while uploading a file) from an array of byte, inputstream... in the worst case using other client library?

UPDATE: Here is the working code (but I want to use byte[] instead of File):

FileDataBodyPart filePart = new FileDataBodyPart("attachment", new File("C:/TEMP/test.txt"));
MultiPart multipart = new FormDataMultiPart().bodyPart(filePart);
Invocation.Builder invocationBuilder = webTarget.request().accept(MediaType.APPLICATION_JSON);
Response response = invocationBuilder
        .buildPost(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA))
        .invoke();
Scyphate answered 12/9, 2017 at 9:53 Comment(2)
The question is rather unclear. Show some more code. What are your intentions?Trailblazer
Question is updated.Scyphate
H
6

FileDataBodyPart is just a convenience class. It is not the only type you can use to create a body part. If you look at the docs for FormDataMultuPart.bodyPart(), you'll see that it takes as an argument a BodyPart. Check out the Javadocs (search for it; can't find direct link). If you look for BodyPart and traverse the hierarchy, there are a few classes the extend from BodyPart, like FormDataBodyPart (the most generic) and also StreamDataBodyPart. You could use either.

If you have an InputStream, the easiest way is to use the StreamDataBodyPart. See the javadoc, it has overloaded constructors.

If you MUST use a byte[], then you can just use the more generic FormDataBodyPart

byte[] bytes = "HelloWorld".getBytes(StandardCharsets.UTF_8);
FormDataContentDisposition fdcd = FormDataContentDisposition.name("test")
        .fileName("hello.txt").build();
FormDataBodyPart bodyPart = new FormDataBodyPart(fdcd, bytes, MediaType.TEXT_PLAIN_TYPE);
MultiPart multiPart = new FormDataMultiPart().bodyPart(bodyPart);

Parts should have a Content-Disposition header which give a little information about the part, so the server can handle it properly. The FileDataBodyPart and StreamDataBodyPart would handle the creation of the FormDataContentDisposition internally, that's why they are convenience classes.

Hemelytron answered 12/9, 2017 at 11:5 Comment(2)
That's my choice. Thank you.Scyphate
Somehow the StreamDataBodyPart did not work for me (no exception, but no data on server side), but creating a FormDataBodyPart did work!Greenquist
T
4

There's no other way for FileDataBodyPart to not accept a file.

As a workaround, you might want to create a temporary file and delete it once the JVM exits:

byte[] bytes = {1, 2, 3};

File tempFile = File.createTempFile("filename", null);
tempFile.deleteOnExit();

FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(bytes);
fos.close();

FileDataBodyPart filePart = new FileDataBodyPart("attachment", tempFile);
Trailblazer answered 12/9, 2017 at 10:19 Comment(1)
Yes, that could be an option. Thanks.Scyphate

© 2022 - 2024 — McMap. All rights reserved.