Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save damianmcdonald/f4ccc7805305daf5691f to your computer and use it in GitHub Desktop.
Save damianmcdonald/f4ccc7805305daf5691f to your computer and use it in GitHub Desktop.
Multi Part Upload using Jersey REST client
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.MultiPart;
import com.sun.jersey.multipart.file.FileDataBodyPart;
import org.json.JSONObject;
import javax.ws.rs.core.MediaType;
import java.io.*;
public class JerseyRestClientMultipartUpload {
public void exampleMultiPartUpload(File fileToUpload) {
final String API_URI = "http://localhost:8080/upload/file";
final ClientConfig config = new DefaultClientConfig();
final Client client = Client.create(config);
final WebResource resource = client.resource(API_URI);
// the file to upload, represented as FileDataBodyPart
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
fileToUpload,
MediaType.APPLICATION_OCTET_STREAM_TYPE);
fileDataBodyPart.setContentDisposition(
FormDataContentDisposition.name("file")
.fileName(fileToUpload.getName()).build());
// some json to send to the server as an element of the multi part request
final JSONObject jsonToSend = new JSONObject();
jsonToSend.put("character", "Jabba the Hutt");
jsonToSend.put("movie", "Return of the Jedi");
jsonToSend.put("isGoodGuy", false);
/* create the MultiPartRequest with:
* Text field called "description"
* JSON field called "characterProfile"
* Text field called "filename"
* Binary body part called "file" using fileDataBodyPart
*/
final MultiPart multiPart = new FormDataMultiPart()
.field("description", "Picture of Jabba the Hutt", MediaType.TEXT_PLAIN_TYPE)
.field("characterProfile", jsonToSend, MediaType.APPLICATION_JSON_TYPE)
.field("filename", fileToUpload.getName(), MediaType.TEXT_PLAIN_TYPE)
.bodyPart(fileDataBodyPart);
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
// POST request final
ClientResponse response = resource
.type("multipart/form-data").post(ClientResponse.class,
multiPart);
final String result = getStringFromInputStream(response.getEntityInputStream());
System.out.println("INFO >>> Response from API was: " + result);
client.destroy();
}
// convert InputStream to String
private String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
final StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
}
<!-- dependencies required for this Gist -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.19</version>
</dependency>
@tvhung83
Copy link

tvhung83 commented Oct 8, 2021

Short and sweet, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment