有没有骆驼的宁静的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();
}
答案 0 :(得分:0)
您可以结合使用Camel REST DSL和Content Enricher(特别是pub trait Event {
fn event(&self);
}
pub struct Property {
message: String,
}
impl Property {
pub fn new(message: String) -> Property {
Property { message }
}
}
impl Event for Property {
fn event(&self) {
println!("{}", &self.message);
}
}
pub struct Utility {
message: String,
}
impl Utility {
pub fn new(message: String) -> Utility {
Utility { message }
}
}
impl Event for Utility {
fn event(&self) {
println!("{}", &self.message);
}
}
pub struct Game {
pub spaces: Vec<Box<Event>>,
}
fn main () {
let game = Game{
spaces: vec![
Box::new(Utility::new(String::from("Water works"))),
Box::new(Property::new(String::from("Fleet Street"))),
Box::new(Utility::new(String::from("Electric company"))),
Box::new(Property::new(String::from("Bond Street"))),
]
};
for space in game.spaces {
space.event();
}
}
// Water works
// Fleet Street
// Electric company
// Bond Street
)。
您的案例的示例实现如下所示:
pollEnrich
请注意,如果在指定的路径中找不到文件,则// 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"));
将一直阻塞,直到文件到达为止。您可以通过为调用pollEnrich
提供第二个类型为long
的参数来设置超时(以毫秒为单位)。