本文整理匯總了Java中org.springframework.http.HttpHeaders.setContentLength方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpHeaders.setContentLength方法的具體用法?Java HttpHeaders.setContentLength怎麽用?Java HttpHeaders.setContentLength使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.http.HttpHeaders
的用法示例。
在下文中一共展示了HttpHeaders.setContentLength方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: buildAvatarHttpEntity
import org.springframework.http.HttpHeaders; //導入方法依賴的package包/類
public static HttpEntity<Resource> buildAvatarHttpEntity(MultipartFile multipartFile) throws IOException {
// result headers
HttpHeaders headers = new HttpHeaders();
// 'Content-Type' header
String contentType = multipartFile.getContentType();
if (StringUtils.isNotBlank(contentType)) {
headers.setContentType(MediaType.valueOf(contentType));
}
// 'Content-Length' header
long contentLength = multipartFile.getSize();
if (contentLength >= 0) {
headers.setContentLength(contentLength);
}
// File name header
String fileName = multipartFile.getOriginalFilename();
headers.set(XM_HEADER_CONTENT_NAME, fileName);
Resource resource = new InputStreamResource(multipartFile.getInputStream());
return new HttpEntity<>(resource, headers);
}
示例2: file
import org.springframework.http.HttpHeaders; //導入方法依賴的package包/類
/**
* Download attached file.
*/
@GetMapping("/file/{id:[a-f0-9]{64}}/{key:[a-f0-9]{64}}")
public ResponseEntity<StreamingResponseBody> file(@PathVariable("id") final String id,
@PathVariable("key") final String keyHex,
final HttpSession session) {
final KeyIv keyIv =
new KeyIv(BaseEncoding.base16().lowerCase().decode(keyHex), resolveFileIv(id, session));
final DecryptedFile decryptedFile = messageService.resolveStoredFile(id, keyIv);
final HttpHeaders headers = new HttpHeaders();
// Set application/octet-stream instead of the original mime type to force download
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
if (decryptedFile.getName() != null) {
headers.setContentDispositionFormData("attachment", decryptedFile.getName(),
StandardCharsets.UTF_8);
}
headers.setContentLength(decryptedFile.getOriginalFileSize());
final StreamingResponseBody body = out -> {
try (final InputStream in = messageService.getStoredFileInputStream(id, keyIv)) {
ByteStreams.copy(in, out);
out.flush();
}
messageService.burnFile(id);
};
return new ResponseEntity<>(body, headers, HttpStatus.OK);
}
示例3: executeInternal
import org.springframework.http.HttpHeaders; //導入方法依賴的package包/類
@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
byte[] bytes = this.bufferedOutput.toByteArray();
if (headers.getContentLength() == -1) {
headers.setContentLength(bytes.length);
}
ClientHttpResponse result = executeInternal(headers, bytes);
this.bufferedOutput = null;
return result;
}
示例4: executeInternal
import org.springframework.http.HttpHeaders; //導入方法依賴的package包/類
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers) throws IOException {
byte[] bytes = this.bufferedOutput.toByteArray();
if (headers.getContentLength() == -1) {
headers.setContentLength(bytes.length);
}
ListenableFuture<ClientHttpResponse> result = executeInternal(headers, bytes);
this.bufferedOutput = null;
return result;
}
示例5: headObject
import org.springframework.http.HttpHeaders; //導入方法依賴的package包/類
/**
* Retrieves metadata from an object without returning the object itself.
*
* @param bucketName name of the bucket to look in
* @return ResponseEntity containing metadata and status
*/
@RequestMapping(
value = "/{bucketName:.+}/**",
method = RequestMethod.HEAD)
public ResponseEntity<String> headObject(@PathVariable final String bucketName,
final HttpServletRequest request) {
final String filename = filenameFrom(bucketName, request);
final S3Object s3Object = fileStore.getS3Object(bucketName, filename);
if (s3Object != null) {
final HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentLength(Long.valueOf(s3Object.getSize()));
if (!"".equals(s3Object.getContentType())) {
responseHeaders.setContentType(MediaType.parseMediaType(s3Object.getContentType()));
}
responseHeaders.setETag("\"" + s3Object.getMd5() + "\"");
responseHeaders.setLastModified(s3Object.getLastModified());
if (s3Object.isEncrypted()) {
responseHeaders.add(SERVER_SIDE_ENCRYPTION_AWS_KMS_KEYID,
s3Object.getKmsKeyId());
}
return new ResponseEntity<>(responseHeaders, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
示例6: writeInternal
import org.springframework.http.HttpHeaders; //導入方法依賴的package包/類
@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
ByteArrayOutputStream outnew = new ByteArrayOutputStream();
try {
HttpHeaders headers = outputMessage.getHeaders();
//獲取全局配置的filter
SerializeFilter[] globalFilters = fastJsonConfig.getSerializeFilters();
List<SerializeFilter> allFilters = new ArrayList<SerializeFilter>(Arrays.asList(globalFilters));
boolean isJsonp = false;
//不知道為什麽會有這行代碼, 但是為了保持和原來的行為一致,還是保留下來
Object value = strangeCodeForJackson(object);
if (value instanceof FastJsonContainer) {
FastJsonContainer fastJsonContainer = (FastJsonContainer) value;
PropertyPreFilters filters = fastJsonContainer.getFilters();
allFilters.addAll(filters.getFilters());
value = fastJsonContainer.getValue();
}
//revise 2017-10-23 ,
// 保持原有的MappingFastJsonValue對象的contentType不做修改 保持舊版兼容。
// 但是新的JSONPObject將返回標準的contentType:application/javascript ,不對是否有function進行判斷
if (value instanceof MappingFastJsonValue) {
if(!StringUtils.isEmpty(((MappingFastJsonValue) value).getJsonpFunction())){
isJsonp = true;
}
} else if (value instanceof JSONPObject) {
isJsonp = true;
}
int len = JSON.writeJSONString(outnew, //
fastJsonConfig.getCharset(), //
value, //
fastJsonConfig.getSerializeConfig(), //
//fastJsonConfig.getSerializeFilters(), //
allFilters.toArray(new SerializeFilter[allFilters.size()]),
fastJsonConfig.getDateFormat(), //
JSON.DEFAULT_GENERATE_FEATURE, //
fastJsonConfig.getSerializerFeatures());
if (isJsonp) {
headers.setContentType(APPLICATION_JAVASCRIPT);
}
if (fastJsonConfig.isWriteContentLength()) {
headers.setContentLength(len);
}
outnew.writeTo(outputMessage.getBody());
} catch (JSONException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
} finally {
outnew.close();
}
}
示例7: write
import org.springframework.http.HttpHeaders; //導入方法依賴的package包/類
/**
* This implementation delegates to {@link #getDefaultContentType(Object)} if a content
* type was not provided, calls {@link #getContentLength}, and sets the corresponding headers
* on the output message. It then calls {@link #writeInternal}.
*/
@Override
public final void write(final T t, MediaType contentType, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
final HttpHeaders headers = outputMessage.getHeaders();
if (headers.getContentType() == null) {
MediaType contentTypeToUse = contentType;
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
contentTypeToUse = getDefaultContentType(t);
}
if (contentTypeToUse != null) {
headers.setContentType(contentTypeToUse);
}
}
if (headers.getContentLength() == -1) {
Long contentLength = getContentLength(t, headers.getContentType());
if (contentLength != null) {
headers.setContentLength(contentLength);
}
}
if (outputMessage instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage =
(StreamingHttpOutputMessage) outputMessage;
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
@Override
public void writeTo(final OutputStream outputStream) throws IOException {
writeInternal(t, new HttpOutputMessage() {
@Override
public OutputStream getBody() throws IOException {
return outputStream;
}
@Override
public HttpHeaders getHeaders() {
return headers;
}
});
}
});
}
else {
writeInternal(t, outputMessage);
outputMessage.getBody().flush();
}
}