gpt4 book ai didi

spring - 使用 RestTemplate.postForLocation 的文件上传进度条

转载 作者:行者123 更新时间:2023-12-03 17:54:16 25 4
gpt4 key购买 nike

我有一个将文件上传到 REST 服务的 Java 桌面客户端应用程序。

对 REST 服务的所有调用都使用 Spring RestTemplate 类处理。

我希望实现一个进度条并取消功能,因为正在上传的文件可能非常大。

我一直在寻找一种在网络上实现此功能的方法,但没有运气。

我尝试实现自己的 ResourceHttpMessageConverter 并替换 writeInternal() 方法,但是在实际发布请求之前,似乎在某种缓冲操作期间调用了此方法(因此在发送之前一次性读取流)。

我什至尝试过覆盖 CommonsClientHttpRequestFactory.createRequest() 方法并使用特殊的 writeRequest() 方法实现我自己的 RequestEntity 类,但是会出现同样的问题(在实际发送帖子之前已读取所有流)。

我找错地方了吗?有没有人做过类似的事情。

我在网上读到的很多关于实现进度条的内容都谈到了停止上传,然后使用单独的 AJAX 请求轮询 Web 服务器的进度,这似乎是一种奇怪的方式。

非常感谢任何帮助或提示。

最佳答案

这是一个古老的问题,但它仍然是相关的。

I tried implementing my own ResourceHttpMessageConverter and substituting the writeInternal() method but this method seems to be called during some sort of buffered operation prior to actually posting the request (so the stream is read all in one go before sending takes place).


你走在正确的轨道上。此外,您还需要在 RestTemplate 的 HttpRequestFactory 上禁用请求正文缓冲,如下所示:
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
clientHttpRequestFactory.setBufferRequestBody(false);
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
这是 working example for tracking file upload progress with RestTemplate.

关于spring - 使用 RestTemplate.postForLocation 的文件上传进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13883161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com