英文:
How to download files from a third party Rest end point in Java
问题
我想在Java中从第三方REST端点下载文件。
谷歌提供了一个样本CURL,并且按预期工作,但我想在Java中进行设计。
我尝试在谷歌上搜索,但没有得到帮助。
请求方法是“GET”,并且是一个多部分HTTP请求。
```java
curl -L -O -k -u '用户名:密码' -X GET http://localhost:8080/secure/attachment/1461863/fileName.txt
任何样本代码或链接都会很有帮助。
谢谢
<details>
<summary>英文:</summary>
I want to download a file from a third party rest end-point in java
Sample CURL available from google and it is working as expected but I want to design it in java.
I tried googling it but could not get help.
Request method is "GET" and a multipart HTTP request
curl -L -O -k -u 'username:password' -X GET http://localhost:8080/secure/attachment/1461863/fileName.txt
Any sample code or link will do good.
Thanks
</details>
# 答案1
**得分**: 0
如果您不想使用除了jdk以外的额外依赖,可以使用类似以下的方法:
```java
URL url = new URL("http://localhost:8080/secure/attachment/1461863/fileName.txt");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
英文:
If you don't want to use additional dependecies other than jdk, you can use something like this:
URL url = new URL("http://localhost:8080/secure/attachment/1461863/fileName.txt");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论