最近需要将一个老系统的功能迁移到新系统,老系统将不再使用,有一个从第三方接口下载文件的功能,post测试如下
在程序内部下载文件,并保存到服务器。hutool便派上用场。首先引入hutool依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.2</version>
</dependency>
具体代码如下,因为知道下载的文件都是pdf,所以保存的时候直接以pdf格式保存了
/**
* @return java.lang.String
* @author
* @description 从网络URL中下载文件
* @date 15:33 2021/11/2
* @params [fileUrl, diskPath]
*/
public static void downLoadFromUrl(String docCode,String docVer ) {
final String url = "http://*******:**/api/erp/u9/doc/downloadXB";
// 请求目标资源
String param = "[{\"docCode\":\""+ docCode+"\",\"docVer\":\""+docVer+"\",\"fileType\":\"2\"}]";
HttpRequest post = HttpUtil.createPost(url);
post.body(param);
// 获取发送请求后的响应对象
HttpResponse execute = post.timeout(30000).execute();
System.out.println(execute.body());
FileUtil.writeBytes(execute.bodyBytes(),"D:\\2222.pdf");
}
public static void main(String[] args) {
downLoadFromUrl("PRE2305261407","00");
}
文件成功下载!!