@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); }
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; }
@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 }
private Schema getSchemaFromMultiPart(FormDataMultiPart multiPart) { try { Map<String, List<FormDataBodyPart>> map = multiPart.getFields(); if (!PinotSegmentUploadRestletResource.validateMultiPart(map, null)) { throw new ControllerApplicationException(LOGGER, "Found not exactly one file from the multi-part fields", Response.Status.BAD_REQUEST); } FormDataBodyPart bodyPart = map.values().iterator().next().get(0); try (InputStream inputStream = bodyPart.getValueAs(InputStream.class)) { return Schema.fromInputSteam(inputStream); } catch (IOException e) { throw new ControllerApplicationException(LOGGER, "Caught exception while de-serializing the schema from request body: " + e.getMessage(), Response.Status.BAD_REQUEST); } } finally { multiPart.cleanup(); } }
/** * 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(); } }
try (InputStream inputStream = bodyPart.getValueAs(InputStream.class); OutputStream outputStream = new FileOutputStream(localTmpFile)) { IOUtils.copyLarge(inputStream, outputStream);
@Path("/upload/multiples") @POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public Response uploadImage(@FormDataParam("image") List<FormDataBodyPart> imageDatas){ for( FormDataBodyPart imageData : imageDatas ){ // Your actual code. imageData.getValueAs(InputStream.class); } }
for (FormDataBodyPart vPart : v1) { String v = vPart.getValueAs(String.class); ...
/** * Change a list of <FormDataBodyPart> to <String> * @param bodyPartList * @return */ public static List<String> toStringList(List<FormDataBodyPart> bodyPartList) { if (bodyPartList == null) { return null; } List<String> stringList = new ArrayList<String>(); for (FormDataBodyPart fdbp : bodyPartList) { stringList.add(fdbp.getValueAs(String.class)); } return stringList; }
/** * Change a list of <FormDataBodyPart> to <String> * @param bodyPartList * @return */ public static List<String> toStringList(List<FormDataBodyPart> bodyPartList) { if (bodyPartList == null) { return null; } List<String> stringList = new ArrayList<String>(); for (FormDataBodyPart fdbp : bodyPartList) { stringList.add(fdbp.getValueAs(String.class)); } return stringList; }
@POST @Path("somepath") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFileAndJSON(@FormDataParam("model") FormDataBodyPart jsonPart, @FormDataParam("file") FormDataBodyPart bodyPart) { jsonPart.setMediaType(MediaType.APPLICATION_JSON_TYPE); SomeModel model = jsonPart.getValueAs(SomeModel.class); }
private <T> T getFormDataFromMultiPartRequestAs (Class<T> clazz, FormDataMultiPart form, String paramName) { T result = null; try { FormDataBodyPart part = form.getField(paramName); if (part != null) { result = part.getValueAs(clazz); } } catch (Exception e) { LOG.debug("Cannot get param " + paramName + " as" + clazz + " from multipart form" ); } return result; } }
@POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile(FormDataMultiPart formDataMultiPart) { FormDataBodyPart filePart = formDataMultiPart.getField("file"); InputStream fileInputStream = filePart.getValueAs(InputStream.class); . . . }
@POST @Consumes("multipart/form-data") public Response post(@FormDataParam("file") List<FormDataBodyPart> files) { for (FormDataBodyPart file: files) { FormDataContentDisposition fdcd = file.getFormDataContentDisposition(); String fileName = fdcd = getFileName(); InputStream is = file.getValueAs(InputStream.class); } }
@POST @Path("/upload1") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response createFile1(@FormDataParam("doc") FormDataBodyPart part) { part.setMediaType(MediaType.APPLICATION_JSON_TYPE); Test doc = part.getValueAs(Test.class); return Response.ok(doc.getName()).build(); }
@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 }
@Path("/upload") @POST @Consumes(MediaType.MULTIPART_FORM_DATA) public ResponseDTO doUpload(FormDataMultiPart multiPartData) { // non-file fields final String imageId = multiPartData.getField("<your_field_name>").getValue(); // for file field final FormDataBodyPart filePart = multiPartData.getField("<file_field_name>"); final ContentDisposition fileDetails = filePart.getContentDisposition(); final InputStream fileInputStream = filePart.getValueAs(InputStream.class); // use the above fields as required // file name can be accessed from field "fileDetails" }
/** * 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 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 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(); } }