我正在尝试下载使用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());
}
}
但上面的代码片段返回400 Bad Request。不确定我是否错误地指定了URL。在Postman中使用相同的URL返回PDF文件。
Exception in thread "main" javax.ws.rs.WebApplicationException: Http Call failed. response code is 400. Error reported is Bad Request
删除证书块也会返回PKIX Certification Exception,而我已经在主类中定义了它并在其中一个子类中使用它。
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方面我出错了。
任何相同的帮助/指导都会有所帮助。
可能是评论但不能在评论中格式化。
阅读文档,我可以看到
- 检索报告数据。
报告完成后,客户端可以以多种格式检索其数据或报告的呈现版本。
以下资源可用于检索报告的呈现版本:
/profiler/1.0/reporting/reports/{id}/view.pdf /profiler/1.0/reporting/reports/{id}/view.csv
这些分别适用于PDF和CSV版本。
你能试试吗?
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");
希望这可以帮助。