Refine search
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(); } }
final MultiPart multiPart = formData ? new FormDataMultiPart() : new MultiPart(); multiPart.setMessageBodyWorkers(workers); final MultivaluedMap<String, String> multiPartHeaders = multiPart.getHeaders(); for (final Map.Entry<String, List<String>> entry : headers.entrySet()) { final List<String> values = entry.getValue(); multiPart.setMediaType(mediaType); fileNameFix = false; } else { final BodyPart bodyPart = formData ? new FormDataBodyPart(fileNameFix) : new BodyPart(); bodyPart.setMessageBodyWorkers(workers); bodyPart.getHeaders().add(header.getName(), header.getValue()); multiPart.getBodyParts().add(bodyPart);
public void close() throws IOException { cleanup(); }
if ((entity.getBodyParts() == null) || (entity.getBodyParts().size() < 1)) { throw new IllegalArgumentException(LocalizationMessages.MUST_SPECIFY_BODY_PART()); if (entity.getParent() == null) { final Object value = headers.getFirst("MIME-Version"); if (value == null) { for (final BodyPart bodyPart : entity.getBodyParts()) { final MediaType bodyMediaType = bodyPart.getMediaType(); if (bodyMediaType == null) { throw new IllegalArgumentException(LocalizationMessages.MISSING_MEDIA_TYPE_OF_BODY_PART()); final MultivaluedMap<String, String> bodyHeaders = bodyPart.getHeaders(); bodyHeaders.putSingle("Content-Type", bodyMediaType.toString()); if (bodyHeaders.getFirst("Content-Disposition") == null && bodyPart.getContentDisposition() != null) { bodyHeaders.putSingle("Content-Disposition", bodyPart.getContentDisposition().toString());
@Nullable String checkApiKey() { try { WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_CHECK_API_KEY); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey()); JSONObject jObj = postData(webTarget, multiPart); if (jObj == null) { return null; } return Boolean.toString(jObj.getBoolean(CoordConsts.SVC_KEY_API_KEY)); } catch (Exception e) { _logger.errorf("exception: "); _logger.error(Throwables.getStackTraceAsString(e) + "\n"); return null; } }
formData = true; } else { multiPart = new MultiPart(); multiPart.setProviders(providers); multiPart.setMediaType(mediaType); bodyPart = new FormDataBodyPart(); } else { bodyPart = new BodyPart(); bodyPart.setProviders(providers); bodyPart.getHeaders().add(h.getName(), getFixedHeaderValue(h)); bodyPart.setEntity(new BodyPartEntity(mp)); multiPart.getBodyParts().add(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); }
WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_GET_CONFIGURATION); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); webTarget .request(MediaType.APPLICATION_JSON) .post(Entity.entity(multiPart, multiPart.getMediaType()));
final MultiPart multiPartEntity = new MultiPart() .bodyPart(new BodyPart().entity("hello")) .bodyPart(new BodyPart(new JaxbBean("xml"), MediaType.APPLICATION_XML_TYPE)) .bodyPart(new BodyPart(new JaxbBean("json"), MediaType.APPLICATION_JSON_TYPE)); final WebTarget target = // Create WebTarget. final Response response = target .request() .post(Entity.entity(multiPartEntity, multiPartEntity.getMediaType()));
private static void addFileMultiPart(MultiPart multiPart, String key, String filename) { multiPart.bodyPart( new FormDataBodyPart(key, new File(filename), MediaType.APPLICATION_OCTET_STREAM_TYPE)); }
/** * 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 final Response getFormPostResponse(String data, String endpoint, Class<? extends OutputStream> compressingClass, String encoding) throws IOException { byte[] bytes; if (compressingClass == null) { bytes = data.getBytes(StandardCharsets.UTF_8); } else { bytes = compress(data, compressingClass); } MediaType type = encoding == null ? MediaType.TEXT_PLAIN_TYPE : new MediaType("application", encoding); InputStream in = new ByteArrayInputStream(bytes); StreamDataBodyPart filePart = new StreamDataBodyPart("data", in, "data", type); try (MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE)) { multiPart.getBodyParts().add(filePart); return target(endpoint).request().post( Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE)); } }
/** * Performs any necessary cleanup at the end of processing this * {@link MultiPart}. */ @Override public void cleanup() { for (BodyPart bp : getBodyParts()) { bp.cleanup(); } }
/** * Builder pattern method to add the specified {@link BodyPart} to this * {@link MultiPart}. * * @param bodyPart {@link BodyPart} to be added. */ public MultiPart bodyPart(BodyPart bodyPart) { getBodyParts().add(bodyPart); return this; }
public Entity<?> requestBody() { return hasAttachment() ? entity(attachments, attachments.getMediaType()) : entity(requestBody, contentType()); }
/** * Builder pattern method to return this {@link MultiPart} after * additional configuration. * * @param type media type to set for this {@link MultiPart}. */ @Override public MultiPart type(MediaType type) { setMediaType(type); return this; }
@POST @Consumes(MultiPartMediaTypes.MULTIPART_MIXED) public Response post(MultiPart multiPart) { for(BodyPart part : multiPart.getBodyParts()) { System.out.println(part.getMediaType()); } return Response.status(Response.Status.ACCEPTED). entity("Attachements processed successfully."). type(MediaType.TEXT_PLAIN).build(); }
@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); } }); }
boolean delAnalysis(String networkName, String analysisName) { try { WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_DEL_ANALYSIS); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey()); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_ANALYSIS_NAME, analysisName); return postData(webTarget, multiPart) != null; } catch (Exception e) { _logger.errorf( "Exception when deleting analysis to %s using (%s, %s): %s\n", _coordWorkMgr, networkName, analysisName, Throwables.getStackTraceAsString(e)); return false; } }