public static UploadResponse uploadFile(WebTarget target, Path file, Path tagsFile) { assertThat(Files.exists(file), is(true)); // MediaType of the body part will be derived from the file. FileDataBodyPart filePart = new FileDataBodyPart("file", file.toFile(), MediaType.APPLICATION_OCTET_STREAM_TYPE); FormDataMultiPart mp = new FormDataMultiPart(); mp.bodyPart(filePart); if (tagsFile != null) { FileDataBodyPart tagsFilePart = new FileDataBodyPart("tags", tagsFile.toFile(), MediaType.APPLICATION_OCTET_STREAM_TYPE); mp.bodyPart(tagsFilePart); } if (staticLogger.isDebugEnabled()) { staticLogger.debug("Rest response: {}", restCall(target, "/_upload", mp, String.class, debugOption)); } return restCall(target, "/_upload", mp, UploadResponse.class, Collections.emptyMap()); } }
private MultipartBuilder bodyPart(BodyPart bodyPart) { form.bodyPart(bodyPart); return this; } }
FormDataMultiPart p = new FormDataMultiPart(); p.bodyPart(new FormDataBodyPart(FormDataContentDisposition .name("fieldKey").fileName("document.pdf").build(), new File("path/to/document.pdf"), DefaultMediaTypePredictor.CommonMediaTypes .getMediaTypeFromFileName("document.pdf")));
final FormDataMultiPart formDataMultiPart = new FormDataMultiPart(); final String value = "Hello World"; final FormDataContentDisposition dispo = FormDataContentDisposition// .name("file")// .fileName("test.txt")// .size(value.getBytes().length)// .build(); final FormDataBodyPart bodyPart = new FormDataBodyPart(dispo, value); formDataMultiPart.bodyPart(bodyPart);
FormDataMultiPart form = new FormDataMultiPart(); URI uri = new File("filepath").toURI(); InputStream data = this.getClass().getResourceAsStream("filePath"); FormDataBodyPart fdp1 = new FormDataBodyPart("key1", uri.toString()); FormDataBodyPart fdp2 = new FormDataBodyPart("key2",data, MediaType.APPLICATION_OCTET_STREAM_TYPE); form.bodyPart(fdp1).bodyPart(fdp2); response = builder.post(ClientResponse.class, form); Assert.assertEquals(response.getStatus(), Status.OK.getStatusCode());
String url = "https://graph.facebook.com/me/photos"; File file = new File("path"); ClientConfig cc = new DefaultClientConfig(); cc.getClasses().add(MultiPartWriter.class); Client client = Client.create(cc); WebResource webResource = client.resource(url); FormDataMultiPart fdmp = new FormDataMultiPart(); fdmp.bodyPart(new FileDataBodyPart("source", file, MediaType.APPLICATION_OCTET_STREAM_TYPE)); fdmp.bodyPart(new FormDataBodyPart("message", "YAY I did it!")); fdmp.bodyPart(new FormDataBodyPart("access_token", accessToken)); ClientResponse response = webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, fdmp); String string = response.getEntity(String.class);
@Override public void uploadFunction(String sourceFile, String path) throws PulsarAdminException { try { final FormDataMultiPart mp = new FormDataMultiPart(); mp.bodyPart(new FileDataBodyPart("data", new File(sourceFile), MediaType.APPLICATION_OCTET_STREAM_TYPE)); mp.bodyPart(new FormDataBodyPart("path", path, MediaType.TEXT_PLAIN_TYPE)); request(functions.path("upload")) .post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA), ErrorData.class); } catch (Exception e) { throw getApiException(e); } }
public Response uploadFile(final String file_path) { final FileDataBodyPart filePart = new FileDataBodyPart("file", new File(file_path)); final FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(filePart); final Response resp = fileUploadService.uploadFile(multiPart); return resp; } }
public boolean deploy(String archiveFile) { FileDataBodyPart archive = new FileDataBodyPart("id", new File(archiveFile)); MultiPart form = new FormDataMultiPart(). field("force", "true").bodyPart(archive); Response response = applicationTarget.request("application/json"). header("X-Requested-By", "loadr"). post(Entity.entity(form, form.getMediaType())); return (response.getStatus() == 200); }
ClientConfig cc = new DefaultClientConfig(); Client client = Client.create(cc); WebResource resource = client.resource("http://sample.com/uploadpath"); FormDataMultiPart form = new FormDataMultiPart(); File file = new File("c:/temp/file.txt"); form.field("filename", file.getName()); form.bodyPart(new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE)); ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
@Test public void testPostWithFileSuccess() throws Exception { FileDataBodyPart filePart = new FileDataBodyPart("file", new File(Resources.getResource("resources/testImage.jpg").toURI())); filePart.setContentDisposition(FormDataContentDisposition.name("file").fileName("testImage.jpg").build()); FormDataMultiPart request = new FormDataMultiPart(); request.field("data", fixture("resources/postWithFileSuccess.json"), MediaType.APPLICATION_JSON_TYPE); request.bodyPart(filePart); WebTarget target = mClient.target("http://localhost:8080/crackers").register(MultiPartFeature.class); Response response = target .request() .post(Entity.entity(request, MediaType.MULTIPART_FORM_DATA)); assertThat(response.getStatus()).isEqualTo(200); }
public void uploadFile(String appName, File file) { FileDataBodyPart applicationPart = new FileDataBodyPart("file", file); MultiPart multipart = new FormDataMultiPart().bodyPart(applicationPart); Response response = post(uri, String.format(APPLICATIONS_FILES_PATH, appName), Entity.entity(multipart, multipart.getMediaType())); if (response.getStatus() != 200) { throw new ApiException(response); } }
public Application deployApplication(File app, String appName, TargetType targetType, String target) { String id = getId(targetType, target); FileDataBodyPart applicationPart = new FileDataBodyPart("file", app); MultiPart multipart = new FormDataMultiPart() .field("artifactName", appName) .field("targetId", id) .bodyPart(applicationPart); Response response = post(uri, APPLICATIONS, Entity.entity(multipart, multipart.getMediaType())); validateStatusSuccess(response); return response.readEntity(Application.class); }
public static FormDataMultiPart createFormDataMultiPartForFact(final LensSessionHandle sessionId, final XFactTable xFactTable, MediaType mt) { final FormDataMultiPart mp = new FormDataMultiPart(); mp.bodyPart(getSessionIdFormDataBodyPart(sessionId, mt)); mp.bodyPart(getFormDataBodyPart("fact", "fact", cubeObjectFactory.createXFactTable(xFactTable), mt)); return mp; }
public static LensSessionHandle openSession(WebTarget target, final String userName, final String passwd, final LensConf conf, MediaType mt) { final WebTarget sessionTarget = target.path("session"); final FormDataMultiPart mp = new FormDataMultiPart(); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("username").build(), userName)); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("password").build(), passwd)); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("sessionconf").fileName("sessionconf").build(), conf, mt)); return sessionTarget.request(mt).post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), LensSessionHandle.class); }
private void setParams(LensSessionHandle lensSessionHandle) { FormDataMultiPart setpart = new FormDataMultiPart(); setpart.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("sessionid").build(), lensSessionHandle, defaultMT)); setpart .bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("key").build(), "lens.session.testRestartKey")); setpart.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("value").build(), "myvalue")); APIResult result = target().path("session").path("params").request(defaultMT) .put(Entity.entity(setpart, MediaType.MULTIPART_FORM_DATA_TYPE), APIResult.class); assertEquals(result.getStatus(), APIResult.Status.SUCCEEDED); }
private void executeAsync() { final WebTarget target = target().path("queryapi/queries"); final FormDataMultiPart mp = new FormDataMultiPart(); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("sessionid").build(), lensSessionId, MediaType.APPLICATION_XML_TYPE)); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("query").build(), "select ID, IDSTR from " + TestQueryService.TEST_TABLE)); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("operation").build(), "execute")); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("conf").fileName("conf").build(), new LensConf(), MediaType.APPLICATION_XML_TYPE)); final QueryHandle handle = target.request(mediaType).post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), new GenericType<LensAPIResult<QueryHandle>>() {}).getData(); Assert.assertNotNull(handle); }
private FormDataMultiPart getMultiFormData(String username, String password, MediaType mt) { final FormDataMultiPart mp = new FormDataMultiPart(); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("username").build(), username)); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("password").build(), password)); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("sessionconf").fileName("sessionconf").build(), new LensConf(), mt)); return mp; }
@Test public void testUploadDisabled() throws Exception { try { // disable uploads getSabotContext().getOptionManager().setOption(OptionValue.createBoolean(OptionValue.OptionType.SYSTEM, UIOptions.ALLOW_FILE_UPLOADS.getOptionName(), false)); FormDataMultiPart form = new FormDataMultiPart(); FormDataBodyPart fileBody = new FormDataBodyPart("file", FileUtils.getResourceAsFile("/datasets/csv/pipe.csv"), MediaType.MULTIPART_FORM_DATA_TYPE); form.bodyPart(fileBody); form.bodyPart(new FormDataBodyPart("fileName", "pipe")); expectStatus(Response.Status.FORBIDDEN, getBuilder(getAPIv2().path("home/" + HOME_NAME + "/upload_start/").queryParam("extension", "csv")).buildPost(Entity.entity(form, form.getMediaType()))); } finally { // re-enable uploads getSabotContext().getOptionManager().setOption(OptionValue.createBoolean(OptionValue.OptionType.SYSTEM, UIOptions.ALLOW_FILE_UPLOADS.getOptionName(), true)); } }