Spring RestTemplate
似乎无法将响应直接流式传输到文件,而不将其全部缓存在内存中。使用较新的Spring 5 WebClient
来实现此目标的合适方法是什么?
WebClient client = WebClient.create("https://example.com");
client.get().uri(".../{name}", name).accept(MediaType.APPLICATION_OCTET_STREAM)
....?
我看到人们发现RestTemplate
可以解决此问题,但是我对使用WebClient
采取正确的方式更感兴趣。
有许多使用RestTemplate
下载二进制数据的示例,但是几乎所有示例都将byte[]
加载到内存中。
答案 0 :(得分:7)
使用最近稳定的Spring WebFlux(截至撰写时为5.2.4.RELEASE):
final WebClient client = WebClient.create("https://example.com");
final Flux<DataBuffer> dataBufferFlux = client.get()
.accept(MediaType.TEXT_HTML)
.retrieve()
.bodyToFlux(DataBuffer.class); // the magic happens here
final Path path = FileSystems.getDefault().getPath("target/example.html");
DataBufferUtils
.write(dataBufferFlux, path, CREATE_NEW)
.block(); // only block here if the rest of your code is synchronous
对我来说,不明显的部分是bodyToFlux(DataBuffer.class)
,正如Spring文档的generic section about streaming中当前提到的那样,在WebClient部分中没有直接引用它。
答案 1 :(得分:1)
我无法测试以下代码是否有效地不在内存中缓冲webClient
有效负载的内容。不过,我认为您应该从那里开始:
public Mono<Void> testWebClientStreaming() throws IOException {
Flux<DataBuffer> stream =
webClient
.get().accept(MediaType.APPLICATION_OCTET_STREAM)
.retrieve()
.bodyToFlux(DataBuffer.class);
Path filePath = Paths.get("filename");
AsynchronousFileChannel asynchronousFileChannel = AsynchronousFileChannel.open(filePath, WRITE);
return DataBufferUtils.write(stream, asynchronousFileChannel)
.doOnNext(DataBufferUtils.releaseConsumer())
.doAfterTerminate(() -> {
try {
asynchronousFileChannel.close();
} catch (IOException ignored) { }
}).then();
}
答案 2 :(得分:1)
static <R> Mono<R> writeBodyToTempFileAndApply(
final WebClient.ResponseSpec spec,
final Function<? super Path, ? extends R> function) {
return using(
() -> createTempFile(null, null),
t -> write(spec.bodyToFlux(DataBuffer.class), t)
.thenReturn(function.apply(t)),
t -> {
try {
deleteIfExists(t);
} catch (final IOException ioe) {
throw new RuntimeException(ioe);
}
}
);
}
static <R> Mono<R> pipeBodyAndApply(
final WebClient.ResponseSpec spec, final ExecutorService executor,
final Function<? super ReadableByteChannel, ? extends R> function) {
return using(
Pipe::open,
p -> {
final Future<Disposable> future = executor.submit(
() -> write(spec.bodyToFlux(DataBuffer.class), p.sink())
.log()
.doFinally(s -> {
try {
p.sink().close();
log.debug("p.sink closed");
} catch (final IOException ioe) {
throw new RuntimeException(ioe);
}
})
.subscribe(DataBufferUtils.releaseConsumer())
);
return just(function.apply(p.source()))
.log()
.doFinally(s -> {
try {
final Disposable disposable = future.get();
assert disposable.isDisposed();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
});
},
p -> {
try {
p.source().close();
log.debug("p.source closed");
} catch (final IOException ioe) {
throw new RuntimeException(ioe);
}
}
);
}
答案 3 :(得分:0)
我不确定您当前使用的spring是否可以访问RestTemplate
,但这对我有用。
RestTemplate restTemplate // = ...;
RequestCallback requestCallback = request -> request.getHeaders()
.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
// Streams the response
ResponseExtractor<Void> responseExtractor = response -> {
// Here I write the response to a file but do what you like
Path path = Paths.get("http://some/path");
Files.copy(response.getBody(), path);
return null;
};
restTemplate.execute(URI.create("www.something.com"), HttpMethod.GET, requestCallback, responseExtractor);
答案 4 :(得分:-1)
这对我有效,没有使用WebClient,而是使用HttpURLConnection。就我而言,我有巨大的xml文件,需要从另一个API提取并返回以返回它们。
@GetMapping(value = "/hugeXml/{fileName}", produces = MediaType.APPLICATION_XML_VALUE)
public ResponseEntity<String> getHugeXml(@PathVariable String fileName) {
try {
URL myURL = new URL("http://some-other-url.com/xml_files/" + fileName + ".xml");
HttpURLConnection myURLConnection = (HttpURLConnection) myURL.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
String result = reader.lines().collect(Collectors.joining());
return ResponseEntity.ok(result);
} catch (IOException e) {
String msg = "IO exception occured! Message = " + e.getMessage();
System.out.println(msg);
e.printStackTrace();
return ResponseEntity.badRequest().body(msg);
}
}