本文整理匯總了Java中org.apache.http.entity.InputStreamEntity.setContentType方法的典型用法代碼示例。如果您正苦於以下問題:Java InputStreamEntity.setContentType方法的具體用法?Java InputStreamEntity.setContentType怎麽用?Java InputStreamEntity.setContentType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.entity.InputStreamEntity
的用法示例。
在下文中一共展示了InputStreamEntity.setContentType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: transformResponse
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
private static HttpResponse transformResponse(Response response) {
int code = response.code();
String message = response.message();
BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message);
ResponseBody body = response.body();
InputStreamEntity entity = new InputStreamEntity(body.byteStream(), body.contentLength());
httpResponse.setEntity(entity);
Headers headers = response.headers();
for (int i = 0, size = headers.size(); i < size; i++) {
String name = headers.name(i);
String value = headers.value(i);
httpResponse.addHeader(name, value);
if ("Content-Type".equalsIgnoreCase(name)) {
entity.setContentType(value);
} else if ("Content-Encoding".equalsIgnoreCase(name)) {
entity.setContentEncoding(value);
}
}
return httpResponse;
}
示例2: transformResponse
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
private static HttpResponse transformResponse(Response response) throws IOException {
int code = response.code();
String message = response.message();
BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message);
ResponseBody body = response.body();
InputStreamEntity entity = new InputStreamEntity(body.byteStream(), body.contentLength());
httpResponse.setEntity(entity);
Headers headers = response.headers();
for (int i = 0, size = headers.size(); i < size; i++) {
String name = headers.name(i);
String value = headers.value(i);
httpResponse.addHeader(name, value);
if ("Content-Type".equalsIgnoreCase(name)) {
entity.setContentType(value);
} else if ("Content-Encoding".equalsIgnoreCase(name)) {
entity.setContentEncoding(value);
}
}
return httpResponse;
}
示例3: sendPostStream
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
/**
* 向指定URL發送POST方法的數據流請求
*
* @param url 發送請求的URL
* @param message 請求數據
* @param isProxy 是否使用代理
* @return URL所代表遠程資源的響應
*/
public static String sendPostStream(String url, String message, String encode, boolean isProxy) {
SafeHttpClient httpClient = HttpClientFactory.getHttpClient(isProxy);
HttpPost httpReq = new HttpPost(url);
try {
if (StringUtils.isNotBlank(message)) {
// 構造最簡單的字符串數據
byte[] content = message.getBytes();
InputStream inputStream = new ByteArrayInputStream(content);
InputStreamEntity reqEntity = new InputStreamEntity(inputStream, content.length);
reqEntity.setContentType("application/x-www-form-urlencoded");
// 設置請求的數據
httpReq.setEntity(reqEntity);
}
ResponseHandler<String> responseHandler = new SimpleResponseHandler(encode);
return httpClient.execute(httpReq, responseHandler, new BasicHttpContext());
} catch (Exception e) {
LOGGER.error("sendPostStream請求遠程地址失敗,url:{},message:{},encode:{},isProxy:{},Exception:{}", url, message,
encode, isProxy, ExceptionUtil.getException(e));
httpReq.abort();
httpClient.closeExpiredConnections();
}
return null;
}
示例4: doInBackground
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
protected String doInBackground(String... urls) {
String url = "";
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"/audiorecordtest.3gp");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(new URI(url));
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
return response.toString();
} catch (Exception e) {
return e.getMessage().toString();
}
}
示例5: testErrorResponse
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
@Test(expected = AciErrorException.class)
public void testErrorResponse() throws IOException, ProcessorException, AciErrorException {
// Create the "response" and give it a content type...
final InputStreamEntity inputStreamEntity = new InputStreamEntity(getClass().getResourceAsStream("/AciException-1.xml"), -1);
inputStreamEntity.setContentType("text/xml");
// Create a response...
final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
response.setEntity(inputStreamEntity);
// Set the AciResponseInputStream...
final AciResponseInputStream stream = new AciResponseInputStreamImpl(response);
// Process...
processor.process(stream);
fail("Should have thrown a AciErrorException.");
}
示例6: testXmlResponse
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
@Test(expected = AciErrorException.class)
public void testXmlResponse() throws IOException, ProcessorException, AciErrorException {
// Create the "response" and give it a content type...
final InputStreamEntity inputStreamEntity = new InputStreamEntity(getClass().getResourceAsStream("/GetVersion.xml"), -1);
inputStreamEntity.setContentType("text/xml");
// Create a response...
final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
response.setEntity(inputStreamEntity);
// Set the AciResponseInputStream...
final AciResponseInputStream stream = new AciResponseInputStreamImpl(response);
// Process...
processor.process(stream);
fail("Should have thrown a AciErrorException.");
}
示例7: addImage
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
public static int addImage(String baseUrl, String imagePlantId, String filePath) {
File file = new File(filePath);
try {
HttpClient client = getClient();
String url = baseUrl + imagePlantId + "/images";
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true);
httppost.setEntity(reqEntity);
HttpResponse response = client.execute(httppost);
return response.getStatusLine().getStatusCode();
}
catch (Exception e) {
return -1;
}
}
示例8: transformResponse
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
public static HttpResponse transformResponse(Response response) {
int code = response.code();
String message = response.message();
BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message);
ResponseBody body = response.body();
InputStreamEntity entity = new InputStreamEntity(body.byteStream(), body.contentLength());
httpResponse.setEntity(entity);
Headers headers = response.headers();
for (int i = 0; i < headers.size(); i++) {
String name = headers.name(i);
String value = headers.value(i);
httpResponse.addHeader(name, value);
if ("Content-Type".equalsIgnoreCase(name)) {
entity.setContentType(value);
} else if ("Content-Encoding".equalsIgnoreCase(name)) {
entity.setContentEncoding(value);
}
}
return httpResponse;
}
示例9: transformResponse
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
private static HttpResponse transformResponse(Response response) {
int code = response.code();
String message = response.message();
BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message);
ResponseBody body = response.body();
InputStreamEntity entity = new InputStreamEntity(body.byteStream(), body.contentLength());
httpResponse.setEntity(entity);
Headers headers = response.headers();
for (int i = 0; i < headers.size(); i++) {
String name = headers.name(i);
String value = headers.value(i);
httpResponse.addHeader(name, value);
if ("Content-Type".equalsIgnoreCase(name)) {
entity.setContentType(value);
} else if ("Content-Encoding".equalsIgnoreCase(name)) {
entity.setContentEncoding(value);
}
}
return httpResponse;
}
示例10: getHttpMethod
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
/**
* HTTPメソッドを取得する
*
* @param endpointPath エンドポイント
* @param stream リクエストボディのInputStream
* @param length InputStreamの長さ
* @return HttpUriRequest
*/
private HttpUriRequest getHttpMethod(String endpointPath, String contentType,
HttpMethod method, InputStream stream, long length)
throws IOException {
InputStreamEntity entity = new InputStreamEntity(stream, length);
entity.setContentType(contentType);
switch (method) {
case POST:
HttpPost postMethod = new HttpPost(Constants.GRAPH_BASE_URL + endpointPath);
postMethod.setEntity(entity);
return postMethod;
case PUT:
HttpPut putMethod = new HttpPut(Constants.GRAPH_BASE_URL + endpointPath);
putMethod.setEntity(entity);
return putMethod;
default:
Log.e(TAG, "Unsupported http method");
throw new IllegalArgumentException("Unsupported HttpMethod parameter:" + method);
}
}
示例11: storeObject
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
/**
* Store a file on the server, including metadata, with the contents coming from an input stream. This allows you to
* not know the entire length of your content when you start to write it. Nor do you have to hold it entirely in memory
* at the same time.
*
* @param region The name of the storage region
* @param container The name of the container
* @param data Any object that implements InputStream
* @param contentType The MIME type of the file
* @param name The name of the file on the server
* @param metadata A map with the metadata as key names and values as the metadata values
* @return the file ETAG if response code is 201
* @throws GenericException Unexpected response
*/
public String storeObject(Region region, String container, InputStream data, String contentType, String name, Map<String, String> metadata) throws IOException {
HttpPut method = new HttpPut(region.getStorageUrl(container, name));
InputStreamEntity entity = new InputStreamEntity(data, -1);
entity.setChunked(true);
entity.setContentType(contentType);
method.setEntity(entity);
for(Map.Entry<String, String> key : this.renameObjectMetadata(metadata).entrySet()) {
method.setHeader(key.getKey(), key.getValue());
}
Response response = this.execute(method, new DefaultResponseHandler());
if(response.getStatusCode() == HttpStatus.SC_CREATED) {
return response.getResponseHeader(HttpHeaders.ETAG).getValue();
}
else {
throw new GenericException(response);
}
}
示例12: setAudioContent
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
private InputStreamEntity setAudioContent() throws NumberFormatException, Exception
{
File f = new File(audioFile);
if( !f.exists() )
{
System.out.println("Audio file does not exist: " + audioFile);
return null;
}
if( !isStreamed )
{
fileSize = f.length();
}
FileAudioStreamer fs = new FileAudioStreamer(audioFile, isStreamed , encodeToSpeex, Integer.parseInt(sampleRate));
InputStreamEntity reqEntity = new InputStreamEntity(fs.getInputStream(), -1);
fs.start();
reqEntity.setContentType(CODEC);
//reqEntity.setChunked(true);
return reqEntity;
}
示例13: getPut
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
protected HttpPut getPut(String url, File file, Object... paramNameValues) throws IOException
{
final HttpPut put = new HttpPut(appendQueryString(url, queryString(paramNameValues)));
if( file != null )
{
InputStreamEntity inputStreamEntity = new InputStreamEntity(new FileInputStream(file), file.length());
inputStreamEntity.setContentType("application/octet-stream");
put.setEntity(inputStreamEntity);
}
return put;
}
示例14: uploadFile
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
protected void uploadFile(String stagingDirUrl, String filename, URL resource, Object... params) throws IOException
{
String avatarUrl = stagingDirUrl + '/' + com.tle.common.URLUtils.urlEncode(filename);
HttpPut putfile = new HttpPut(appendQueryString(avatarUrl, queryString(params)));
URLConnection file = resource.openConnection();
InputStreamEntity inputStreamEntity = new InputStreamEntity(file.getInputStream(), file.getContentLength());
inputStreamEntity.setContentType("application/octet-stream");
putfile.setEntity(inputStreamEntity);
HttpResponse putfileResponse = execute(putfile, true);
assertResponse(putfileResponse, 200, "200 not returned from file upload");
}
示例15: createRequest
import org.apache.http.entity.InputStreamEntity; //導入方法依賴的package包/類
public HttpUriRequest createRequest(URI uri) {
HttpPost post = new HttpPost(uri);
post.addHeader(X_HTTP_METHOD_OVERRIDE, "PUT");
InputStreamEntity entity =
new InputStreamEntity(mMediaInputStream, -1 /* read until EOF */);
entity.setContentType(mContentType);
post.setEntity(entity);
return post;
}