使用 Micronaut 下载文件
Download File with Micronaut
我的客户应该从控制器收到一个文件。问题是客户端只收到一个字符串。如何从控制器获取返回的流?
这是我的控制器:
@Get("/{databaseName}")
MutableHttpResponse < Object > createDatabaseBackup(String databaseName) {
InputStream inputStreamDatabaseBackup = backupTask.exportBackup(databaseName);
return HttpResponse.ok(new StreamedFile(inputStreamDatabaseBackup, MediaType.APPLICATION_OCTET_STREAM_TYPE));
}
这是我的客户:
@Inject
@Client("${agent.connection-url}")
private RxHttpClient client;
public String getBackup(String dataBaseName) {
return client.toBlocking().retrieve(HttpRequest.GET("/backup/" + dataBaseName));
}
您必须将您需要的响应定义为 byte[]
数组:
public byte[] getBackup(String databaseName) {
return client.toBlocking().retrieve(HttpRequest.GET("/backup/" + databaseName), byte[].class);
}
然后你可以将byte
数组转换成你想要的任何流。
或者你可以使用响应方式:
public Single<byte[]> getBackup(String databaseName) {
return client.retrieve(HttpRequest.GET("/backup/" + databaseName), byte[].class)
.collect(ByteArrayOutputStream::new, ByteArrayOutputStream::writeBytes)
.map(ByteArrayOutputStream::toByteArray);
}
或者您可以定义声明式客户端:
@Client("${agent.connection-url}")
public interface SomeControllerClient {
@Get(value = "/{databaseName}", processes = MediaType.APPLICATION_OCTET_STREAM)
Flowable<byte[]> getBackup(String databaseName);
}
然后在需要的地方使用它:
@Inject
SomeControllerClient client;
public void someMethod() {
client.getBackup("some-database")
.collect(ByteArrayOutputStream::new, ByteArrayOutputStream::writeBytes)
.map(ByteArrayOutputStream::toByteArray)
.otherStreamProcessing...
}
我的客户应该从控制器收到一个文件。问题是客户端只收到一个字符串。如何从控制器获取返回的流?
这是我的控制器:
@Get("/{databaseName}")
MutableHttpResponse < Object > createDatabaseBackup(String databaseName) {
InputStream inputStreamDatabaseBackup = backupTask.exportBackup(databaseName);
return HttpResponse.ok(new StreamedFile(inputStreamDatabaseBackup, MediaType.APPLICATION_OCTET_STREAM_TYPE));
}
这是我的客户:
@Inject
@Client("${agent.connection-url}")
private RxHttpClient client;
public String getBackup(String dataBaseName) {
return client.toBlocking().retrieve(HttpRequest.GET("/backup/" + dataBaseName));
}
您必须将您需要的响应定义为 byte[]
数组:
public byte[] getBackup(String databaseName) {
return client.toBlocking().retrieve(HttpRequest.GET("/backup/" + databaseName), byte[].class);
}
然后你可以将byte
数组转换成你想要的任何流。
或者你可以使用响应方式:
public Single<byte[]> getBackup(String databaseName) {
return client.retrieve(HttpRequest.GET("/backup/" + databaseName), byte[].class)
.collect(ByteArrayOutputStream::new, ByteArrayOutputStream::writeBytes)
.map(ByteArrayOutputStream::toByteArray);
}
或者您可以定义声明式客户端:
@Client("${agent.connection-url}")
public interface SomeControllerClient {
@Get(value = "/{databaseName}", processes = MediaType.APPLICATION_OCTET_STREAM)
Flowable<byte[]> getBackup(String databaseName);
}
然后在需要的地方使用它:
@Inject
SomeControllerClient client;
public void someMethod() {
client.getBackup("some-database")
.collect(ByteArrayOutputStream::new, ByteArrayOutputStream::writeBytes)
.map(ByteArrayOutputStream::toByteArray)
.otherStreamProcessing...
}