final MediaType boundaryMediaType = Boundary.addBoundary(mediaType); if (boundaryMediaType != mediaType) { headers.putSingle(HttpHeaders.CONTENT_TYPE, boundaryMediaType.toString());
/** * POST an object with multiplepart. For example Uploading a file. * Below is a sample on how to create a Multipart from a string and post it * String xml = "some string"; * final FormDataBodyPart templatePart = new FormDataBodyPart("template", xml, MediaType.APPLICATION_OCTET_STREAM_TYPE); * <p> * FormDataContentDisposition.FormDataContentDispositionBuilder disposition = FormDataContentDisposition.name(templatePart.getName()); * disposition.fileName("fileName"); * templatePart.setFormDataContentDisposition(disposition.build()); * // Combine parts * MultiPart multiPart = new MultiPart(); * multiPart.bodyPart(templatePart); * multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); * * @param path the path to access * @param object the multiplart object to post * @param returnType the type to return * @return returns the response of the type T */ public <T> T postMultiPart(String path, MultiPart object, Class<T> returnType) { WebTarget target = buildTarget(path, null); MediaType contentType = MediaType.MULTIPART_FORM_DATA_TYPE; contentType = Boundary.addBoundary(contentType); return target.request().post(Entity.entity(object, contentType), returnType); }
/** * POST a multipart object streaming * * @param path the path to access * @param name the name of the param the endpoint is expecting * @param fileName the name of the file * @param stream the stream itself * @param returnType the type to return from the post * @return the response of type T */ public <T> T postMultiPartStream(String path, String name, String fileName, InputStream stream, Class<T> returnType) { WebTarget target = buildTarget(path, null); MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE); StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart(name, stream, fileName, MediaType.APPLICATION_OCTET_STREAM_TYPE); multiPart.getBodyParts().add(streamDataBodyPart); MediaType contentType = MediaType.MULTIPART_FORM_DATA_TYPE; contentType = Boundary.addBoundary(contentType); return target.request().post( Entity.entity(multiPart, contentType), returnType); }
final MediaType boundaryMediaType = Boundary.addBoundary(mediaType); if (boundaryMediaType != mediaType) { headers.putSingle(HttpHeaders.CONTENT_TYPE, boundaryMediaType.toString());
/** * 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)); } }
final MediaType boundaryMediaType = Boundary.addBoundary(mediaType); if (boundaryMediaType != mediaType) { headers.putSingle(HttpHeaders.CONTENT_TYPE, boundaryMediaType.toString());
final MediaType boundaryMediaType = Boundary.addBoundary(mediaType); if (boundaryMediaType != mediaType) { headers.putSingle(HttpHeaders.CONTENT_TYPE, boundaryMediaType.toString());
final MediaType boundaryMediaType = Boundary.addBoundary(mediaType); if (boundaryMediaType != mediaType) { headers.putSingle(HttpHeaders.CONTENT_TYPE, boundaryMediaType.toString());
new FileDataBodyPart(name, fileToUpload); multiPart.bodyPart(filePart); final Entity<?> entity = Entity.entity(multiPart, Boundary.addBoundary(multiPart.getMediaType())); return (invocation(url, null).post(entity));
new FileDataBodyPart(name, fileToUpload); multiPart.bodyPart(filePart); final Entity<?> entity = Entity.entity(multiPart, Boundary.addBoundary(multiPart.getMediaType())); return (invocation(url, null).post(entity));