本文整理汇总了Java中org.apache.http.entity.mime.content.InputStreamBody类的典型用法代码示例。如果您正苦于以下问题:Java InputStreamBody类的具体用法?Java InputStreamBody怎么用?Java InputStreamBody使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InputStreamBody类属于org.apache.http.entity.mime.content包,在下文中一共展示了InputStreamBody类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testInputStreamBody
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
@Test
public void testInputStreamBody() throws Exception {
final byte[] stuff = "Stuff".getBytes(Consts.ASCII);
final InputStreamBody b1 = new InputStreamBody(new ByteArrayInputStream(stuff), "stuff");
Assert.assertEquals(-1, b1.getContentLength());
Assert.assertNull(b1.getCharset());
Assert.assertEquals("stuff", b1.getFilename());
Assert.assertEquals("application/octet-stream", b1.getMimeType());
Assert.assertEquals("application", b1.getMediaType());
Assert.assertEquals("octet-stream", b1.getSubType());
Assert.assertEquals(MIME.ENC_BINARY, b1.getTransferEncoding());
final InputStreamBody b2 = new InputStreamBody(
new ByteArrayInputStream(stuff), ContentType.create("some/stuff"), "stuff");
Assert.assertEquals(-1, b2.getContentLength());
Assert.assertNull(b2.getCharset());
Assert.assertEquals("stuff", b2.getFilename());
Assert.assertEquals("some/stuff", b2.getMimeType());
Assert.assertEquals("some", b2.getMediaType());
Assert.assertEquals("stuff", b2.getSubType());
Assert.assertEquals(MIME.ENC_BINARY, b2.getTransferEncoding());
}
示例2: materialAdd_material
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
/**
* 新增其他类型永久素材
* @param access_token
* @param mediaType
* @param inputStream 多媒体文件有格式和大小限制,如下:
图片(image): 128K,支持JPG格式
语音(voice):256K,播放长度不超过60s,支持AMR\MP3格式
视频(video):1MB,支持MP4格式
缩略图(thumb):64KB,支持JPG格式
* @param description 视频文件类型额外字段,其它类型不用添加
* @return
*/
public static Media materialAdd_material(String access_token,MediaType mediaType,InputStream inputStream,Description description){
HttpPost httpPost = new HttpPost(MEDIA_URI+"/cgi-bin/material/add_material");
@SuppressWarnings("deprecation")
InputStreamBody inputStreamBody = new InputStreamBody(inputStream, mediaType.mimeType(),"temp."+mediaType.fileSuffix());
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create()
.addPart("media", inputStreamBody);
if(description != null){
multipartEntityBuilder.addTextBody("description", JsonUtil.toJSONString(description));
}
HttpEntity reqEntity = multipartEntityBuilder.addTextBody("access_token", access_token)
.addTextBody("type",mediaType.uploadType())
.build();
httpPost.setEntity(reqEntity);
return LocalHttpClient.executeJsonResult(httpPost,Media.class);
}
示例3: testOTAWrongToken
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
@Test
public void testOTAWrongToken() throws Exception {
HttpPost post = new HttpPost(httpsAdminServerUrl + "/ota/start?token=" + 123);
post.setHeader(HttpHeaderNames.AUTHORIZATION.toString(), "Basic " + Base64.getEncoder().encodeToString(auth));
String fileName = "test.bin";
InputStream binFile = OTATest.class.getResourceAsStream("/static/ota/" + fileName);
ContentBody fileBody = new InputStreamBody(binFile, ContentType.APPLICATION_OCTET_STREAM, fileName);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("upfile", fileBody);
HttpEntity entity = builder.build();
post.setEntity(entity);
try (CloseableHttpResponse response = httpclient.execute(post)) {
assertEquals(400, response.getStatusLine().getStatusCode());
String error = consumeText(response);
assertNotNull(error);
assertEquals("Invalid token.", error);
}
}
示例4: testAuthorizationFailed
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
@Test
public void testAuthorizationFailed() throws Exception {
HttpPost post = new HttpPost(httpsAdminServerUrl + "/ota/start?token=" + 123);
post.setHeader(HttpHeaderNames.AUTHORIZATION.toString(), "Basic " + Base64.getEncoder().encodeToString("123:123".getBytes()));
String fileName = "test.bin";
InputStream binFile = OTATest.class.getResourceAsStream("/static/ota/" + fileName);
ContentBody fileBody = new InputStreamBody(binFile, ContentType.APPLICATION_OCTET_STREAM, fileName);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("upfile", fileBody);
HttpEntity entity = builder.build();
post.setEntity(entity);
try (CloseableHttpResponse response = httpclient.execute(post)) {
assertEquals(403, response.getStatusLine().getStatusCode());
String error = consumeText(response);
assertNotNull(error);
assertEquals("Authentication failed.", error);
}
}
示例5: basicOTAForNonExistingSingleUser
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
@Test
public void basicOTAForNonExistingSingleUser() throws Exception {
HttpPost post = new HttpPost(httpsAdminServerUrl + "/ota/[email protected]");
post.setHeader(HttpHeaderNames.AUTHORIZATION.toString(), "Basic " + Base64.getEncoder().encodeToString(auth));
String fileName = "test.bin";
InputStream binFile = OTATest.class.getResourceAsStream("/static/ota/" + fileName);
ContentBody fileBody = new InputStreamBody(binFile, ContentType.APPLICATION_OCTET_STREAM, fileName);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("upfile", fileBody);
HttpEntity entity = builder.build();
post.setEntity(entity);
try (CloseableHttpResponse response = httpclient.execute(post)) {
assertEquals(400, response.getStatusLine().getStatusCode());
String er = consumeText(response);
assertNotNull(er);
assertEquals("Requested user not found.", er);
}
}
示例6: makePost
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
/** Utility method:
* Formulate an HTTP POST request to upload the batch query file
* @param queryBody
* @return
* @throws UnsupportedEncodingException
*/
private HttpPost makePost(String queryBody) throws UnsupportedEncodingException {
HttpPost post = new HttpPost(ClueWebSearcher.BATCH_CATB_BASEURL);
InputStreamBody qparams =
new InputStreamBody(
new ReaderInputStream(new StringReader(queryBody)),
"text/plain",
"query.txt");
MultipartEntity entity = new MultipartEntity();
entity.addPart("viewstatus", new StringBody("1"));
entity.addPart("indextype", new StringBody("catbparams"));
entity.addPart("countmax", new StringBody("100"));
entity.addPart("formattype", new StringBody(format));
entity.addPart("infile", qparams);
post.setEntity(entity);
return post;
}
示例7: request
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
private JSON request(HttpEntityEnclosingRequestBase req, Issue.NewAttachment... attachments)
throws RestException, IOException {
if (attachments != null) {
req.setHeader("X-Atlassian-Token", "nocheck");
MultipartEntity ent = new MultipartEntity();
for(Issue.NewAttachment attachment : attachments) {
String filename = attachment.getFilename();
Object content = attachment.getContent();
if (content instanceof byte[]) {
ent.addPart("file", new ByteArrayBody((byte[]) content, filename));
} else if (content instanceof InputStream) {
ent.addPart("file", new InputStreamBody((InputStream) content, filename));
} else if (content instanceof File) {
ent.addPart("file", new FileBody((File) content, filename));
} else if (content == null) {
throw new IllegalArgumentException("Missing content for the file " + filename);
} else {
throw new IllegalArgumentException(
"Expected file type byte[], java.io.InputStream or java.io.File but provided " +
content.getClass().getName() + " for the file " + filename);
}
}
req.setEntity(ent);
}
return request(req);
}
示例8: postUpload
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
@Override
public <V> V postUpload(String uri, Map<String, String> stringParts, InputStream in, String mimeType, String fileName,
final long fileSize, Class<V> type) throws IOException {
HttpPost request = new HttpPost(createURI(uri));
configureRequest(request);
MultipartEntity entity = new MultipartEntity();
for(Map.Entry<String, String> entry : stringParts.entrySet())
entity.addPart(entry.getKey(), StringBody.create(entry.getValue(), "text/plain", UTF_8));
entity.addPart("file", new InputStreamBody(in, mimeType, fileName) {
@Override
public long getContentLength() {
return fileSize;
}
});
request.setEntity(entity);
return executeRequest(request, type, null);
}
示例9: addBitmap
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
public static void addBitmap(MultipartEntity multipartEntity, Bitmap image,
String imageId) {
int measureId = PerformanceMeasure.startMeasure();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
image.compress(CompressFormat.JPEG, BitmapUtils.JPEG_COMPRESSION_LEVEL,
bos);
ByteArrayInputStream bs = new ByteArrayInputStream(bos.toByteArray());
InputStreamBody body = new InputStreamBody(bs, "image/jpeg", imageId);
multipartEntity.addPart(imageId, body);
PerformanceMeasure.endMeasure(measureId, "Jpeg compression time");
Log.d(TAG,
"Multipart entry after bitmap addition: "
+ multipartEntity.toString());
}
示例10: createRequest
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
@Override
protected HttpUriRequest createRequest() throws IOException {
MultipartEntity entity = new MultipartEntity();
List<NameValuePair> dataList = getData();
for (NameValuePair d : dataList) {
entity.addPart(new FormBodyPart(d.getName(), new StringBody(d.getValue(), Charset.forName("utf-8"))));
}
for (Map.Entry<String, InputStreamBody> entry : files.entrySet()) {
entity.addPart(new FormBodyPart(entry.getKey(), entry.getValue()));
}
final HttpPost request = new HttpPost(url);
if(this.sendProgressListener != null) {
CountingRequestEntity countingEntity = new CountingRequestEntity(entity, this.sendProgressListener);
request.setEntity(countingEntity);
} else {
request.setEntity(entity);
}
return request;
}
示例11: getNewFileMultipartEntity
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
private static MultipartEntityWithProgressListener getNewFileMultipartEntity(final String parentId, final InputStream inputStream, final String fileName)
throws BoxRestException, UnsupportedEncodingException {
MultipartEntityWithProgressListener me = new MultipartEntityWithProgressListener(HttpMultipartMode.BROWSER_COMPATIBLE);
me.addPart(Constants.FOLDER_ID, new StringBody(parentId));
String date = ISO8601DateParser.toString(new Date());
if (me.getPart(KEY_CONTENT_CREATED_AT) == null) {
me.addPart(KEY_CONTENT_CREATED_AT, new StringBody(date));
}
if (me.getPart(KEY_CONTENT_MODIFIED_AT) == null) {
me.addPart(KEY_CONTENT_MODIFIED_AT, new StringBody(date));
}
me.addPart(fileName, new InputStreamBody(inputStream, fileName));
return me;
}
示例12: newContentBody
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
private ContentBody newContentBody(Part part) {
ContentType contentType = ContentType.create(part.getMediaType());
if (part instanceof TextPart) {
TextPart textPart = (TextPart)part;
return new StringBody(textPart.getText(), contentType);
}
BinaryPart binaryPart = (BinaryPart)part;
return new InputStreamBody(binaryPart.getData(), contentType, binaryPart.getDownloadName());
}
示例13: testNonRepeatable
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
@Test
public void testNonRepeatable() throws Exception {
final HttpEntity entity = MultipartEntityBuilder.create()
.addPart("p1", new InputStreamBody(
new ByteArrayInputStream("blah blah".getBytes()), ContentType.DEFAULT_BINARY))
.addPart("p2", new InputStreamBody(
new ByteArrayInputStream("yada yada".getBytes()), ContentType.DEFAULT_BINARY))
.build();
Assert.assertFalse(entity.isRepeatable());
Assert.assertTrue(entity.isChunked());
Assert.assertTrue(entity.isStreaming());
Assert.assertTrue(entity.getContentLength() == -1);
}
示例14: sendPostUploadFile
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
public Observable<String> sendPostUploadFile(InputStream inputStream,
String filename, String pid,
Map<String, String> parameters) {
return Observable.create(o -> {
try {
postConfigureStart(pid, parameters);
// загрузка файла
InputStreamBody bin = new InputStreamBody(inputStream,
filename);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bundlefile", bin);
reqEntity.addPart("action", new StringBody("install"));
reqEntity.addPart("bundlestart", new StringBody("start"));
reqEntity.addPart("bundlestartlevel", new StringBody("1"));
postHttpClientQuery(URL_BUNDLES, reqEntity, Collections
.list(new BasicHeader("Accept", "application/json")));
o.onNext("");
} catch (Exception e) {
throw Exceptions.propagate(e);
}
o.onCompleted();
}).subscribeOn(Schedulers.from(mes)).cast(String.class);
}
示例15: mediaUpload
import org.apache.http.entity.mime.content.InputStreamBody; //导入依赖的package包/类
/**
* 上传媒体文件
* 媒体文件在后台保存时间为3天,即3天后media_id失效。
* @param access_token
* @param mediaType
* @param inputStream 多媒体文件有格式和大小限制,如下:
图片(image): 128K,支持JPG格式
语音(voice):256K,播放长度不超过60s,支持AMR\MP3格式
视频(video):1MB,支持MP4格式
缩略图(thumb):64KB,支持JPG格式
* @return
*/
public static Media mediaUpload(String access_token,MediaType mediaType,InputStream inputStream){
HttpPost httpPost = new HttpPost(MEDIA_URI+"/cgi-bin/media/upload");
@SuppressWarnings("deprecation")
InputStreamBody inputStreamBody = new InputStreamBody(inputStream, mediaType.mimeType(),"temp."+mediaType.fileSuffix());
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("media",inputStreamBody)
.addTextBody("access_token", access_token)
.addTextBody("type",mediaType.uploadType())
.build();
httpPost.setEntity(reqEntity);
return LocalHttpClient.executeJsonResult(httpPost,Media.class);
}