/** * Gets the field value after appropriate conversion to the requested type. This is useful only when the containing {@link * FormDataMultiPart} instance has been received, which causes the {@code providers} property to have been set. * * @param <T> the type of the field value. * @param clazz Desired class into which the field value should be converted. * @return the field value. * @throws ProcessingException if an IO error arises during reading an entity. * @throws IllegalArgumentException if no {@code MessageBodyReader} can be found to perform the requested conversion. * @throws IllegalStateException if this method is called when the {@code providers} property has not been set or when * the entity instance is not the unconverted content of the body part entity. */ public <T> T getValueAs(Class<T> clazz) { return getEntityAs(clazz); }
@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; } }
stream = part.getEntityAs(BodyPartEntity.class).getInputStream();
/** * Gets the field value after appropriate conversion to the requested type. This is useful only when the containing {@link * FormDataMultiPart} instance has been received, which causes the {@code providers} property to have been set. * * @param <T> the type of the field value. * @param clazz Desired class into which the field value should be converted. * @return the field value. * @throws ProcessingException if an IO error arises during reading an entity. * @throws IllegalArgumentException if no {@code MessageBodyReader} can be found to perform the requested conversion. * @throws IllegalStateException if this method is called when the {@code providers} property has not been set or when * the entity instance is not the unconverted content of the body part entity. */ public <T> T getValueAs(Class<T> clazz) { return getEntityAs(clazz); }
/** * Gets the field value after appropriate conversion to the requested type. This is useful only when the containing {@link * FormDataMultiPart} instance has been received, which causes the {@code providers} property to have been set. * * @param <T> the type of the field value. * @param clazz Desired class into which the field value should be converted. * @return the field value. * @throws ProcessingException if an IO error arises during reading an entity. * @throws IllegalArgumentException if no {@code MessageBodyReader} can be found to perform the requested conversion. * @throws IllegalStateException if this method is called when the {@code providers} property has not been set or when * the entity instance is not the unconverted content of the body part entity. */ public <T> T getValueAs(Class<T> clazz) { return getEntityAs(clazz); }
/** * Gets the field value after appropriate conversion to the requested type. This is useful only when the containing {@link * FormDataMultiPart} instance has been received, which causes the {@code providers} property to have been set. * * @param <T> the type of the field value. * @param clazz Desired class into which the field value should be converted. * @return the field value. * @throws ProcessingException if an IO error arises during reading an entity. * @throws IllegalArgumentException if no {@code MessageBodyReader} can be found to perform the requested conversion. * @throws IllegalStateException if this method is called when the {@code providers} property has not been set or when * the entity instance is not the unconverted content of the body part entity. */ public <T> T getValueAs(Class<T> clazz) { return getEntityAs(clazz); }
/** * Gets the field value after appropriate conversion to the requested type. This is useful only when the containing {@link * FormDataMultiPart} instance has been received, which causes the {@code providers} property to have been set. * * @param <T> the type of the field value. * @param clazz Desired class into which the field value should be converted. * @return the field value. * @throws ProcessingException if an IO error arises during reading an entity. * @throws IllegalArgumentException if no {@code MessageBodyReader} can be found to perform the requested conversion. * @throws IllegalStateException if this method is called when the {@code providers} property has not been set or when * the entity instance is not the unconverted content of the body part entity. */ public <T> T getValueAs(Class<T> clazz) { return getEntityAs(clazz); }
@Consumes(MediaType.MULTIPART_FORM_DATA) public void uploadImage(FormDataMultiPart multiPartData) { FormDataBodyPart filePart = multiPartData.getField("file"); if (filePart != null) { InputStream file = filePart.getEntityAs(InputStream.class); } }
@POST @Path("no-content-type") public Bean[] post2(@FormDataParam("beans") FormDataBodyPart bodyPart) throws IOException { bodyPart.setMediaType(MediaType.APPLICATION_JSON_TYPE); Bean[] beans = bodyPart.getEntityAs(Bean[].class); return beans; }
@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(); }
LOG.info("Processing paramaeter [" + name + "]"); final FormDataBodyPart part = getEntity().getField(name); final BodyPartEntity entity = part != null ? part.getEntityAs(BodyPartEntity.class) : null;
@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() } } }
@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 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()); } } }
@Override public File provide() { final FormDataBodyPart part = getEntity().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; } }
Config config; if (!cfgFormList.isEmpty()) { String jsonConfig = cfgFormList.get(0).getEntityAs(String.class); try { config = objectMapper.readValue(jsonConfig, Config.class); List<FormDataBodyPart> values = entry.getValue(); return values.stream() .map(val -> new ManualServiceRegistrar.ConfigFileInfo(key, val.getEntityAs(InputStream.class))); }).collect(toList());
@Override public File provide() { final FormDataBodyPart part = getEntity().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; } }
@Override public File provide() { final FormDataBodyPart part = getEntity().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 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()); } } } }
stream = part.getEntityAs(BodyPartEntity.class).getInputStream();