【问题标题】:Download file using REST API使用 REST API 下载文件
【发布时间】:2019-01-08 19:27:59
【问题描述】:

我正在尝试使用 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 + "] } } }";
    } 
}

【问题讨论】:

  • 我不知道这是否是您的问题,但文件是 pdf 并且您将标题设置为接受 json。
  • { "message": "405 Method Not Allowed: The method is not allowed for the requested URL." } 甚至对 API 的 POST 调用也不起作用。
  • 不要出现任何异常。我只是想帮助修改代码以下载文件。附加文件名时,对 API 的 POST 调用通过浏览器工作。
  • 浏览器执行 GET 请求,您的代码执行 POST 请求。
  • 我在邮递员中尝试了 GET,得到了文件。

标签: java rest api


【解决方案1】:

您不能只输出字符串,因为您正在尝试下载 pdf。如果您只是想下载文件,有一个更简单的方法改编自 this 答案:

    String requestUrl = "https://api.gdc.cancer.gov/data/556e5e3f-0ab9-4b6c-aa62-c42f6a6cf20c";
    URL url = new URL(requestUrl);
    InputStream in = url.openStream();
    Files.copy(in, Paths.get("your_filename.pdf"), StandardCopyOption.REPLACE_EXISTING);
    in.close();
    System.out.println("finished!");

我已经针对您提供的 URL 进行了测试,并且没有问题地获得了 pdf 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 2017-06-18
    • 2015-07-26
    • 2015-10-27
    相关资源
    最近更新 更多