我正在尝试使用Java客户端调用REST API。
Rest API https://api.gdc.cancer.gov/data具有文件数据。 将文件名附加到URL(https://api.gdc.cancer.gov/data/556e5e3f-0ab9-4b6c-aa62-c42f6a6cf20c)后,可以使用浏览器下载给定文件。
此处文件名是556e5e3f-0ab9-4b6c-aa62-c42f6a6cf20c
。
请让我知道,我如何在这个JAVA中实现。我正在使用的代码。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class DownloadFilesAPI {
public DownloadFilesAPI() {
super();
}
public static String sendPostRequest(String requestUrl) {
StringBuffer jsonString = new StringBuffer();
try {
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// connection.setRequestMethod("POST");
// connection.connect();
//Get the response status of the Rest API
// int responsecode = connection.getResponseCode();
//System.out.println("Response code is: " +responsecode);
//connection.getResponseMessage();
// System.out.println(connection.getResponseMessage());
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");
// System.out.println(connection.getResponseMessage());
// System.out.println( JsonPath.from(requestUrl));
OutputStreamWriter writer = new
OutputStreamWriter(connection.getOutputStream());
writer.write(requestUrl);
writer.close();
/* BufferedReader br = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close(); */
connection.disconnect();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return jsonString.toString();
}
public static void main(String[] args) {
List<String> values = new ArrayList<>();
// values.add("556e5e3f-0ab9-4b6c-aa62-c42f6a6cf20c");
String requestUrl = "https://api.gdc.cancer.gov/data/556e5e3f-0ab9-4b6c-aa62-c42f6a6cf20c";
sendPostRequest(requestUrl);
}
private static String preparePayload(List<String> values) {
StringBuilder sb = new StringBuilder();
for (String value : values) {
sb.append("\"" + value + "\",");
}
String Requiredvalue = sb.toString().substring(0, sb.toString().length() - 1);
return "{ \"ids\":[" + Requiredvalue + "] } } }";
}
}
答案 0 :(得分:2)
由于您正在尝试下载pdf文件,因此无法仅输出字符串。如果您只想下载文件,则可以使用一种更简单的方法来改编this答案:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="CheckOn" >
<table id="tblID" BORDER="1">
<tr class="heading">
<td>Items</td>
<td>Default value</td>
<td>Extra(if any)</td>
<td>Output</td>
</tr>
<tr>
<td>Apple</td>
<td>
<input type="text" value="26" size=15 name="apple">
</td>
<td>
<input type="text" value="0" size=15 name="apple">
</td>
<td>
<input type="text" size=15 name="apple">
</td>
</tr>
<tr>
<td>Mango</td>
<td><input type="text" size=15 value="26" name="SDK"></td>
<td>
<input type="text" value="0" size=15 name="SDK">
</td>
<td>
<input type="text" size=15 name="SDK">
</td>
</tr>
<tr>
<td>Banana</td>
<td>
<input type="text" value="26" size=15 name="Banana">
</td>
<td>
<input type="text" value="27" size=15 name="Banana">
</td>
<td>
<input type="text" size=15 name="Banana">
</td>
</tr>
<tr>
</table>
<p><input type="submit" value="submit" id="subs" name="subs"></p>
我已经对您提供的URL进行了测试,并获得了pdf文件,没有任何问题。