/** * Builder pattern method to add a newly configured {@link BodyPart} * to this {@link MultiPart}. * * @param entity entity object for this body part. * @param mediaType content type for this body part. */ public MultiPart bodyPart(Object entity, MediaType mediaType) { BodyPart bodyPart = new BodyPart(entity, mediaType); return bodyPart(bodyPart); }
public class Slimclient { private static final String TARGET_URL = "http://localhost:49158/rest/service/upload"; public Slimclient() { Client client = ClientBuilder.newBuilder() .register(MultiPartFeature.class).build(); WebTarget webTarget = client.target(TARGET_URL); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", new File("C:/Users/Nicklas/Desktop/aab.txt"), MediaType.APPLICATION_OCTET_STREAM_TYPE); multiPart.bodyPart(fileDataBodyPart); Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.entity(multiPart, multiPart.getMediaType())); System.out.println(response.getStatus() + " " + response.getStatusInfo() + " " + response); } public static void main(String[] args) { new Slimclient(); } }
/** * Builder pattern method to add a newly configured {@link BodyPart} * to this {@link MultiPart}. * * @param entity entity object for this body part. * @param mediaType content type for this body part. */ public MultiPart bodyPart(Object entity, MediaType mediaType) { BodyPart bodyPart = new BodyPart(entity, mediaType); return bodyPart(bodyPart); }
/** * Builder pattern method to add a newly configured {@link BodyPart} * to this {@link MultiPart}. * * @param entity entity object for this body part. * @param mediaType content type for this body part. */ public MultiPart bodyPart(Object entity, MediaType mediaType) { BodyPart bodyPart = new BodyPart(entity, mediaType); return bodyPart(bodyPart); }
/** * Builder pattern method to add a newly configured {@link BodyPart} * to this {@link MultiPart}. * * @param entity entity object for this body part. * @param mediaType content type for this body part. */ public MultiPart bodyPart(Object entity, MediaType mediaType) { BodyPart bodyPart = new BodyPart(entity, mediaType); return bodyPart(bodyPart); }
/** * Builder pattern method to add a newly configured {@link BodyPart} * to this {@link MultiPart}. * * @param entity entity object for this body part. * @param mediaType content type for this body part. */ public MultiPart bodyPart(Object entity, MediaType mediaType) { BodyPart bodyPart = new BodyPart(entity, mediaType); return bodyPart(bodyPart); }
@Produces("multipart/mixed") public Response multiPartResponder(){ MultiPart multiPart = new MultiPart(); BodyPart bodyPart = new BodyPart(myFileInputStream, MediaType.APPLICATION_OCTET_STREAM_TYPE); multiPart.bodyPart(bodyPart); return Response.status(OK).entity(multiPart); }
private static void addFileMultiPart(MultiPart multiPart, String key, String filename) { multiPart.bodyPart( new FormDataBodyPart(key, new File(filename), MediaType.APPLICATION_OCTET_STREAM_TYPE)); }
private static void addTextMultiPart(MultiPart multiPart, String key, String value) { multiPart.bodyPart(new FormDataBodyPart(key, value, MediaType.TEXT_PLAIN_TYPE)); }
@Override public String uploadFile(InputStream inputStream) { MultiPart multiPart = new MultiPart(); BodyPart filePart = new StreamDataBodyPart("file", inputStream, "file"); multiPart.bodyPart(filePart); return Subject.doAs(subject, new PrivilegedAction<String>() { @Override public String run() { return currentSchemaRegistryTargets().filesTarget.request() .post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA), String.class); } }); }
public void attachment(String attachment) { File file = file(attachment); this.attachments.bodyPart(new FileDataBodyPart(file.getName(), file, APPLICATION_OCTET_STREAM_TYPE)); }
public void attachment(String attachment) { File file = file(attachment); this.attachments.bodyPart(new FileDataBodyPart(file.getName(), file, APPLICATION_OCTET_STREAM_TYPE)); }
byte[] bin = some binary data... Book b = new Book(); b.setAuthor("John"); b.setTitle("wild stuff"); b.setYear(2012); MultiPart multiPart = new MultiPart(); multiPart.bodyPart(new BodyPart(b, MediaType.APPLICATION_XML_TYPE)); multiPart.bodyPart(new BodyPart(bin, MediaType.APPLICATION_OCTET_STREAM_TYPE)); response = service.path("rest").path("multipart"). type(MultiPartMediaTypes.MULTIPART_MIXED). post(ClientResponse.class, multiPart);
Client client = ClientBuilder.newClient(); client.register(MultiPartFeature.class); WebTarget target = client.target("https://apisomeurl.com"); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); //Image here FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("image", new File("/some/img/path/img.png")); multiPart.bodyPart(fileDataBodyPart); //MediaType.APPLICATION_JSON_TYPE because I'm expecting a JSON response from the server String str = target.queryParam("param1", "1") .queryParam("param2", "2") .queryParam("param3", "3") .request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.entity(multiPart, multiPart.getMediaType()), String.class);
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()) .fileName(file.getName()).size(file.length()).build(); multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, MediaType.APPLICATION_OCTET_STREAM_TYPE)); } else { FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build(); multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()) .fileName(file.getName()).size(file.length()).build(); multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, MediaType.APPLICATION_OCTET_STREAM_TYPE)); } else { FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build(); multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
/** * Perform a file upload using multipart/form-data using the HTTP PUT method, returning * a ClientResponse instance with the data returned from the endpoint. * * @param name the name for the form field that contains the file name * @param fileToUpload a File instance pointing to the file to upload * @param url the fully formed path to the GitLab API endpoint * @return a ClientResponse instance with the data returned from the endpoint * @throws IOException if an error occurs while constructing the URL */ protected Response putUpload(String name, File fileToUpload, URL url) throws IOException { try (MultiPart multiPart = new FormDataMultiPart()) { multiPart.bodyPart(new FileDataBodyPart(name, fileToUpload, MediaType.APPLICATION_OCTET_STREAM_TYPE)); final Entity<?> entity = Entity.entity(multiPart, Boundary.addBoundary(multiPart.getMediaType())); return (invocation(url, null).put(entity)); } }
/** * Perform a file upload using multipart/form-data using the HTTP PUT method, returning * a ClientResponse instance with the data returned from the endpoint. * * @param name the name for the form field that contains the file name * @param fileToUpload a File instance pointing to the file to upload * @param url the fully formed path to the GitLab API endpoint * @return a ClientResponse instance with the data returned from the endpoint * @throws IOException if an error occurs while constructing the URL */ protected Response putUpload(String name, File fileToUpload, URL url) throws IOException { try (MultiPart multiPart = new FormDataMultiPart()) { multiPart.bodyPart(new FileDataBodyPart(name, fileToUpload, MediaType.APPLICATION_OCTET_STREAM_TYPE)); final Entity<?> entity = Entity.entity(multiPart, Boundary.addBoundary(multiPart.getMediaType())); return (invocation(url, null).put(entity)); } }
protected MultiPart getMultiPart(ResourceTestElement resourceToTest, Object entity) { MultiPart multiPart = new MultiPart(); BodyPart filePart = new FileDataBodyPart(resourceToTest.getFileNameHeader(), resourceToTest.getFileToUpload()); BodyPart entityPart = new FormDataBodyPart(resourceToTest.getEntityNameHeader(), entity, MediaType.APPLICATION_JSON_TYPE); multiPart.bodyPart(filePart).bodyPart(entityPart); return multiPart; }
@Test @Ignore public void checkDictionaryUpload() throws Exception { Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test client"); client.property(ClientProperties.CONNECT_TIMEOUT, 5000); client.property(ClientProperties.READ_TIMEOUT, 5000); client.register(MultiPartFeature.class); final MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); File testDictionaryFile = new File(ResourceHelpers.resourceFilePath("test_dictionary.txt")); final FileDataBodyPart filePart = new FileDataBodyPart("file", testDictionaryFile); multiPart.bodyPart(filePart); Response response = client.target( String.format("http://localhost:%d/api/upload/dictionary", RULE.getLocalPort())) .request(MediaType.MULTIPART_FORM_DATA_TYPE) .post(Entity.entity(multiPart, multiPart.getMediaType())); assertThat(response.getStatus()).isEqualTo(200); } }