1

是否有任何骆驼restful Web服务示例提供文件下载,如下api

@GET
@Path("/jar")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFile() {
    File file = new File("/home/user/Downloads/classes.jar");
    ResponseBuilder response = Response.ok((Object) file);
    response.header("Content-Disposition", "attachment;filename=classes.jar");
    return response.build();
}
4

1 回答 1

1

You can use a combination of Camel REST DSL and Content Enricher (specifically - pollEnrich).

An example implementation for your case could look like this:

// you can configure it as you want, it's just an example
restConfiguration().component("restlet").host("localhost").port(8081);

rest("/jar")
    .get()
    .produces(MediaType.APPLICATION_OCTET_STREAM_VALUE)
    .route()
    .routeId("downloadFile")
    .pollEnrich("file:/home/user/Downloads?fileName=classes.jar&noop=true")
    .setHeader("Content-Disposition", simple("attachment;filename=classes.jar"));

Please note, that if the file is not found in the specified path, pollEnrich will block until the file arrives. You can set a timeout in milliseconds by providing a second argument of type long to the call to pollEnrich.

于 2018-09-02T06:55:21.427 回答