如何在Java中从第三方Rest终端下载文件

huangapple go评论102阅读模式
英文:

How to download files from a third party Rest end point in Java

问题

  1. 我想在Java中从第三方REST端点下载文件
  2. 谷歌提供了一个样本CURL并且按预期工作但我想在Java中进行设计
  3. 我尝试在谷歌上搜索但没有得到帮助
  4. 请求方法是GET”,并且是一个多部分HTTP请求
  5. ```java
  6. curl -L -O -k -u '用户名:密码' -X GET http://localhost:8080/secure/attachment/1461863/fileName.txt

任何样本代码或链接都会很有帮助。

谢谢

  1. <details>
  2. <summary>英文:</summary>
  3. I want to download a file from a third party rest end-point in java
  4. Sample CURL available from google and it is working as expected but I want to design it in java.
  5. I tried googling it but could not get help.
  6. Request method is &quot;GET&quot; and a multipart HTTP request

curl -L -O -k -u 'username:password' -X GET http://localhost:8080/secure/attachment/1461863/fileName.txt

  1. Any sample code or link will do good.
  2. Thanks
  3. </details>
  4. # 答案1
  5. **得分**: 0
  6. 如果您不想使用除了jdk以外的额外依赖,可以使用类似以下的方法:
  7. ```java
  8. URL url = new URL("http://localhost:8080/secure/attachment/1461863/fileName.txt");
  9. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  10. conn.setRequestMethod("GET");
  11. if (conn.getResponseCode() != 200) {
  12. throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
  13. }
英文:

If you don't want to use additional dependecies other than jdk, you can use something like this:

  1. URL url = new URL(&quot;http://localhost:8080/secure/attachment/1461863/fileName.txt&quot;);
  2. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  3. conn.setRequestMethod(&quot;GET&quot;);
  4. if (conn.getResponseCode() != 200) {
  5. throw new RuntimeException(&quot;Failed : HTTP error code : &quot;
  6. + conn.getResponseCode());
  7. }

huangapple
  • 本文由 发表于 2020年5月4日 15:49:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61587431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定