生成部分内容 (206) 作为使用 rest Web 服务对 zip 文件进行分块下载的输出时出现问题



>我已经编写了一个 rest Web 服务,使用 spring boot(2.0.5.RELEASE( 使用 mime 类型application_octet_stream块生成一个 zip 文件,但是当我使用 curl 命令指定范围时,我无法将partial_content(206)作为输出,如下所示,但我得到 200(OK( 作为响应,即我无法根据块检索文件。

curl http://localhost:8080/resource/getlist -i -H  "Range: bytes=0-100"

PS:弹簧启动应用程序部署在外部码头服务器上,并且不使用嵌入式tomcat和码头服务器,其中排除是在pom中完成的.xml.这是由于各种设计原因造成的。

是否需要在外部码头服务器上进行任何配置添加/修改?

代码如下

@RestController
@RequestMapping("/resource")
public class ListResource {
/* Logger **/
private static final Logger _LOG = LoggerFactory.getLogger(ListResource.class);
@Autowired
private AppContext appContext;
private static final String DATE_FORMAT_FOR_ETAG = "yyyy-MM-dd HH:mm:ss'Z'";
private static final DateFormat eTagDateFormat = new SimpleDateFormat(DATE_FORMAT_FOR_ETAG, Locale.US);
private static final String ACCEPT_RANGES_BYTES = "bytes";
@RequestMapping(path = "/getlist", method = RequestMethod.HEAD)
public ResponseEntity<?> fetchListHead() throws IOException {
return fetchList(true);
}
@RequestMapping(path = "/getlist", method = RequestMethod.GET,produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<?> fetchListGet() throws IOException {
return fetchList(false);
}
private ResponseEntity<?> fetchList(final boolean justHead) throws IOException {
File file = null;
try {
file = new File("/home/resource/hello.zip");
byte[] readBytes = Files.readAllBytes(file.toPath());
ByteArrayResource resource = new ByteArrayResource(readBytes);
ResponseEntity.BodyBuilder responseBuilder = ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="" + file.getName() + """)
.header(HttpHeaders.ACCEPT_RANGES, ACCEPT_RANGES_BYTES)
.lastModified(new Date().getTime())
.eTag(getETag(file))
.cacheControl(CacheControl.maxAge(3600, TimeUnit.SECONDS).cachePublic().mustRevalidate())
.contentLength(file.length())
.contentType(MediaType.parseMediaType(MediaType.APPLICATION_OCTET_STREAM_VALUE));
return justHead ? responseBuilder.build() : responseBuilder.body(resource);
} catch (Exception e) {
_LOG.error("Error in gettingResource:{}", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
private String getETag(File file) {
String tag = "";
if (file != null) {
tag = eTagDateFormat.format(new Date(file.getAbsoluteFile().lastModified()));
}
return tag;
}
}

处理对动态内容(您的 REST 服务(的范围请求不是 Jetty 服务器的角色。

Jetty 提供的唯一远程请求支持是......

  • 具有重叠范围的传入请求范围在调度到上下文之前合并为合理的范围。
  • 无效的请求范围(例如负数(会导致 BadMessageException 和对用户代理的 400 响应。
  • 通过DefaultServlet提供的静态内容支持范围请求。

由动态终结点(其余 API(来读取请求的范围并确定是否支持提供这些特定范围,然后提供它、拒绝范围或提供整个内容。

如果您认为这是 Jetty 应该支持的内容,请随时在 https://github.com/eclipse/jetty.project/issues 提交增强请求

最新更新