List<File> files = ( List<File> ) param.getValue(); for( File file : files ) { mp.bodyPart( new FileDataBodyPart( param.getKey(), file, MediaType.APPLICATION_OCTET_STREAM_TYPE ) ); mp.bodyPart(new FileDataBodyPart(param.getKey(), file, MediaType.APPLICATION_OCTET_STREAM_TYPE)); } else { mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE);
WebTarget t = client.target(Main.BASE_URI).path("multipart").path("upload2"); FileDataBodyPart filePart = new FileDataBodyPart("file", new File("stackoverflow.png")); filePart.setContentDisposition( FormDataContentDisposition.name("file") .fileName("stackoverflow.png").build());
protected Iterable<BodyPart> prepareUploads(final Iterable<File> files) { final List<BodyPart> bodyParts = new ArrayList<BodyPart>(); for(final File file : files) { final String paramName = "files_0|file_data"; final FileDataBodyPart fdbp = new FileDataBodyPart(paramName, file); bodyParts.add(fdbp); } return bodyParts; }
@Test public void testPostWithFileSuccess() throws Exception { FileDataBodyPart filePart = new FileDataBodyPart("file", new File(Resources.getResource("resources/testImage.jpg").toURI())); filePart.setContentDisposition(FormDataContentDisposition.name("file").fileName("testImage.jpg").build()); FormDataMultiPart request = new FormDataMultiPart(); request.field("data", fixture("resources/postWithFileSuccess.json"), MediaType.APPLICATION_JSON_TYPE); request.bodyPart(filePart); WebTarget target = mClient.target("http://localhost:8080/crackers").register(MultiPartFeature.class); Response response = target .request() .post(Entity.entity(request, MediaType.MULTIPART_FORM_DATA)); assertThat(response.getStatus()).isEqualTo(200); }
public AtlasImportResult importData(AtlasImportRequest request, String absoluteFilePath) throws AtlasServiceException { return performImportData(getImportRequestBodyPart(request), new FileDataBodyPart(IMPORT_DATA_PARAMETER, new File(absoluteFilePath))); }
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", fileToUpload, MediaType.APPLICATION_OCTET_STREAM_TYPE); fileDataBodyPart.setContentDisposition( FormDataContentDisposition.name("file") .fileName(fileToUpload.getName()).build());
@Override public Void call() throws Exception { final MultiPart multiPartInput = new MultiPart(); for (File file : myFiles) { FileDataBodyPart fileDataBodyPart = new FileDataBodyPart(FILE_ATTACHMENT_CONTROL_NAME, file); multiPartInput.bodyPart(fileDataBodyPart); } postFileMultiPart(multiPartInput, attachmentsUri); return null; }
@Override public Void call() throws Exception { final MultiPart multiPartInput = new MultiPart(); for (File file : myFiles) { FileDataBodyPart fileDataBodyPart = new FileDataBodyPart(FILE_ATTACHMENT_CONTROL_NAME, file); multiPartInput.bodyPart(fileDataBodyPart); } postFileMultiPart(multiPartInput, attachmentsUri); return null; }
List<File> files = ( List<File> ) param.getValue(); for( File file : files ) { mp.bodyPart( new FileDataBodyPart( param.getKey(), file, MediaType.APPLICATION_OCTET_STREAM_TYPE ) ); mp.bodyPart(new FileDataBodyPart(param.getKey(), file, MediaType.APPLICATION_OCTET_STREAM_TYPE)); } else { mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE);
Client client = ClientBuilder.newClient(); client.register(MultiPartFeature.class); WebTarget target = client.target("https://apisomeurl.com"); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); //Image here FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("image", new File("/some/img/path/img.png")); multiPart.bodyPart(fileDataBodyPart); //MediaType.APPLICATION_JSON_TYPE because I'm expecting a JSON response from the server String str = target.queryParam("param1", "1") .queryParam("param2", "2") .queryParam("param3", "3") .request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.entity(multiPart, multiPart.getMediaType()), String.class);
public AtlasImportResult importData(AtlasImportRequest request, String absoluteFilePath) throws AtlasServiceException { FileDataBodyPart filePart = new FileDataBodyPart("data", new File(absoluteFilePath)); MultiPart multipartEntity = new FormDataMultiPart() .field("request", AtlasType.toJson(request), MediaType.APPLICATION_JSON_TYPE) .bodyPart(filePart); return callAPI(IMPORT, AtlasImportResult.class, multipartEntity); }
/** * Serialize the given Java object into string according the given * Content-Type (only JSON is supported for now). */ public Object serialize(Object obj, String contentType, Map<String, Object> formParams) throws ApiException { if (contentType.startsWith("multipart/form-data")) { FormDataMultiPart mp = new FormDataMultiPart(); for (Entry<String, Object> param: formParams.entrySet()) { if (param.getValue() instanceof File) { File file = (File) param.getValue(); mp.bodyPart(new FileDataBodyPart(param.getKey(), file, MediaType.MULTIPART_FORM_DATA_TYPE)); } else { mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE); } } return mp; } else if (contentType.startsWith("application/x-www-form-urlencoded")) { return this.getXWWWFormUrlencodedParams(formParams); } else if (contentType.startsWith("text/csv")) { return this.serializeToCsv(obj); } else { // We let Jersey attempt to serialize the body return obj; } }
@POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.TEXT_PLAIN) public String getTest(@FormDataParam("file") InputStream in, @FormDataParam("file") FormDataContentDisposition fdc) { try (FileOutputStream out = new FileOutputStream(fdc.getFileName())) { ReaderWriter.writeTo(in, out); } catch (IOException ex) { ex.printStackTrace(System.out); return "Bad $#!t happended"; } return "Upload OK"; } @Test public void doit() { File file = new File("..."); FileDataBodyPart filePart = new FileDataBodyPart("file", file); MultiPart entity = new FormDataMultiPart() .bodyPart(filePart); Client client = Client.create(); WebResource resource = client.resource("http://localhost:9998/service"); ClientResponse response = resource .type(MediaType.MULTIPART_FORM_DATA_TYPE) .post(ClientResponse.class, entity); assertEquals(200, response.getStatus()); assertEquals("Upload OK", response.getEntity(String.class)); response.close(); }
FileDataBodyPart filePart = new FileDataBodyPart("file", new File(file)); entity.bodyPart(filePart);
File file2 = new File(FILE_TWO); MultiPart entity = new FormDataMultiPart(); FileDataBodyPart filePart = new FileDataBodyPart("file", file1); entity.bodyPart(filePart); filePart = new FileDataBodyPart("file", file2); entity.bodyPart(filePart);
@Override public IOsKeyResponse postNewKey(WebResource resource, ResourcePath<PlatformKeys> platformResourcePath, IOsKeyRequest iOsKeyRequest, File cert, File profile) { try { FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(new FileDataBodyPart("cert", cert)); multiPart.bodyPart(new FileDataBodyPart("profile", profile)); multiPart.bodyPart(new FormDataBodyPart("data", iOsKeyRequest, MediaType.APPLICATION_JSON_TYPE)); return resource.path(platformResourcePath.getPath()).type(MediaType.MULTIPART_FORM_DATA_TYPE).post( IOsKeyResponse.class, multiPart); } catch (UniformInterfaceException e) { throw new ApiException(e.getResponse().getEntity(ErrorResponse.class), e); } }
@Override public AppResponse postNewApp(WebResource resource, ResourcePath<Apps> appsResponsePath, AppDetailsRequest appsRequest, File file) { try { FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(new FileDataBodyPart("file", file)); multiPart.bodyPart(new FormDataBodyPart("data", appsRequest, MediaType.APPLICATION_JSON_TYPE)); return resource.path(appsResponsePath.getPath()).type(MediaType.MULTIPART_FORM_DATA_TYPE).post(AppResponse.class, multiPart); } catch (UniformInterfaceException e) { throw new ApiException(e.getResponse().getEntity(ErrorResponse.class), e); } }
@Override public AndroidKeyResponse postNewKey(WebResource resource, ResourcePath<PlatformKeys> platformResourcePath, AndroidKeyRequest androidKeyRequest, File keystore) { try { FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(new FileDataBodyPart("keystore", keystore)); multiPart.bodyPart(new FormDataBodyPart("data", androidKeyRequest, MediaType.APPLICATION_JSON_TYPE)); return resource.path(platformResourcePath.getPath()).type(MediaType.MULTIPART_FORM_DATA_TYPE).post( AndroidKeyResponse.class, multiPart); } catch (UniformInterfaceException e) { throw new ApiException(e.getResponse().getEntity(ErrorResponse.class), e); } }
@Override public AppResponse putApp(WebResource resource, ResourcePath<App> appResourcePath, AppDetailsRequest appsRequest, File file) { try { FormDataMultiPart multiPart = new FormDataMultiPart(); multiPart.bodyPart(new FileDataBodyPart("file", file)); if (appsRequest != null) { multiPart.bodyPart(new FormDataBodyPart("data", appsRequest, MediaType.APPLICATION_JSON_TYPE)); } return resource.path(appResourcePath.getPath()).type(MediaType.MULTIPART_FORM_DATA_TYPE).put(AppResponse.class, multiPart); } catch (UniformInterfaceException e) { throw new ApiException(e.getResponse().getEntity(ErrorResponse.class), e); } }