@Override public List<String> getValues(String param) { List<FormDataBodyPart> bodyParts = form.getFields(param); if (bodyParts == null) return Collections.emptyList(); List<String> values = new ArrayList<>(bodyParts.size()); for (FormDataBodyPart bodyPart : bodyParts) values.add(bodyPart.getValue()); return values; }
@Override public String getFirstValue(String param) { FormDataBodyPart bodyPart = form.getField(param); return bodyPart == null ? null : bodyPart.getValue(); }
@POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Path("/upload") public Response uploadFile( FormDataMultiPart multiPart, @FormDataParam("file") FormDataContentDisposition contentDispositionHeader ) throws IOException { InputStream fileInputStream = multiPart.getField("file").getValueAs(InputStream.class); FormDataBodyPart b=multipart.getField("id"); int id=Integer.parseInt(b.getValue()); System.out.println(id); // ... }
/** * parses form data as multipart form data and extracts the value of given fieldName from it. * * @param fieldName * @return Optional value of field passed as multipart post data */ public Optional<String> getFormDataFieldValue(final String fieldName) { FormDataBodyPart field = getFormData(FormDataMultiPart.class).getField(fieldName); return field == null ? Optional.<String>absent() : Optional.of(field.getValue()); }
/** * parses form data as multipart form data and extracts the value of given fieldName from it. * * @param fieldName * @return Optional value of field passed as multipart post data */ public Optional<String> getFormDataFieldValue(final String fieldName) { FormDataBodyPart field = getFormData(FormDataMultiPart.class).getField(fieldName); return field == null ? Optional.<String>absent() : Optional.of(field.getValue()); }
@POST @Path(value="{dId}") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public Response sendStatus(FormDataMultiPart multipart){ .... ... Map<String, List<FormDataBodyPart>> parts = multipart.getFields(); if(parts==null || parts.isEmpty()){ lg.warn("Empty/non-existent parts in request body!"); return sendBadRequestError(sp); } for(List<FormDataBodyPart> p: parts.values()){ FormDataBodyPart bp = p.get(0); lg.info("\t body part name {}",bp.getName()); lg.info("\t body part value {}",bp.getValue()); ....//do your real stuff here } }
valueBodyPart = valuesList.get(0); parameters.put(parameterName, valueBodyPart.getValue());
.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())));
@POST @Consumes(MediaType.MULTIPART_FORM_DATA) public void upload(FormDataMultiPart formParams){ Map<String, List<FormDataBodyPart>> fieldsByName = formParams.getFields(); for (List<FormDataBodyPart> fields : fieldsByName.values()) { for (FormDataBodyPart field : fields){ // Check if fields are files(If any one knows better solution to check files[] please share your answers) if (StringUtils.equals("files", StringUtils.substringBefore(field.getName(), "["))) { //To read file content InputStream is = field.getEntityAs(InputStream.class); String fileName = field.getName(); field.getMediaType();//File mimeType //To get file details like size,file name,etc FormDataContentDisposition f=field.getFormDataContentDisposition(); System.out.println(f.getFileName()); Note: f.getType() not return the actual file type it returns mime type as form-data to get actual mime type use FormDataBodyPart media type like above. } get other form data like user name else if(StringUtils.equals(field.getName(),"userName")){ System.out.println(field.getValue()); } } } }