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());
/** * Gets the field value for this body part. This should be called only on body parts representing simple field values. * * @return the simple field value. * @throws ProcessingException if an IO error arises during reading the value. * @throws IllegalStateException if called on a body part with a media type other than {@code text/plain} */ public String getValue() { if (!MediaTypes.typeEqual(MediaType.TEXT_PLAIN_TYPE, getMediaType())) { throw new IllegalStateException(LocalizationMessages.MEDIA_TYPE_NOT_TEXT_PLAIN()); } if (getEntity() instanceof BodyPartEntity) { return getValueAs(String.class); } else { return (String) getEntity(); } }
/** * Gets the form data content disposition. * * @return the form data content disposition. */ public FormDataContentDisposition getFormDataContentDisposition() { return (FormDataContentDisposition) getContentDisposition(); }
@POST @Path("upload2") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFileAndJSON(@FormDataParam("emp") FormDataBodyPart jsonPart, @FormDataParam("file") FormDataBodyPart bodyPart) { jsonPart.setMediaType(MediaType.APPLICATION_JSON_TYPE); Employee emp = jsonPart.getValueAs(Employee.class); }
/** * Sets the field value for this body part. This should be called * only on body parts representing simple field values. * * @param value the field value. * @throws IllegalStateException if called on a body part with a media type other than {@code text/plain}. */ public void setValue(String value) { if (!MediaType.TEXT_PLAIN_TYPE.equals(getMediaType())) { throw new IllegalStateException(LocalizationMessages.MEDIA_TYPE_NOT_TEXT_PLAIN()); } setEntity(value); }
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());
/** * 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; }
@POST @Consumes(MediaType.MULTIPART_FORM_DATA) public void upload(FormDataMultiPart formParams) { Map<String, List<FormDataBodyPart>> fieldsByName = formParams.getFields(); //Assume i am sending only files with the request for (List<FormDataBodyPart> fields : fieldsByName.values()) { for (FormDataBodyPart field : fields) { InputStream is = field.getEntityAs(InputStream.class); String fileName = field.getName(); field.getMediaType(); //This working fine FormDataContentDisposition f=field.getFormDataContentDisposition(); System.out.println(f.getFileName()); } } }
final MultiPart multiPart = formData ? new FormDataMultiPart() : new MultiPart(); final BodyPart bodyPart = formData ? new FormDataBodyPart(fileNameFix) : new BodyPart();
private static FormDataBodyPart getFormDataBodyPart(final String fdContentDispName, final Object entity, final MediaType mt) { return new FormDataBodyPart(FormDataContentDisposition.name(fdContentDispName).build(), entity, mt); }
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; }
@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 }
@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()); mediaType, request.getHeaders(), ((BodyPartEntity) p.getEntity()).getInputStream());
@Override public File apply(ContainerRequest request) { final FormDataBodyPart part = getEntity(request).getField(name); final BodyPartEntity entity = part != null ? part.getEntityAs(BodyPartEntity.class) : null; if (entity != null) { try { // Create a temporary file. final File file = Utils.createTempFile(); // Move the part (represented either via stream or file) to the specific temporary file. entity.moveTo(file); return file; } catch (final IOException | MIMEParsingException cannotMove) { // Unable to create a temporary file or move the file. LOGGER.log(Level.WARNING, LocalizationMessages.CANNOT_INJECT_FILE(), cannotMove); } } return null; } }
@POST @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadAsset(FormDataMultiPart multipart) { Map<String, List<FormDataBodyPart>> map = multipart.getFields(); for (Map.Entry<String, List<FormDataBodyPart>> entry : map.entrySet()) { for (FormDataBodyPart part : entry.getValue()) { InputStream in = part.getEntityAs(InputStream.class); String name = part.getName(); System.out.println("--- name: " + name); } } return Response.ok("cool upload").build(); }
@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 }
final MediaType mediaType = mimeTypeOverride == null ? body.getMediaType() : mimeTypeOverride; Optional<Loader> loader = LoaderFactory.createFor(mediaType.toString(), formData.getFields().entrySet().stream() .filter(entry -> entry.getValue().size() > 0) .filter(entry -> entry.getValue().get(0) != null) .filter(entry -> MediaTypes.typeEqual(MediaType.TEXT_PLAIN_TYPE, entry.getValue().get(0).getMediaType())) .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().get(0).getValue())));
@Path("/uploadPhoto") @ApiOperation( value = "Upload a photo for an Ad", response = Response.class) @POST @Timed @UnitOfWork @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public Response uploadFile(FormDataMultiPart multiPart) { List<AdImage> images = new ArrayList<AdImage>(); List<FormDataBodyPart> bodyParts = multiPart.getFields("file"); for (FormDataBodyPart part : bodyParts) { images.add(writeImageAndSave(part.getValueAs(InputStream.class ), part.getFormDataContentDisposition())); } return Response.ok(toJson(images), MediaType.APPLICATION_JSON).build(); }