简体   繁体   English

通过JAX-RS使用RESTful Web服务的文件下载列表

[英]Download list of File Using RESTful Web Services with JAX-RS

taken from, http://www.concretepage.com/webservices/download-file-using-restful-web-services-jax-rs , here is the code to download a File from a jax-rs rest service 摘自http://www.concretepage.com/webservices/download-file-using-restful-web-services-jax-rs ,这是从jax-rs rest服务下载文件的代码

@Path("/restwb") 
public class FileResource {
    @GET
    @Path("/download/{fname}/{ext}")
        @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response  downloadFile(@PathParam("fname") String fileName,@PathParam("ext") String fileExt){
        File file = new File("C:/temp/"+fileName+"."+fileExt);
        ResponseBuilder rb = Response.ok(file);
        rb.header("Content-Disposition", "attachment; filename=" + file.getName());
        Response response = rb.build();
        return response;
    }
} 

My question now is what should the response look like in order to download a list of File objects (ArrayList)? 现在我的问题是,下载文件对象列表 (ArrayList)时响应应该是什么样?

Can we just write: 我们可以只写:

List<File> lFiles = new ArrayList<File>();
...
ResponseBuilder rb = Response.ok(lFiles);

You can not download multiple files in the same request. 您不能在同一请求中下载多个文件。 If you need to download multiple files you have to create an archive then download the archived file. 如果需要下载多个文件,则必须创建一个存档,然后下载存档的文件。

This is because raw bytes of the file are sent in the HTTP Response, and HTTP headers are used for how to translate the content you can only set one set of headers HTTP response. 这是因为文件的原始字节是在HTTP响应中发送的,而HTTP标头用于翻译内容,因此您只能设置一组标头HTTP响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM