本文整理汇总了Java中org.glassfish.jersey.media.multipart.MultiPart类的典型用法代码示例。如果您正苦于以下问题:Java MultiPart类的具体用法?Java MultiPart怎么用?Java MultiPart使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MultiPart类属于org.glassfish.jersey.media.multipart包,在下文中一共展示了MultiPart类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFormPostResponse
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
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));
}
}
示例2: testPassThroughPostMultiPart
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
/**
* Test to make sure that the proxy can proxy a redirect
* @throws Exception
*/
@Test
public void testPassThroughPostMultiPart() throws Exception {
final FileDataBodyPart filePart = new FileDataBodyPart("test_file", new File(TEST_IMAGE_PATH));
MultiPart multiPart = new FormDataMultiPart()
.field("foo", "bar")
.bodyPart(filePart);
Client multipartClient = ClientBuilder.newBuilder()
.register(MultiPartFeature.class)
.build();
String responseBody = multipartClient.target("http://localhost:" + RULE.getLocalPort() + "/proxy")
.request()
.header("proxy-url", "http://httpbin.org/post")
.post(Entity.entity(multiPart, multiPart.getMediaType()))
.readEntity(String.class);
assertTrue(responseBody.toLowerCase().contains("test_file"));
}
示例3: sendFax
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
@Override
public APIResponse sendFax(final String faxNumber,
final File[] filesToSendAsFax,
final Optional<SendFaxOptions> options) throws IOException, URISyntaxException {
MultiPart multiPart = new MultiPart();
int count = 1;
for (File file : filesToSendAsFax) {
String contentType = tika.detect(file);
String entityName = "file"+count++;
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart(entityName, file, MediaType.valueOf(contentType));
multiPart.bodyPart(fileDataBodyPart);
}
return sendMultiPartFax(faxNumber, multiPart, options);
}
示例4: testPostDoNotOwn
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
@Test
public void testPostDoNotOwn() throws IOException {
Focus focus = new Focus();
focus.setAuthoruserguid("88888888-8888-8888-8888-888888888888");
currentUser.setGuid("99999999-9999-9999-9999-999999999999");
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
MultiPart multipart = formDataMultiPart.bodyPart(new FileDataBodyPart("file", Paths.get("pom.xml").toFile()));
when(focusRepository.loadOrNotFound("00000000-0000-0000-0000-000000000000")).thenReturn(focus);
Response response = target().path("/picture/focus/00000000-0000-0000-0000-000000000000").request()
.post(Entity.entity(multipart, multipart.getMediaType()));
assertEquals(400, response.getStatus());
verify(focusRepository).loadOrNotFound("00000000-0000-0000-0000-000000000000");
formDataMultiPart.close();
}
示例5: shouldUploadAFile
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
@Test
public void shouldUploadAFile() {
Path path = Paths.get(System.getProperty("user.dir"),
"src", "test", "resources", "rest", "IFrameProxyTestData.html");
FileDataBodyPart filePart = new FileDataBodyPart("file", path.toFile());
filePart.setContentDisposition(FormDataContentDisposition.name("file")
.fileName("IFrameProxyTestData.html")
.build());
MultiPart multipartEntity = new FormDataMultiPart().bodyPart(filePart);
Response response = target("/projects/" + PROJECT_TEST_ID + "/files")
.register(MultiPartFeature.class)
.request()
.post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA));
System.out.println(" -> " + response.readEntity(String.class));
// assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}
示例6: convertScript
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
/**
* @{inheritDoc
*/
public ScriptTO convertScript(ScriptUploadRequest request, InputStream in) throws RestServiceException {
WebTarget webTarget = client.target(urlBuilder.buildUrl(ScriptService.METHOD_CONVERT_SCRIPT));
MultiPart multiPart = new MultiPart();
BodyPart bp = new FormDataBodyPart("file", in, MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.bodyPart(bp);
multiPart.bodyPart(new FormDataBodyPart("scriptUploadRequest", request, MediaType.APPLICATION_XML_TYPE));
Response response = webTarget.request().post(Entity.entity(multiPart,MediaType.MULTIPART_FORM_DATA_TYPE));
exceptionHandler.checkStatusCode(response);
String loc = response.getHeaders().getFirst("location").toString();
webTarget = client.target(loc);
response = webTarget.request(MediaType.APPLICATION_XML_TYPE).get();
exceptionHandler.checkStatusCode(response);
return response.readEntity(ScriptTO.class);
}
示例7: runAutomationJob
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
/**
* @{inheritDoc
*/
public String runAutomationJob(AutomationRequest request, File xmlFile)
throws RestServiceException {
WebTarget webTarget = client.target(baseUrl + METHOD_RUN_JOB);
MultiPart multiPart = new MultiPart();
if (xmlFile != null) {
BodyPart bp = new FileDataBodyPart("file", xmlFile);
multiPart.bodyPart(bp);
}
multiPart.bodyPart(new FormDataBodyPart("automationRequest", request,
MediaType.APPLICATION_XML_TYPE));
Response response = webTarget.request().post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE));
exceptionHandler.checkStatusCode(response);
return response.readEntity(String.class);
}
示例8: multipartToMap
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
/**
* Convert multi part body into a map of key value pairs.
*
* @param multiPart
* @return
* @throws IOException
*/
private Map<String, String> multipartToMap(final MultiPart multiPart) throws IOException {
final Map<String, String> map = new HashMap<>();
for (final BodyPart part : multiPart.getBodyParts()) {
Object value = part.getEntity();
if (value instanceof BodyPartEntity) {
value = IOUtils.toString(((BodyPartEntity) value).getInputStream());
}
if (value instanceof File) {
value = IOUtils.toString(new FileInputStream((File) value));
}
final String fieldName =
part.getHeaders().get("Content-Disposition").get(0)
.split("form-data;\\s*.*name=\"")[1].replaceFirst("\"$", "");
map.put(fieldName, ObjectUtils.toString(value));
}
return map;
}
示例9: uploadPath
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
private void uploadPath(final String name, final String id, final Path path) {
final Client client = ClientBuilder.newClient();
client.register(MultiPartFeature.class);
final WebTarget target = client.target(uri).path(
uri.getPath() + "_exposr/publish/" + name + "/" + id);
final FileDataBodyPart filePart = new FileDataBodyPart("file",
path.toFile(),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
final MultiPart multipart = new FormDataMultiPart().bodyPart(filePart);
Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
.put(Entity.entity(multipart, multipart.getMediaType()));
}
示例10: upload
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
private void upload(String server, String environment, File file, boolean dryRun) throws Exception {
JerseyClient client = new JerseyClientBuilder()
.register(HttpAuthenticationFeature.basic("user", "pass")) // set.getUser(), set.getPass()
.register(MultiPartFeature.class)
.build();
JerseyWebTarget t = client.target(UriBuilder.fromUri(server).build()).path("rest").path("items").path("upload");
FileDataBodyPart filePart = new FileDataBodyPart("file", file);
String fn = file.getName();
fn = fn.substring(0, fn.lastIndexOf("_report") + 7); // die tempnummer am ende des filenamens noch wegoperieren!
System.out.println(fn);
filePart.setContentDisposition(FormDataContentDisposition.name("file").fileName(fn).build());
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
MultiPart multipartEntity = formDataMultiPart.field("comment", "Analysis from BfR").bodyPart(filePart);
if (!dryRun) {
Response response = t.queryParam("environment", environment).request().post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA));
System.out.println(response.getStatus() + " \n" + response.readEntity(String.class));
response.close();
}
formDataMultiPart.close();
multipartEntity.close();
}
示例11: multipartPost
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
private Response multipartPost(String path, File resource, String mediaType, Map<String, String> arguments)
throws ParseException {
MultiPart formdata = new MultiPart();
arguments.forEach((key, value) -> formdata.bodyPart(new FormDataBodyPart(key, value)));
formdata.bodyPart(new FormDataBodyPart(
new FormDataContentDisposition(
"form-data; name=\"file\"; filename=\"" + resource.getName().replace("\"", "") + "\""
),
resource,
MediaType.valueOf(mediaType)
));
Response result = call(path)
.post(Entity.entity(formdata, "multipart/form-data; boundary=Boundary_1_498293219_1483974344746"));
return result;
}
示例12: createDataStore
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
/**
* Creates a new data store.
*
* @param file CSV file that should be uploaded (N.B. max 50MB)
* @param name name to use in the Load Impact web-console
* @param fromline Payload from this line (1st line is 1). Set to value 2, if the CSV file starts with a headings line
* @param separator field separator, one of {@link com.loadimpact.resource.DataStore.Separator}
* @param delimiter surround delimiter for text-strings, one of {@link com.loadimpact.resource.DataStore.StringDelimiter}
* @return {@link com.loadimpact.resource.DataStore}
*/
public DataStore createDataStore(final File file, final String name, final int fromline, final DataStore.Separator separator, final DataStore.StringDelimiter delimiter) {
return invoke(DATA_STORES,
new RequestClosure<JsonObject>() {
@Override
public JsonObject call(Invocation.Builder request) {
MultiPart form = new FormDataMultiPart()
.field("name", name)
.field("fromline", Integer.toString(fromline))
.field("separator", separator.param())
.field("delimiter", delimiter.param())
.bodyPart(new FileDataBodyPart("file", file, new MediaType("text", "csv")));
return request.post(Entity.entity(form, form.getMediaType()), JsonObject.class);
}
},
new ResponseClosure<JsonObject, DataStore>() {
@Override
public DataStore call(JsonObject json) {
return new DataStore(json);
}
}
);
}
示例13: getEntity
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
@Override
public Entity getEntity(List<MultiPartItem> items, String mediaType) {
MultiPart multiPart = new MultiPart();
for (MultiPartItem item : items) {
if (item.getValue() == null || item.getValue().isNull()) {
continue;
}
String name = item.getName();
String filename = item.getFilename();
ScriptValue sv = item.getValue();
String ct = item.getContentType();
if (ct == null) {
ct = HttpUtils.getContentType(sv);
}
MediaType itemType = MediaType.valueOf(ct);
if (name == null) { // most likely multipart/mixed
BodyPart bp = new BodyPart().entity(sv.getAsString()).type(itemType);
multiPart.bodyPart(bp);
} else if (filename != null) {
StreamDataBodyPart part = new StreamDataBodyPart(name, sv.getAsStream(), filename, itemType);
multiPart.bodyPart(part);
} else {
multiPart.bodyPart(new FormDataBodyPart(name, sv.getAsString(), itemType));
}
}
return Entity.entity(multiPart, mediaType);
}
示例14: sendMultiPartFax
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
private APIResponse sendMultiPartFax(String faxNumber, MultiPart multiPart, Optional<SendFaxOptions> options)
throws URISyntaxException {
final URI uri = getSendFaxUri(faxNumber, options);
return executePostRequest(
uri,
(target) ->
target
.request()
.header("Content-Type", "multipart/mixed")
.post(Entity.entity(multiPart, multiPart.getMediaType()))
);
}
示例15: uploadSchemaVersion
import org.glassfish.jersey.media.multipart.MultiPart; //导入依赖的package包/类
public SchemaIdVersion uploadSchemaVersion(final String schemaBranchName,
final String schemaName,
final String description,
final InputStream schemaVersionInputStream)
throws InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName);
if (schemaMetadataInfo == null) {
throw new SchemaNotFoundException("Schema with name " + schemaName + " not found");
}
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", schemaVersionInputStream);
WebTarget target = currentSchemaRegistryTargets().schemasTarget.path(schemaName).path("/versions/upload").queryParam("branch",schemaBranchName);
MultiPart multipartEntity =
new FormDataMultiPart()
.field("description", description, MediaType.APPLICATION_JSON_TYPE)
.bodyPart(streamDataBodyPart);
Entity<MultiPart> multiPartEntity = Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA);
Response response = Subject.doAs(subject, new PrivilegedAction<Response>() {
@Override
public Response run() {
return target.request().post(multiPartEntity, Response.class);
}
});
return handleSchemaIdVersionResponse(schemaMetadataInfo, response);
}