简体   繁体   English

如何保存从 HTTP 获取的文件在磁盘文件 java 上获取响应

[英]How to save file getting from HTTP GET response on disk file java

I am getting a file in response to the HTTP GET request.我收到一个响应 HTTP GET 请求的文件。 I tried the following curl command which is providing me the exact output that I want:我尝试了以下 curl 命令,它为我提供了我想要的确切 output :

curl -X GET <URL> -H 'Authorization: Bearer <token>' --output <Path on local machine>/Test.pdf

Can someone provide a java code to save this file on my local storage without using an external library?有人可以提供 java 代码来将此文件保存在我的本地存储中而不使用外部库吗?

How about trying to write the code and get some practice on java.net and java.io packages?尝试编写代码并在 java.net 和 java.io 包上进行一些练习怎么样?

Please take a look at class URL and method openStream for getting access to content.请查看 class URL和方法openStream以访问内容。

You can then save that by leveraging Java File IO, classes of interest would be BufferedInputStream for reading content and BufferedOutputStream for saving content.然后,您可以通过利用 Java 文件 IO 来保存它,感兴趣的类将是用于读取内容的BufferedInputStream和用于保存内容的BufferedOutputStream

Please refer File IO Tutorial请参考 文件 IO 教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM