本文整理匯總了Java中org.apache.http.entity.ContentType.TEXT_PLAIN屬性的典型用法代碼示例。如果您正苦於以下問題:Java ContentType.TEXT_PLAIN屬性的具體用法?Java ContentType.TEXT_PLAIN怎麽用?Java ContentType.TEXT_PLAIN使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類org.apache.http.entity.ContentType
的用法示例。
在下文中一共展示了ContentType.TEXT_PLAIN屬性的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: constructContentBody
ContentBody constructContentBody() throws UnsupportedEncodingException {
ContentType contentTypeObject = constructContentTypeObject();
if (filePath != null) { // FILE part
if (contentTypeObject != null) {
return new FileBody(new File(filePath), contentTypeObject);
} else {
return new FileBody(new File(filePath));
}
} else if (content != null) { // TEXT part
if (contentTypeObject != null) {
return new StringBody(content, contentTypeObject);
} else {
return new StringBody(content, ContentType.TEXT_PLAIN);
}
} else { // BYTE ARRAY part
if (contentTypeObject != null) {
return new ByteArrayBody(this.fileBytes, contentTypeObject, fileName);
} else {
return new ByteArrayBody(this.fileBytes, fileName);
}
}
}
示例2: attachFileToTransaction
/**
* Retorna a lista de anexos de uma transação com ou sem filtro de tipo
* @param idTransacao: Código da transação à ser consultada
* @param arquivo: Arquivo à ser anexado (imagem [JPEG/PNG] ou documento [PDF])
* @param tipoAnexo: Tipo de anexo à ser enviado
* @return boolean
*/
public boolean attachFileToTransaction(String idTransacao, File arquivo, TipoAnexo tipoAnexo) throws IOException,
PJBankException {
Set<String> extensoesPermitidas = new HashSet<>();
extensoesPermitidas.add("pdf");
extensoesPermitidas.add("jpg");
extensoesPermitidas.add("jpeg");
extensoesPermitidas.add("png");
if (!extensoesPermitidas.contains(FilenameUtils.getExtension(arquivo.getName()))) {
throw new IllegalArgumentException("O arquivo a ser anexado em uma transação deve estar no formato PDF, JPG," +
" JPEG ou PNG, sendo assim um documento ou uma imagem.");
}
PJBankClient client = new PJBankClient(this.endPoint.concat("/transacoes/").concat(idTransacao).concat("/documentos"));
HttpPost httpPost = client.getHttpPostClient();
httpPost.addHeader("x-chave-conta", this.chave);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//FileBody fileBody = new FileBody(arquivo);
StringBody stringBody = new StringBody(tipoAnexo.getName(), ContentType.TEXT_PLAIN);
//builder.addPart("arquivos", fileBody);
builder.addBinaryBody("arquivos", arquivo,
ContentType.APPLICATION_OCTET_STREAM, arquivo.getName());
builder.addPart("tipo", stringBody);
httpPost.setEntity(builder.build());
return client.doRequest(httpPost).getStatusLine().getStatusCode() == 201;
}
示例3: testWrapExceptionToPreserveStatus
public void testWrapExceptionToPreserveStatus() throws IOException {
Exception cause = new Exception();
// Successfully get the status without a body
RestStatus status = randomFrom(RestStatus.values());
ElasticsearchStatusException wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), null, cause);
assertEquals(status, wrapped.status());
assertEquals(cause, wrapped.getCause());
assertEquals("No error body.", wrapped.getMessage());
// Successfully get the status without a body
HttpEntity okEntity = new StringEntity("test body", ContentType.TEXT_PLAIN);
wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), okEntity, cause);
assertEquals(status, wrapped.status());
assertEquals(cause, wrapped.getCause());
assertEquals("body=test body", wrapped.getMessage());
// Successfully get the status with a broken body
IOException badEntityException = new IOException();
HttpEntity badEntity = mock(HttpEntity.class);
when(badEntity.getContent()).thenThrow(badEntityException);
wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), badEntity, cause);
assertEquals(status, wrapped.status());
assertEquals(cause, wrapped.getCause());
assertEquals("Failed to extract body.", wrapped.getMessage());
assertEquals(badEntityException, wrapped.getSuppressed()[0]);
// Fail to get the status without a body
int notAnHttpStatus = -1;
assertNull(RestStatus.fromCode(notAnHttpStatus));
wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, null, cause);
assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
assertEquals(cause, wrapped.getCause());
assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. No error body.", wrapped.getMessage());
// Fail to get the status without a body
wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, okEntity, cause);
assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
assertEquals(cause, wrapped.getCause());
assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. body=test body", wrapped.getMessage());
// Fail to get the status with a broken body
wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, badEntity, cause);
assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
assertEquals(cause, wrapped.getCause());
assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. Failed to extract body.", wrapped.getMessage());
assertEquals(badEntityException, wrapped.getSuppressed()[0]);
}