本文整理了Java中org.glassfish.jersey.media.multipart.MultiPart.<init>()
方法的一些代码示例,展示了MultiPart.<init>()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MultiPart.<init>()
方法的具体详情如下:
包路径:org.glassfish.jersey.media.multipart.MultiPart
类名称:MultiPart
方法名:<init>
[英]Instantiates a new MultiPart with a mediaType of multipart/mixed.
[中]用MultiPart/mixed的mediaType实例化新的MultiPart。
代码示例来源:origin: stackoverflow.com
public class Slimclient {
private static final String TARGET_URL = "http://localhost:49158/rest/service/upload";
public Slimclient() {
Client client = ClientBuilder.newBuilder()
.register(MultiPartFeature.class).build();
WebTarget webTarget = client.target(TARGET_URL);
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
new File("C:/Users/Nicklas/Desktop/aab.txt"),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.bodyPart(fileDataBodyPart);
Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(multiPart, multiPart.getMediaType()));
System.out.println(response.getStatus() + " "
+ response.getStatusInfo() + " " + response);
}
public static void main(String[] args) {
new Slimclient();
}
}
代码示例来源:origin: OryxProject/oryx
protected final Response getFormPostResponse(String data,
String endpoint,
Class<? extends OutputStream> compressingClass,
String encoding) throws IOException {
byte[] bytes;
if (compressingClass == null) {
bytes = data.getBytes(StandardCharsets.UTF_8);
} else {
bytes = compress(data, compressingClass);
}
MediaType type =
encoding == null ? MediaType.TEXT_PLAIN_TYPE : new MediaType("application", encoding);
InputStream in = new ByteArrayInputStream(bytes);
StreamDataBodyPart filePart = new StreamDataBodyPart("data", in, "data", type);
try (MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE)) {
multiPart.getBodyParts().add(filePart);
return target(endpoint).request().post(
Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE));
}
}
代码示例来源:origin: jersey/jersey
final MultiPart multiPart = formData ? new FormDataMultiPart() : new MultiPart();
代码示例来源:origin: stackoverflow.com
@Produces("multipart/mixed")
public Response multiPartResponder(){
MultiPart multiPart = new MultiPart();
BodyPart bodyPart = new BodyPart(myFileInputStream, MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.bodyPart(bodyPart);
return Response.status(OK).entity(multiPart);
}
代码示例来源:origin: stackoverflow.com
final MultiPart multiPartEntity = new MultiPart()
.bodyPart(new BodyPart().entity("hello"))
.bodyPart(new BodyPart(new JaxbBean("xml"), MediaType.APPLICATION_XML_TYPE))
.bodyPart(new BodyPart(new JaxbBean("json"), MediaType.APPLICATION_JSON_TYPE));
final WebTarget target = // Create WebTarget.
final Response response = target
.request()
.post(Entity.entity(multiPartEntity, multiPartEntity.getMediaType()));
代码示例来源:origin: hortonworks/registry
@Override
public String uploadFile(InputStream inputStream) {
MultiPart multiPart = new MultiPart();
BodyPart filePart = new StreamDataBodyPart("file", inputStream, "file");
multiPart.bodyPart(filePart);
return Subject.doAs(subject, new PrivilegedAction<String>() {
@Override
public String run() {
return currentSchemaRegistryTargets().filesTarget.request()
.post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA), String.class);
}
});
}
代码示例来源:origin: com.thinkbiganalytics.kylo/kylo-commons-rest-client
/**
* POST a multipart object streaming
*
* @param path the path to access
* @param name the name of the param the endpoint is expecting
* @param fileName the name of the file
* @param stream the stream itself
* @param returnType the type to return from the post
* @return the response of type T
*/
public <T> T postMultiPartStream(String path, String name, String fileName, InputStream stream, Class<T> returnType) {
WebTarget target = buildTarget(path, null);
MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE);
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart(name, stream, fileName, MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.getBodyParts().add(streamDataBodyPart);
MediaType contentType = MediaType.MULTIPART_FORM_DATA_TYPE;
contentType = Boundary.addBoundary(contentType);
return target.request().post(
Entity.entity(multiPart, contentType), returnType);
}
代码示例来源:origin: stackoverflow.com
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);
代码示例来源:origin: stackoverflow.com
byte[] bin = some binary data...
Book b = new Book();
b.setAuthor("John");
b.setTitle("wild stuff");
b.setYear(2012);
MultiPart multiPart = new MultiPart();
multiPart.bodyPart(new BodyPart(b, MediaType.APPLICATION_XML_TYPE));
multiPart.bodyPart(new BodyPart(bin, MediaType.APPLICATION_OCTET_STREAM_TYPE));
response = service.path("rest").path("multipart").
type(MultiPartMediaTypes.MULTIPART_MIXED).
post(ClientResponse.class, multiPart);
代码示例来源:origin: batfish/batfish
@Nullable
String checkApiKey() {
try {
WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_CHECK_API_KEY);
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
JSONObject jObj = postData(webTarget, multiPart);
if (jObj == null) {
return null;
}
return Boolean.toString(jObj.getBoolean(CoordConsts.SVC_KEY_API_KEY));
} catch (Exception e) {
_logger.errorf("exception: ");
_logger.error(Throwables.getStackTraceAsString(e) + "\n");
return null;
}
}
代码示例来源:origin: batfish/batfish
boolean delAnalysis(String networkName, String analysisName) {
try {
WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_DEL_ANALYSIS);
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_ANALYSIS_NAME, analysisName);
return postData(webTarget, multiPart) != null;
} catch (Exception e) {
_logger.errorf(
"Exception when deleting analysis to %s using (%s, %s): %s\n",
_coordWorkMgr, networkName, analysisName, Throwables.getStackTraceAsString(e));
return false;
}
}
代码示例来源:origin: batfish/batfish
boolean uploadQuestion(String networkName, String snapshotName, String qName, String qFileName) {
try {
WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_UPLOAD_QUESTION);
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_SNAPSHOT_NAME, snapshotName);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_QUESTION_NAME, qName);
addFileMultiPart(multiPart, CoordConsts.SVC_KEY_FILE, qFileName);
return postData(webTarget, multiPart) != null;
} catch (Exception e) {
if (e.getMessage().contains("FileNotFoundException")) {
_logger.errorf("File not found: %s (question file)\n", qFileName);
} else {
_logger.errorf(
"Exception when uploading question to %s using (%s, %s, %s): %s\n",
_coordWorkMgr, snapshotName, qName, qFileName, Throwables.getStackTraceAsString(e));
}
return false;
}
}
代码示例来源:origin: batfish/batfish
boolean killWork(UUID workId) {
try {
WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_KILL_WORK);
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_WORKID, workId.toString());
JSONObject jObj = postData(webTarget, multiPart);
if (jObj == null) {
return false;
}
if (!jObj.has(CoordConsts.SVC_KEY_RESULT)) {
_logger.errorf("result key not found in: %s\n", jObj);
return false;
}
return jObj.getBoolean(CoordConsts.SVC_KEY_RESULT);
} catch (Exception e) {
_logger.errorf("exception: %s\n", Throwables.getStackTraceAsString(e));
return false;
}
}
代码示例来源:origin: batfish/batfish
boolean queueWork(WorkItem wItem) {
try {
WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_QUEUE_WORK);
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
addTextMultiPart(
multiPart, CoordConsts.SVC_KEY_WORKITEM, BatfishObjectMapper.writeString(wItem));
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
JSONObject jObj = postData(webTarget, multiPart);
return jObj != null;
} catch (Exception e) {
_logger.errorf("exception: ");
_logger.error(Throwables.getStackTraceAsString(e) + "\n");
return false;
}
}
代码示例来源:origin: batfish/batfish
boolean delSnapshot(String networkName, String snapshotName) {
try {
WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_DEL_SNAPSHOT);
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_SNAPSHOT_NAME, snapshotName);
JSONObject jObj = postData(webTarget, multiPart);
return jObj != null;
} catch (Exception e) {
_logger.errorf("Exception in delSnapshot for network %s:\n", networkName);
_logger.error(Throwables.getStackTraceAsString(e) + "\n");
return false;
}
}
代码示例来源:origin: batfish/batfish
boolean syncSnapshotsSyncNow(String pluginId, String networkName, boolean force) {
try {
WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_SYNC_SNAPSHOTS_SYNC_NOW);
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_VERSION, Version.getVersion());
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_PLUGIN_ID, pluginId);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_FORCE, String.valueOf(force));
JSONObject jObj = postData(webTarget, multiPart);
return jObj != null;
} catch (Exception e) {
_logger.errorf("Exception syncing snapshots in network %s:\n", networkName);
_logger.error(Throwables.getStackTraceAsString(e) + "\n");
return false;
}
}
代码示例来源:origin: batfish/batfish
boolean delQuestion(String networkName, String snapshotName, String questionName) {
try {
WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_DEL_QUESTION);
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_SNAPSHOT_NAME, snapshotName);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_QUESTION_NAME, questionName);
JSONObject jObj = postData(webTarget, multiPart);
return jObj != null;
} catch (Exception e) {
_logger.errorf("exception: ");
_logger.error(Throwables.getStackTraceAsString(e) + "\n");
return false;
}
}
代码示例来源:origin: Texera/texera
@Test
@Ignore
public void checkDictionaryUpload() throws Exception {
Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test client");
client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
client.property(ClientProperties.READ_TIMEOUT, 5000);
client.register(MultiPartFeature.class);
final MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
File testDictionaryFile = new File(ResourceHelpers.resourceFilePath("test_dictionary.txt"));
final FileDataBodyPart filePart = new FileDataBodyPart("file", testDictionaryFile);
multiPart.bodyPart(filePart);
Response response = client.target(
String.format("http://localhost:%d/api/upload/dictionary", RULE.getLocalPort()))
.request(MediaType.MULTIPART_FORM_DATA_TYPE)
.post(Entity.entity(multiPart, multiPart.getMediaType()));
assertThat(response.getStatus()).isEqualTo(200);
}
}
代码示例来源:origin: batfish/batfish
boolean syncSnapshotsUpdateSettings(
String pluginId, String networkName, Map<String, String> settings) {
try {
String settingsStr = BatfishObjectMapper.writePrettyString(settings);
WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_SYNC_SNAPSHOTS_UPDATE_SETTINGS);
MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_VERSION, Version.getVersion());
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_PLUGIN_ID, pluginId);
addTextMultiPart(multiPart, CoordConsts.SVC_KEY_SETTINGS, settingsStr);
JSONObject jObj = postData(webTarget, multiPart);
return jObj != null;
} catch (Exception e) {
_logger.errorf("Exception syncing snapshots in network %s:\n", networkName);
_logger.error(Throwables.getStackTraceAsString(e) + "\n");
return false;
}
}
代码示例来源:origin: hortonworks/registry
protected MultiPart getMultiPart(ResourceTestElement resourceToTest, Object entity) {
MultiPart multiPart = new MultiPart();
BodyPart filePart = new FileDataBodyPart(resourceToTest.getFileNameHeader(), resourceToTest.getFileToUpload());
BodyPart entityPart = new FormDataBodyPart(resourceToTest.getEntityNameHeader(), entity, MediaType.APPLICATION_JSON_TYPE);
multiPart.bodyPart(filePart).bodyPart(entityPart);
return multiPart;
}
内容来源于网络,如有侵权,请联系作者删除!