我正在尝试使用新的Wikipedia rest api {https://en.wikipedia.org/api/rest_v1/#!/Page_content/generatePDF)get
为Wikipedia文章创建pdf文件。
我正在通过此链接(http://www.benchresources.net/resteasy-jax-rs-web-service-for-uploadingdownloading-pdf-file-java-client/)使用类TestDownloadFileService.java。这是我的课程:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.glassfish.jersey.client.*;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
public class TestDownloadFileService {
public static final String DOWNLOAD_FILE_LOCATION = "/home/Downloads";
public static void main(String []args) throws IOException {
String httpURL = "https://en.wikipedia.org/api/rest_v1/page/pdf/Forest";
String responseString = testDownloadService(httpURL);
System.out.println("responseString : " + responseString);
}
/**
* using Client, WebTarget from core JAX-RS classes javax.ws.rs.client
* @param url
*/
public static String testDownloadService(String httpURL) throws IOException {
// local variables
Client client = null;
WebTarget webTarget = null;
Builder builder = null;
Response response = null;
InputStream inputStream = null;
OutputStream outputStream = null;
int responseCode;
String responseMessageFromServer = null;
String responseString = null;
String qualifiedDownloadFilePath = null;
try {
// invoke service after setting necessary parameters
client = ClientBuilder.newClient();
webTarget = client.target(httpURL);
client.property("accept", "application/pdf");
builder = webTarget.request();
response = builder.get();
// get response code
responseCode = response.getStatus();
System.out.println("Response code: " + responseCode);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed with HTTP error code : " + responseCode);
}
// get response message
responseMessageFromServer = response.getStatusInfo().getReasonPhrase();
System.out.println("ResponseMessageFromServer: " + responseMessageFromServer);
// read response string
inputStream = response.readEntity(InputStream.class);
qualifiedDownloadFilePath = DOWNLOAD_FILE_LOCATION + "MyPdfFile.pdf";
outputStream = new FileOutputStream(qualifiedDownloadFilePath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
// set download SUCCES message to return
responseString = "downloaded successfully at " + qualifiedDownloadFilePath;
}
catch(Exception ex) {
ex.printStackTrace();
}
finally{
// release resources, if any
outputStream.close();
client.close();
}
return responseString;
}
}
但是我在第87行和第26行分别得到了NullPointerException:
client.close();
String responseString = testDownloadService(httpURL);
在此先感谢您指出原因和解决方案。