Refine search
public void testPostMultiPartFile() { final WebTarget target = target().path("test/multipart"); final FormDataMultiPart mp = new FormDataMultiPart(); final FormDataBodyPart p = new FormDataBodyPart("field1", "CONTENT ONE"); mp.bodyPart(p); final FormDataBodyPart p2 = new FormDataBodyPart("field2", "CONTENT TWO"); mp.bodyPart(p2); System.out.println("field1: " + mp.getField("field1").getValue()); System.out.println("field2: " + mp.getField("field2").getValue()); System.out.println("making multipart request"); final Response r = target.request().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA), Response.class);
@Override public List<FormDataBodyPart> apply(ContainerRequest request) { return getEntity(request).getFields(name); } }
/** * Builder pattern method to add a named field with a text value, * and return this instance. * * @param name the control name. * @param value the text value. * @return this instance. */ public FormDataMultiPart field(String name, String value) { getBodyParts().add(new FormDataBodyPart(name, value)); return this; }
private File getFileFromMultipart(FormDataMultiPart multiPart, File dstFile) throws IOException { // Read segment file or segment metadata file and directly use that information to update zk Map<String, List<FormDataBodyPart>> segmentMetadataMap = multiPart.getFields(); if (!validateMultiPart(segmentMetadataMap, null)) { throw new ControllerApplicationException(LOGGER, "Invalid multi-part form for segment metadata", Response.Status.BAD_REQUEST); } FormDataBodyPart segmentMetadataBodyPart = segmentMetadataMap.values().iterator().next().get(0); try (InputStream inputStream = segmentMetadataBodyPart.getValueAs(InputStream.class); OutputStream outputStream = new FileOutputStream(dstFile)) { IOUtils.copyLarge(inputStream, outputStream); } finally { multiPart.cleanup(); } return dstFile; }
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());
@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)); } }
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);
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()); } }
try (FormDataMultiPart multiPart = new FormDataMultiPart()) { formParams.forEach((key, values) -> { if (values != null) { values.forEach(value -> multiPart.field(key, value)); new FileDataBodyPart(name, fileToUpload, mediaType) : new FileDataBodyPart(name, fileToUpload); multiPart.bodyPart(filePart); final Entity<?> entity = Entity.entity(multiPart, Boundary.addBoundary(multiPart.getMediaType())); return (invocation(url, null).post(entity));
FormDataMultiPart mMultiPart = null; byte fileContent[] = readFile(fileName); /// readfile is a method read file bytes if (mMultiPart == null) { mMultiPart = new FormDataMultiPart(); } mMultiPart.field(attachmentName, fileContent,MediaType.APPLICATION_OCTET_STREAM_TYPE);
WebTarget webTarget = client.target(url); Builder builder = webTarget.request(MediaType.MULTIPART_FORM_DATA); FormDataMultiPart multiPart = new FormDataMultiPart(); for (int i = 0; i < filePath.length; i++) { multiPart.bodyPart(new FileDataBodyPart(filePath[i].getName(), filePath[i].getFileEntity())); multiPart.type(Boundary.addBoundary(new MediaType("multipart", "form-data", Collections.singletonMap(Boundary.BOUNDARY_PARAMETER, boundary)))); for (String jstr : jsonString) { multiPart.field("Content-Type", jstr, MediaType.APPLICATION_JSON_TYPE); Response response = builder.accept(MediaType.APPLICATION_JSON).put(Entity.entity(multiPart, multiPart.getMediaType())); multiPart.close();
final MultiPart multiPart = formData ? new FormDataMultiPart() : new MultiPart(); final BodyPart bodyPart = formData ? new FormDataBodyPart(fileNameFix) : new BodyPart();
private MultiPart computeMultiPart( Method method, Object[] parameter ) { FormDataMultiPart result = new FormDataMultiPart(); Annotation[][] parameterAnnotations = method.getParameterAnnotations(); for( int i = 0; i < parameterAnnotations.length; i++ ) { Annotation[] annotations = parameterAnnotations[ i ]; FormDataParam param = extractAnnotation( annotations, FormDataParam.class ); if( param != null ) { result.field( param.value(), parameter[ i ], determinePartContentType( parameter[ i ] ) ); } } return result.getFields().isEmpty() ? null : result; }
MultiPart multipartEntity = new FormDataMultiPart() .field("emp", empPartJson, MediaType.APPLICATION_JSON_TYPE) .bodyPart(filePart);
@Override public Object apply(ContainerRequest request) { final List<FormDataBodyPart> parts = getEntity(request).getFields(parameter.getSourceName()); final MediaType mediaType = part != null ? part.getMediaType() : MediaType.TEXT_PLAIN_TYPE; stream = part.getEntityAs(BodyPartEntity.class).getInputStream(); String.class, parameter.getAnnotations(), p.getMediaType());
@Override public FormDataContentDisposition apply(ContainerRequest request) { final FormDataBodyPart part = getEntity(request).getField(name); return part == null ? null : part.getFormDataContentDisposition(); } }
@POST @Path("files") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadMultipart(FormDataMultiPart multiPart) throws IOException{ List<FormDataBodyPart> fields = multiPart.getFields("test"); for(FormDataBodyPart field : fields){ handleInputStream(field.getValueAs(InputStream.class)); } //prepare the response } private void handleInputStream(InputStream is){ //read the stream any way you want }
/** * Gets a form data body part given a control name. * * @param name the control name. * @return the form data body part, otherwise null if no part is present with the given control name. If more that one part * is present with the same control name, then the first part that occurs is returned. */ public FormDataBodyPart getField(String name) { FormDataBodyPart result = null; for (BodyPart bodyPart : getBodyParts()) { if (!(bodyPart instanceof FormDataBodyPart)) { continue; } if (name.equals(((FormDataBodyPart) bodyPart).getName())) { result = (FormDataBodyPart) bodyPart; } } return result; }
@Path("/upload") @POST @Consumes(MediaType.MULTIPART_FORM_DATA) public ResponseDTO upload(FormDataMultiPart multiPartData) { // non-file fields final String imageId = multiPartData.getField("imageId").getValue(); // for file field final FormDataBodyPart imagePart = multiPartData.getField("image"); final ContentDisposition imageDetails= imagePart.getContentDisposition(); final InputStream imageDoc = imagePart.getValueAs(InputStream.class); // set the retrieved content in DTO }