【问题标题】:Is it possible to control the filename for a Response from a Jersey Rest service?是否可以控制来自 Jersey Rest 服务的响应的文件名?
【发布时间】:2011-03-15 21:12:05
【问题描述】:

目前我在 Jersey 有一个方法,可以从内容存储库中检索文件并将其作为响应返回。该文件可以是 jpeg、gif、pdf、docx、html 等(基本上任何东西)。但是,目前,我无法弄清楚如何控制文件名,因为每个文件都会使用名称自动下载(下载。[文件扩展名],即(download.jpg,download.docx,download.pdf)。有没有办法让我可以设置文件名吗?我已经有它在一个字符串中,但我不知道如何设置响应,以便它显示该文件名而不是默认为“下载”。

@GET
@Path("/download/{id}")
public Response downloadContent(@PathParam("id") String id)
{
    String serverUrl = "http://localhost:8080/alfresco/service/cmis";
    String username = "admin";
    String password = "admin";

    Session session = getSession(serverUrl, username, password);

    Document doc = (Document)session.getObject(session.createObjectId(id));

    String filename = doc.getName();

    ResponseBuilder rb = new ResponseBuilderImpl();

    rb.type(doc.getContentStreamMimeType());
    rb.entity(doc.getContentStream().getStream());

    return rb.build();
}

【问题讨论】:

    标签: jersey jax-rs


    【解决方案1】:

    使用 Jersey 提供的 ContentDisposition 类的更好的方法,它更安全:

    ContentDisposition contentDisposition = ContentDisposition.type("attachment")
        .fileName("filename.csv").creationDate(new Date()).build();
    
     return Response.ok(
                new StreamingOutput() {
                    @Override
                    public void write(OutputStream outputStream) throws IOException, WebApplicationException {
                        outputStream.write(stringWriter.toString().getBytes(Charset.forName("UTF-8")));
                    }
                }).header("Content-Disposition",contentDisposition).build();
    

    【讨论】:

    • ContentDisposition 来自org.glassfish.jersey.media/jersey-media-multipart,如果您只在客户端和服务器之间使用 JSON,您可能还没有。
    • 在泽西岛 1.17。它来自com.sun.jersey.core.header.ContentDisposition
    【解决方案2】:

    您可以在响应中添加"Content-Disposition header",例如

    rb.header("Content-Disposition",  "attachment; filename=\"thename.jpg\"");
    

    【讨论】:

    • 我无法将此字符串附加到 Content-Disposition 标头 filename*=UTF-8''url_encoded_filename。你知道为什么吗?
    【解决方案3】:

    在不使用 ResponseBuilder 类的情况下,可以将 header 直接设置到 Response 上,这样可以避免任何额外的依赖:

    return Response.ok(entity).header("Content-Disposition", "attachment; filename=\"somefile.jpg\"").build();
    

    【讨论】:

      【解决方案4】:
      @GET
          @Path("zipFile")
          @Produces("application/zip")
          public Response getFile() {
              File f = new File("/home/mpasala/Documents/Example.zip");
              String filename= f.getName();
      
              if (!f.exists()) {
                  throw new WebApplicationException(404);
              } else {
                  Boolean success = moveFile();
      
              }
      
              return Response
                      .ok(f)
                      .header("Content-Disposition",
                              "attachment; filename="+filename).build();
          }
      

      在这里我找到了我的问题的解决方案。我在响应头中附加了文件名。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-07
        • 2020-07-14
        • 1970-01-01
        • 2016-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多