@PUT @Path("/someurl") @Consumes("multipart/form-data") @Produces("text/plain;charset=UTF-8") public String someUrl(MultipartFormDataInput form) { try { List<InputPart> l = form.getFormDataMap().get("topics"); String topic1 = l.get(0).getBodyAsString(); String topic2 = l.get(1).getBodyAsString(); String topic3 = l.get(2).getBodyAsString(); // do something with the topics ... } catch (IOException e) { e.printStackTrace(); } return "something went wrong"; }
@Nonnull @SuppressWarnings("PMD.CyclomaticComplexity") private InputStream getBinaryArtifact(MultipartFormDataInput input) { if (input == null || input.getParts() == null || input.getParts().isEmpty()) { throw new IllegalArgumentException("Multipart request is empty"); } try { InputStream result; if (input.getParts().size() == 1) { InputPart filePart = input.getParts().iterator().next(); result = filePart.getBody(InputStream.class, null); } else { result = input.getFormDataPart("file", InputStream.class, null); } if (result == null) { throw new IllegalArgumentException("Can't find a valid 'file' part in the multipart request"); } return result; } catch (IOException e) { throw new IllegalArgumentException("Error while reading multipart request", e); } }
@SuppressWarnings("PMD.CyclomaticComplexity") public Connector create(MultipartFormDataInput dataInput) { if (dataInput == null || dataInput.getParts() == null || dataInput.getParts().isEmpty()) { throw new IllegalArgumentException("Multipart request is empty"); if (dataInput.getParts().size() != 1) { throw new IllegalArgumentException("Wrong number of parts in multipart request"); InputPart filePart = dataInput.getParts().iterator().next(); InputStream result = filePart.getBody(InputStream.class, null);
@POST @Path("/form") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.TEXT_PLAIN) public Response uploadForm(MultipartFormDataInput input) throws IOException { String charset = input.getFormDataMap().get("_charset_").get(0).getBodyAsString(); InputPart file = input.getFormDataMap().get("file").get(0); InputStream inputStream = file.getBody(InputStream.class, null); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset)); String line; StringBuilder content = new StringBuilder(); while ((line = br.readLine()) != null) { content.append(line); } return Response.ok(content).header("Content-Type", MediaType.TEXT_PLAIN_TYPE.withCharset(charset)).build(); }
@Test public void testService() { // given MultipartFormDataInput newForm = mock(MultipartFormDataInput.class); InputPart token = mock(InputPart.class); Map<String, List<InputPart>> paramsMap = new HashMap<>(); paramsMap.put("Token", Arrays.asList(token)); when(newForm.getFormDataMap()).thenReturn(paramsMap); when(token.getBodyAsString()).thenReturn("expected token param body"); // when DeviceCodeModel actual = _service.requestDeviceCode(newForm); // then // verifications and assertions go here }
@POST @Path("/") @Consumes("multipart/form-data") public Response uploadFileMultipart(MultipartFormDataInput input) { Map<String, List<InputPart>> uploadForm = input.getFormDataMap(); List<InputPart> inputParts = uploadForm.get("video"); String videoFileName = "GENERATE_YOUR_FILENAME_HERE.mp4"; File file = new File(filename); if (!file.exists()) { file.createNewFile(); } FileOutputStream fop = new FileOutputStream(file); for (InputPart inputPart : inputParts) { InputStream inputStream = inputPart.getBody(InputStream.class, null); byte[] content = IOUtils.toByteArray(inputStream); fop.write(content); } fop.flush(); fop.close(); return Response.status(HttpStatus.SC_OK).build(); }
Map<String, List<InputPart>> formParts = input.getFormDataMap();
@POST @Path("/insertDataInDB") @Consumes(MediaType.MULTIPART_FORM_DATA) public void insertDataInDB2(@FormDataParam("inputfile") MultipartFormDataInput inputfile) { Map<String, List<InputPart>> uploadForm = inputfile.getFormDataMap(); List<InputPart> inputParts = uploadForm.get("inputfile"); //i have only one inputPart InputPart inputPart = inputParts.get(0); try { InputStream inputStream = inputPart.getBody(InputStream.class, null); XSSFWorkbook workbook = new XSSFWorkbook(inputStream); XSSFSheet sheet = workbook.getSheetAt(0); System.out.println(sheet.getSheetName()); } catch (IOException e) { e.printStackTrace(); } }
public String getBodyPartAsString (List<InputPart> parts) throws IOException { InputPart part = parts.get(0); String value = part.getBody(String.class, null); return value; } @POST @Consumes(MediaType.MULTIPART_FORM_DATA) public Response create (@Context UriInfo uriInfo, @Context HttpServletRequest req, MultipartFormDataInput input) throws IOException, ParseException { Map<String, List<InputPart>> formParts = input.getFormDataMap(); if (!formParts.containsKey("model")) { throw new IllegalArgumentException("Cannot create document due to param missing (model)"); } //Parsea los datos y los pone en el DTO String str = getBodyPartAsString(formParts.get("model")); }
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
Map<String, List<InputPart>> inputParameters = form.getFormDataMap();
Map<String, List<InputPart>> inputParameters = form.getFormDataMap();
Map<String, List<InputPart>> formParts = form.getFormDataMap();
Objects.requireNonNull(form); try { Map<String, List<InputPart>> formParts = form.getFormDataMap(); Utils.Format format = Utils.getFormat(formParts);