休息 - 如何为手机下载 PDF

Rest - How to download PDF for mobile

我只在 android 和 iphone 上测试过。

我们有一个网络应用程序,用户可以在其中下载 pdf 文件 这适用于笔记本电脑,但是当我在 android 或 iphone 上按下 link 时,下载不会开始,但 pdf 从未显示或下载。

我在 Content-Disposition Header 中尝试了很多不同的变体,但目前没有任何效果。

有什么想法吗?

@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response showPDF(@Context HttpServletRequest httpRequest) {
        byte[] dokument = somePDF();

        return Response.ok(dokument)
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM)
                .header(HttpHeaders.CONTENT_DISPOSITION, "inline;filename=\"kvittering.PDF\"")
                .header(HttpHeaders.CONTENT_LENGTH, dokument.length)
                .build();
    }

我已经尝试了所有这些,但行为相同:

.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"kvittering.PDF\"")
.header(HttpHeaders.CONTENT_DISPOSITION,"inline;filename=\"kvittering.PDF\"")
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"kvittering.PDF\"")
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"kvittering.PDF\"")
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=kvittering.pdf")

我也试过了 @Produces("application/pdf") 结果相同

所以 here 你有来自 mkyong

的教程

主要区别似乎是@Produces 注释设置

@Produces("application/pdf")

完整代码:

import java.io.File;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

@Path("/pdf")
public class PdfService {

    private static final String FILE_PATH = "c:\Android-Book.pdf";

    @GET
    @Path("/get")
    @Produces("application/pdf")
    public Response getFile() {

        File file = new File(FILE_PATH);

        ResponseBuilder response = Response.ok((Object) file);
        response.header("Content-Disposition",
                "attachment; filename=new-android-book.pdf");
        return response.build();

    }

}

对我来说这可行,但下载的是 xml 文件,因此我使用:

@Produces("application/xml")

我想我们已经解决了。虽然尚未验证,但会 post 回答并在验证后接受。

我们的登台服务器似乎使用了无效的 ssl 证书,android 不喜欢这样。我相信这就是它不起作用的原因。

我会尝试在 phone 上安装证书,然后重试。