万户网络公司怎么样,杭州企业seo,广告公司是干什么的,怎样把网站做的高大上服务器禁止head 请求HEAD是一个经常被遗忘的HTTP方法#xff08;动词#xff09;#xff0c;其行为类似于GET#xff0c;但不返回正文。 您使用HEAD来检查资源的存在#xff08;如果不存在#xff0c;它应该返回404#xff09;#xff0c;并确保您的缓存中没有陈旧的版… 服务器禁止head 请求  HEAD是一个经常被遗忘的HTTP方法动词其行为类似于GET但不返回正文。 您使用HEAD来检查资源的存在如果不存在它应该返回404并确保您的缓存中没有陈旧的版本。 在这种情况下您期望304 Not Modified 而200则意味着服务器具有最新版本。 例如您可以使用HEAD来高效地实施软件更新。 在这种情况下 ETag是您的应用程序版本生成标记提交哈希并且您具有固定的/most_recent端点。 您的软件会在ETag发送当前版本的HEAD请求。 如果没有更新则服务器将以304答复。如果为200则可以询问用户是否要升级而不下载软件。 最终请求GET /most_recent将始终下载您软件的最新版本。 HTTP的力量   在servlet中默认情况下 HEAD是在您应该重写的doHead()实现的。 默认实现只是委派给GET但丢弃主体。 这效率不高尤其是当您从外部例如Amazon S3加载资源时。 幸运的是Spring MVC默认情况下不实现HEAD因此您必须手动执行。 让我们从HEAD的一些集成测试开始  def should return 200 OK on HEAD request, but without body() {expect:mockMvc.perform(head(/download/  FileExamples.TXT_FILE_UUID)).andExpect(status().isOk()).andExpect(content().bytes(new byte[0]))
}def should return 304 on HEAD request if we have cached version() {expect:mockMvc.perform(head(/download/  FileExamples.TXT_FILE_UUID).header(IF_NONE_MATCH, FileExamples.TXT_FILE.getEtag())).andExpect(status().isNotModified()).andExpect(header().string(ETAG, FileExamples.TXT_FILE.getEtag()))
}def should return Content-length header() {expect:mockMvc.perform(head(/download/  FileExamples.TXT_FILE_UUID)).andExpect(status().isOk()).andExpect(header().longValue(CONTENT_LENGTH, FileExamples.TXT_FILE.size))
} 实际的实现非常简单但是为了避免重复需要一些重构。 下载端点现在接受GET和HEAD  RequestMapping(method  {GET, HEAD}, value  /{uuid})
public ResponseEntityResource download(HttpMethod method,PathVariable UUID uuid,RequestHeader(IF_NONE_MATCH) OptionalString requestEtagOpt,RequestHeader(IF_MODIFIED_SINCE) OptionalDate ifModifiedSinceOpt) {return storage.findFile(uuid).map(pointer - new ExistingFile(method, pointer)).map(file - file.handle(requestEtagOpt, ifModifiedSinceOpt)).orElseGet(() - new ResponseEntity(NOT_FOUND));
} 我创建了一个新的抽象ExistingFile 它封装了我们在其上调用的找到的FilePointer和HTTP动词。 ExistingFile.handle()具有通过HEAD提供文件或仅提供元数据所需的一切  public class ExistingFile {private static final Logger log  LoggerFactory.getLogger(ExistingFile.class);private final HttpMethod method;private final FilePointer filePointer;public ExistingFile(HttpMethod method, FilePointer filePointer) {this.method  method;this.filePointer  filePointer;}public ResponseEntityResource handle(OptionalString requestEtagOpt, OptionalDate ifModifiedSinceOpt) {if (requestEtagOpt.isPresent()) {final String requestEtag  requestEtagOpt.get();if (filePointer.matchesEtag(requestEtag)) {return notModified(filePointer);}}if (ifModifiedSinceOpt.isPresent()) {final Instant isModifiedSince  ifModifiedSinceOpt.get().toInstant();if (filePointer.modifiedAfter(isModifiedSince)) {return notModified(filePointer);}}return serveDownload(filePointer);}private ResponseEntityResource serveDownload(FilePointer filePointer) {log.debug(Serving {} {}, method, filePointer);final InputStreamResource resource  resourceToReturn(filePointer);return response(filePointer, OK, resource);}private InputStreamResource resourceToReturn(FilePointer filePointer) {if (method  HttpMethod.GET)return buildResource(filePointer);elsereturn null;}private InputStreamResource buildResource(FilePointer filePointer) {final InputStream inputStream  filePointer.open();return new InputStreamResource(inputStream);}private ResponseEntityResource notModified(FilePointer filePointer) {log.trace(Cached on client side {}, returning 304, filePointer);return response(filePointer, NOT_MODIFIED, null);}private ResponseEntityResource response(FilePointer filePointer, HttpStatus status, Resource body) {return ResponseEntity.status(status).eTag(filePointer.getEtag()).lastModified(filePointer.getLastModified().toEpochMilli()).body(body);}} resourceToReturn()至关重要。 如果返回null Spring MVC将不包含任何主体作为响应。 其他所有内容均保持不变响应标头等  编写下载服务器  第一部分始终流式传输永远不要完全保留在内存中  第二部分标头Last-ModifiedETag和If-None-Match  第三部分标头内容长度和范围  第四部分有效地执行HEAD操作  第五部分油门下载速度  第六部分描述您发送的内容内容类型等   这些文章中开发的示例应用程序可在GitHub上找到。  翻译自: https://www.javacodegeeks.com/2015/07/writing-a-download-server-part-iv-implement-head-operation-efficiently.html服务器禁止head 请求