远程调用http接口下载文件,接口返回流
一、将文件保存本地文章来源:https://www.toymoban.com/news/detail-632702.html
public String httpDownload(String httpUrl){
try {
URL url = new URL(httpUrl) ;
//filePath文件地址,fileName文件名
File file = new File(filePath, fileName);
FileUtils.copyURLToFile(url,file);
} catch (IOException e) {
logger.info("用印文件下载失败:{}",e.getMessage());
return null;
}
//文件地址
return basePath + "/" + fileName;
}
二、将接收到流直接返回文章来源地址https://www.toymoban.com/news/detail-632702.html
public void httpDownload(String httpUrl,HttpServletResponse response)throws IOException{
URL url = new URL(httpUrl) ;
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection() ;
urlConnection.connect() ;
OutputStream outputStream = response.getOutputStream();
InputStream inputStream = urlConnection.getInputStream() ;
IOUtils.copy(inputStream,outputStream);
inputStream.close();
response.flushBuffer();
outputStream.close();
}
到了这里,关于java http远程调用接口下载文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!