使用 Jboss DMR 下载/保存附件
Posted
技术标签:
【中文标题】使用 Jboss DMR 下载/保存附件【英文标题】:Download/Save attachment using Jboss DMR 【发布时间】:2021-04-27 09:08:39 【问题描述】:我有一个 Jboss CLI 命令,我正在尝试使用 DMR 库在 Java 中实现它。该步骤涉及下载附件(显示/保存),但是任何地方都没有可用的文档。任何帮助表示赞赏。
我正在尝试转换为 DMR 的 CLI 命令:
attachment display --operation=/deployment=app.war:read-content(path=META-INF/MANIFEST.MF)
下面是我的操作对象,但是没有办法提供显示/保存附件的命令
ModelNode request = new ModelNode();
request.get(ClientConstants.OP_ADDR).add(ClientConstants.DEPLOYMENT, "app.war");
request.get(ClientConstants.OP).set(ClientConstants.READ_CONTENT_OPERATION);
request.get(ClientConstants.PATH).set("META-INF/MANIFEST.MF");
这个 DMR 操作产生的只是
"outcome" => "success",
"result" => "uuid" => "1f45b2bf-8402-4b46-a721-3e3f23db5d80",
"response-headers" => "attached-streams" => [
"uuid" => "1f45b2bf-8402-4b46-a721-3e3f23db5d80",
"mime-type" => "text/plain"
]
有没有办法编辑操作或使用操作中返回的 uuid 从流中下载附件?
【问题讨论】:
【参考方案1】:我也没有看到任何明确的文档。但是,您需要使用OperationResponse
来获取流。这是一个例子。
public class ReadContent
public static void main(final String[] args) throws Exception
try (ModelControllerClient client = ModelControllerClient.Factory.create("localhost", 9990))
try (
StreamEntry entry = readContent(client, "kitchensink.war", "META-INF/MANIFEST.MF");
InputStream in = entry.getStream();
)
int len;
final byte[] buffer = new byte[256];
while ((len = in.read(buffer)) != -1)
System.out.write(buffer, 0, len);
private static StreamEntry readContent(final ModelControllerClient client, final String deploymentName, final String path) throws IOException
final ModelNode address = Operations.createAddress("deployment", deploymentName);
final ModelNode op = Operations.createOperation(ClientConstants.READ_CONTENT_OPERATION, address);
op.get(ClientConstants.PATH).set(path);
// Execute the operation
final OperationResponse response = client.executeOperation(Operation.Factory.create(op), OperationMessageHandler.logging);
// Get the response node to evaluate the outcome
final ModelNode outcome = response.getResponseNode();
if (Operations.isSuccessfulOutcome(outcome))
// Read the result and get the UUID the stream is attached to
final String uuid = Operations.readResult(outcome).get(ClientConstants.UUID).asString();
return response.getInputStream(uuid);
// The operation has failed, raise and exception with the failure description
throw new RuntimeException("Failed to read the content: " + Operations.getFailureDescription(outcome).asString());
【讨论】:
谢谢你,詹姆斯,这很好用。我已经到了获取 UUID 和 StreamEntry 的地步,但无法将这两者联系在一起。使用 OperationResponse 然后 response.getInputStream(uuid) 是神奇的。非常感谢。以上是关于使用 Jboss DMR 下载/保存附件的主要内容,如果未能解决你的问题,请参考以下文章
如何下载和保存PDF文件,该文件在PhantomJS的响应标题中作为附件接收?