/** * Gets a map of form data body parts where the key is the control name * and the value is a list of one or more form data body parts. * * @return return the map of form data body parts. */ public Map<String, List<FormDataBodyPart>> getFields() { Map<String, List<FormDataBodyPart>> map = new HashMap<>(); for (BodyPart bodyPart : getBodyParts()) { if (!(bodyPart instanceof FormDataBodyPart)) { continue; } FormDataBodyPart p = (FormDataBodyPart) bodyPart; List<FormDataBodyPart> l = map.get(p.getName()); if (l == null) { l = new ArrayList<>(1); map.put(p.getName(), l); } l.add(p); } return map; }
/** * Gets a list of one or more form data body parts given a control name. * * @param name the control name. * @return the list of form data body parts, otherwise null if no parts are present with the given control name. */ public List<FormDataBodyPart> getFields(String name) { List<FormDataBodyPart> result = null; for (BodyPart bodyPart : getBodyParts()) { if (!(bodyPart instanceof FormDataBodyPart)) { continue; } if (name.equals(((FormDataBodyPart) bodyPart).getName())) { if (result == null) { result = new ArrayList<>(1); } result.add((FormDataBodyPart) bodyPart); } } return result; }
/** * 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; }
/** * Gets a map of form data body parts where the key is the control name * and the value is a list of one or more form data body parts. * * @return return the map of form data body parts. */ public Map<String, List<FormDataBodyPart>> getFields() { Map<String, List<FormDataBodyPart>> map = new HashMap<>(); for (BodyPart bodyPart : getBodyParts()) { if (!(bodyPart instanceof FormDataBodyPart)) { continue; } FormDataBodyPart p = (FormDataBodyPart) bodyPart; List<FormDataBodyPart> l = map.get(p.getName()); if (l == null) { l = new ArrayList<>(1); map.put(p.getName(), l); } l.add(p); } return map; }
/** * Gets a map of form data body parts where the key is the control name * and the value is a list of one or more form data body parts. * * @return return the map of form data body parts. */ public Map<String, List<FormDataBodyPart>> getFields() { Map<String, List<FormDataBodyPart>> map = new HashMap<>(); for (BodyPart bodyPart : getBodyParts()) { if (!(bodyPart instanceof FormDataBodyPart)) { continue; } FormDataBodyPart p = (FormDataBodyPart) bodyPart; List<FormDataBodyPart> l = map.get(p.getName()); if (l == null) { l = new ArrayList<>(1); map.put(p.getName(), l); } l.add(p); } return map; }
/** * Gets a map of form data body parts where the key is the control name * and the value is a list of one or more form data body parts. * * @return return the map of form data body parts. */ public Map<String, List<FormDataBodyPart>> getFields() { Map<String, List<FormDataBodyPart>> map = new HashMap<>(); for (BodyPart bodyPart : getBodyParts()) { if (!(bodyPart instanceof FormDataBodyPart)) { continue; } FormDataBodyPart p = (FormDataBodyPart) bodyPart; List<FormDataBodyPart> l = map.get(p.getName()); if (l == null) { l = new ArrayList<>(1); map.put(p.getName(), l); } l.add(p); } return map; }
/** * Gets a map of form data body parts where the key is the control name * and the value is a list of one or more form data body parts. * * @return return the map of form data body parts. */ public Map<String, List<FormDataBodyPart>> getFields() { Map<String, List<FormDataBodyPart>> map = new HashMap<>(); for (BodyPart bodyPart : getBodyParts()) { if (!(bodyPart instanceof FormDataBodyPart)) { continue; } FormDataBodyPart p = (FormDataBodyPart) bodyPart; List<FormDataBodyPart> l = map.get(p.getName()); if (l == null) { l = new ArrayList<>(1); map.put(p.getName(), l); } l.add(p); } return map; }
/** * 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; }
/** * Gets a list of one or more form data body parts given a control name. * * @param name the control name. * @return the list of form data body parts, otherwise null if no parts are present with the given control name. */ public List<FormDataBodyPart> getFields(String name) { List<FormDataBodyPart> result = null; for (BodyPart bodyPart : getBodyParts()) { if (!(bodyPart instanceof FormDataBodyPart)) { continue; } if (name.equals(((FormDataBodyPart) bodyPart).getName())) { if (result == null) { result = new ArrayList<>(1); } result.add((FormDataBodyPart) bodyPart); } } return result; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * Gets a list of one or more form data body parts given a control name. * * @param name the control name. * @return the list of form data body parts, otherwise null if no parts are present with the given control name. */ public List<FormDataBodyPart> getFields(String name) { List<FormDataBodyPart> result = null; for (BodyPart bodyPart : getBodyParts()) { if (!(bodyPart instanceof FormDataBodyPart)) { continue; } if (name.equals(((FormDataBodyPart) bodyPart).getName())) { if (result == null) { result = new ArrayList<>(1); } result.add((FormDataBodyPart) bodyPart); } } return result; }
/** * Gets a list of one or more form data body parts given a control name. * * @param name the control name. * @return the list of form data body parts, otherwise null if no parts are present with the given control name. */ public List<FormDataBodyPart> getFields(String name) { List<FormDataBodyPart> result = null; for (BodyPart bodyPart : getBodyParts()) { if (!(bodyPart instanceof FormDataBodyPart)) { continue; } if (name.equals(((FormDataBodyPart) bodyPart).getName())) { if (result == null) { result = new ArrayList<>(1); } result.add((FormDataBodyPart) bodyPart); } } return result; }
/** * Gets a list of one or more form data body parts given a control name. * * @param name the control name. * @return the list of form data body parts, otherwise null if no parts are present with the given control name. */ public List<FormDataBodyPart> getFields(String name) { List<FormDataBodyPart> result = null; for (BodyPart bodyPart : getBodyParts()) { if (!(bodyPart instanceof FormDataBodyPart)) { continue; } if (name.equals(((FormDataBodyPart) bodyPart).getName())) { if (result == null) { result = new ArrayList<>(1); } result.add((FormDataBodyPart) bodyPart); } } return result; }
@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(); }
@POST @Consumes(MediaType.MULTIPART_FORM_DATA) public void upload(FormDataMultiPart formParams) { Map<String, List<FormDataBodyPart>> fieldsByName = formParams.getFields(); // Usually each value in fieldsByName will be a list of length 1. // Assuming each field in the form is a file, just loop through them. for (List<FormDataBodyPart> fields : fieldsByName.values()) { for (FormDataBodyPart field : fields) { InputStream is = field.getEntityAs(InputStream.class); String fileName = field.getName(); // TODO: SAVE FILE HERE // if you want media type for validation, it's field.getMediaType() } } }
@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 } }
@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()); } } } }
@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()); } } }