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)); } }
/** * Adds an attachment from a {@link InputStream}. * * @param is an stream to read the attachment * @return this builder */ public MultipartBuilder attachment(InputStream is) { return bodyPart(new StreamDataBodyPart(ATTACHMENT_NAME, is)); }
/** * Adds an attachment directly by content. * * @param content the content of the attachment * @param filename the filename of the attachment * @return this builder */ public MultipartBuilder attachment(String content, String filename) { ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes()); return bodyPart(new StreamDataBodyPart(ATTACHMENT_NAME, is, filename)); }
/** * Adds a named attachment. * * @param is an stream to read the attachment * @param filename the filename to give to the attachment * @return this builder */ public MultipartBuilder attachment(InputStream is, String filename) { return bodyPart(new StreamDataBodyPart(ATTACHMENT_NAME, is, filename)); }
/** * Adds a named attachment with a custom MIME media type. * * @param is an stream to read the attachment * @param filename the filename to give to the attachment * @param mediaType the media type of the attachment * @return this builder */ public MultipartBuilder attachment(InputStream is, String filename, MediaType mediaType) { return bodyPart(new StreamDataBodyPart(ATTACHMENT_NAME, is, filename, mediaType)); }
/** * Adds a named inline attachment. * * @param is an stream to read the attachment * @param cidName the name to give to the attachment as referenced by the HTML email body * i.e. use cidName sample-image.png for the below example * <p> * <img src="cid:sample-image.png" alt="sample"> * </p> * @return this builder */ public MultipartBuilder inline(InputStream is, String cidName) { return bodyPart(new StreamDataBodyPart("inline", is, cidName)); }
StreamDataBodyPart filePart = new StreamDataBodyPart("File.docx", is) { @Override protected FormDataContentDisposition buildContentDisposition() { FormDataContentDisposition contentDisposition = new FormDataContentDisposition("form-data", getName(), getName(), null, null, null, 0) { @Override protected StringBuilder toStringBuffer() { StringBuilder sb = new StringBuilder(); sb.append(this.getType()); addStringParameter(sb, "name", getName()); addStringParameter(sb, "filename", getName()); return sb; } }; return contentDisposition; } };
@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); } }); }
/** * 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); }
public void saveEnrollments(InputStream inputStream, String url){ try { ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); client.addFilter(new LoggingFilter()); WebResource webResource = client.resource(url); FormDataMultiPart fdmp = new FormDataMultiPart(); fdmp.bodyPart(new StreamDataBodyPart("enrollments", inputStream)); ClientResponse response = webResource.type( "multipart/form-data").post(ClientResponse.class, fdmp); String response= response.getEntity(String.class); System.out.println(response); } catch (Exception e) { e.printStackTrace(); } }
public SchemaIdVersion uploadSchemaVersion(final String schemaBranchName, final String schemaName, final String description, final InputStream schemaVersionInputStream) throws InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException { SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName); if (schemaMetadataInfo == null) { throw new SchemaNotFoundException("Schema with name " + schemaName + " not found"); } StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", schemaVersionInputStream); WebTarget target = currentSchemaRegistryTargets().schemasTarget.path(schemaName).path("/versions/upload").queryParam("branch",schemaBranchName); MultiPart multipartEntity = new FormDataMultiPart() .field("description", description, MediaType.APPLICATION_JSON_TYPE) .bodyPart(streamDataBodyPart); Entity<MultiPart> multiPartEntity = Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA); Response response = Subject.doAs(subject, new PrivilegedAction<Response>() { @Override public Response run() { return target.request().post(multiPartEntity, Response.class); } }); return handleSchemaIdVersionResponse(schemaMetadataInfo, response); }