java - Content-Length 分隔的消息正文过早结束 - Resttemplate
问题描述
我正在使用 Spring RestTemplate 从第三方 URL 下载一个大文件。我使用以下方法下载文件,以免将整个文件加载到内存中。
// Optional Accept header
RequestCallback requestCallback = request -> request.getHeaders()
.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
// Streams the response instead of loading it all in memory
ResponseExtractor<Void> responseExtractor = response -> {
// Here I write the response to a file but do what you like
Path path = Paths.get("some/path");
Files.copy(response.getBody(), path);
return null;
};
restTemplate.execute(URI.create("www.something.com"), HttpMethod.GET, requestCallback, responseExtractor
这很好用,但是我们间歇性地得到一个Premature end of Content-Length delimited message body
我从另一个堆栈溢出帖子中读到这是一个服务器端问题。如何确定这是服务器端问题?我如何知道这是第三方方面的内存问题还是响应标头中的内容长度不正确等?在我向第三方寻求解决方案之前,我需要确认一下。
解决方案
根据触发错误的难易程度,您可以使用诸如curl
调用 URL 之类的工具(仅用于测试,在您的应用程序之外)。如果还是看到失败,那肯定不是客户端的问题,因为那些知名的第三方工具都经过了很好的测试,没有这样的bug。
推荐阅读
- reactjs - 如何使用 react-final-form 多屏幕正确验证表单值?
- google-api - 未捕获的类型错误:无法读取 null 的属性“signIn”(在 localhost 上运行代码时!)
- google-apps-script - 将焦点放在工作表的一部分上
- javascript - Google 登录 API 失败:网络错误
- php - 用户登录时,在 Laravel 8.x 中调用 null 上的成员函数 prepare()
- javascript - 如何在异步函数中的数组内插入对象?
- pdf - SwiftUI PDF Kit 未填充可用视图空间
- database - 如何检查是否已经在 JanusGraph 中创建了模式?
- javascript - 如何在 React js 中以 Next-Gen 格式提供图像
- python - 列表理解中的字典