本文整理汇总了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();
}
}