【问题标题】:Download list of File Using RESTful Web Services with JAX-RS使用带有 JAX-RS 的 RESTful Web 服务的文件下载列表
【发布时间】:2016-11-18 05:05:01
【问题描述】:

取自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;
    }
} 

我现在的问题是,为了下载文件对象 (ArrayList) 的列表,响应应该是什么样子?

我们可以写吗:

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

【问题讨论】:

  • ---- 为了下载文件对象列表 (ArrayList),响应应该是什么样子? ---> 为什么不测试一下,看看结果如何?
  • 现在写代码试试看。我只是想在此之前有一些意见,看看它是否正确

标签: java file jax-rs


【解决方案1】:

您不能在同一个请求中下载多个文件。如果您需要下载多个文件,您必须创建一个存档,然后下载存档文件。

这是因为文件的原始字节是在 HTTP Response 中发送的,而 HTTP headers 用于如何翻译内容,您只能设置一组 headers HTTP response。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 2013-06-26
    • 2012-04-23
    • 2015-09-30
    • 1970-01-01
    相关资源
    最近更新 更多