JAX RS 下载 PDF

JAX RS download PDF

我正在尝试使用 JAX RS 和 Jersey 并获得授权来下载其他 URL 之一可用的 PDF 文件。

import org.apache.commons.io.IOUtils;
import javax.net.ssl.*;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.File;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

public class ReportView {
    public void process(String authStringEnc) {

        System.setProperty("javax.net.ssl.trustStore","C:\Users\alim\Desktop\my-app\stage\npkeystore.jks");
        System.setProperty("javax.net.ssl.trustStorePassword","changeit");
        System.setProperty("javax.net.ssl.trustAnchors","C:\Users\alim\Desktop\my-app\stage\npkeystore.jks");

        Client client = ClientBuilder.newClient();
       // WebTarget target = client.target("https://x.x.x.x/api/profiler/1.0/reporting/reports/751252/").path("view");
        WebTarget target = client.target("https://x.x.x.x/api/profiler/1.0/reporting/reports/751252/view");

        Response resp = target.request("application/pdf,image/jpeg,application/xml,application/vnd.ms-excel").header("Authorization", authStringEnc).get(Response.class);


        System.out.println("Code : " + resp.getStatus());

        if(resp.getStatus() == Response.Status.OK.getStatusCode()) {
            InputStream is = resp.readEntity(InputStream.class);

            File downloadfile = new File("C://Users/alim/Downloads/view.pdf");
            try {

                byte[] byteArray = IOUtils.toByteArray(is);
                FileOutputStream fos = new FileOutputStream(downloadfile);
                fos.write(byteArray);
                fos.flush();
                fos.close();
            }catch(Exception e){
                e.getMessage();
            }

            IOUtils.closeQuietly(is);
            System.out.println("the file details after call:"+ downloadfile.getAbsolutePath()+", size is "+downloadfile.length());
        }
        else{
            throw new WebApplicationException("Http Call failed. response code is"+resp.getStatus()+". Error reported is"+resp.getStatusInfo());
        }
    }

但是上面的代码片段 returns 一个 400 Bad Request 。不确定我是否错误地指定了 URL。在 Postman returns 中使用相同的 URL PDF 文件。

Exception in thread "main" javax.ws.rs.WebApplicationException: Http Call failed. response code is 400. Error reported is Bad Request

同时删除证书块 returns 我已经在主 class 中定义了 PKIX 证书异常并在子 class 之一中使用它。

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

JAX-RS 和 Jersey 概念对我来说很新。不确定在使用身份验证、证书和请求指定 URL 方面我哪里出错了。

任何 help/guidance 都非常有用。

可能是评论,但无法在评论中设置格式。

阅读文档我可以看到

  1. Retrieve the report data.

Once the report completes, the client can retrieve its data or the rendered version of the report in a number of formats.

The following resources can be used to retrieve a rendered version of the report:

/profiler/1.0/reporting/reports/{id}/view.pdf
/profiler/1.0/reporting/reports/{id}/view.csv

These are for PDF and CSV versions respectively.

你可以试试吗

WebTarget target = client
    .target("https://x.x.x.x/api/profiler/1.0/reporting/reports/751252/view.pdf");
Response resp = target
    .request("application/pdf,image/jpeg,application/xml,application/vnd.ms-excel")
    .header("Authorization", authStringEnc)
    .get(Response.class);

我尝试了另一种方法并能够下载 PDF 文件。下面是相同的代码片段。

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


        String download_url = "https://x.x.x.x/api/profiler/1.0/reporting/reports/" + reportID +"/view";
        String name = "report";
        String password = "report";
        String authString = name + ":" + password;
        String authStringEnc = Base64.getEncoder().encodeToString(authString.getBytes());

        System.out.println(" Downloading " + name + " report");

        Client restClient = Client.create();
        WebResource webResource = restClient.resource(download_url);
        ClientResponse resp = webResource.header("Authorization", "Basic " + authStringEnc)
                .get(ClientResponse.class);


        if(resp.getStatus() != 200){
            System.err.println(" Failed : HTTP error code : " + resp.getStatus());
        }
        else
        {
            System.out.println(" Response : " + resp.getStatus() + " OK. Successfully Connected");
        }

        InputStream is = resp.getEntityInputStream();
        OutputStream os = new FileOutputStream(curDir + "\" + country.toLowerCase() + "\ReportsExtracted\" + name + ".pdf");

        byte[] buffer = new byte[1024];
        int bytesRead;

        while((bytesRead = is.read(buffer)) != -1){
            os.write(buffer, 0, bytesRead);
        }
        is.close();

        //flush OutputStream to write any buffered data to file
        os.flush();
        os.close();

        System.out.println(" Downloaded " + name + " report");

希望对您有所帮助。