使用REST API下载文件

编程入门 行业动态 更新时间:2024-10-18 12:19:31
本文介绍了使用REST API下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用Java客户端调用REST API.

I am trying to call a REST API using Java client.

Rest API api.gdc.cancer.gov/data 具有文件数据. 当我将文件名附加到URL时( api .gdc.cancer.gov/data/556e5e3f-0ab9-4b6c-aa62-c42f6a6cf20c ),我可以使用浏览器下载给定文件.

The Rest API api.gdc.cancer.gov/data has files data. When I append file name to the URL (api.gdc.cancer.gov/data/556e5e3f-0ab9-4b6c-aa62-c42f6a6cf20c) I can download the given file from using browser.

此处文件名是556e5e3f-0ab9-4b6c-aa62-c42f6a6cf20c.

请让我知道,我如何在这个JAVA中实现.我正在使用的代码.

can you please let me know,How can i achieve in this JAVA. The code I am using.

import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.HttpURLConnection; import java.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 = "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文件,因此无法仅输出String.如果您只想下载文件,则可以使用一种更简单的方法来改编此答案:

You can't just output a String since you are trying to download a pdf. If you simply want to download the File there is an easier method adapted from this answer:

String requestUrl = "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文件,没有任何问题.

I have tested it for the URL you provided and got the pdf File without problems.

更多推荐

使用REST API下载文件

本文发布于:2023-11-28 09:58:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641938.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   REST   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!