首页 > 编程语言 > 详细

Java 通过URL进行远程文件下载

时间:2020-09-01 16:28:27      阅读:926      评论:0      收藏:0      [点我收藏+]
@Service
public class FileService {
private static Logger LOGGER = LoggerFactory.getLogger(FileService.class);

@Autowired
private HttpService httpService;
@Autowired
private CloseableHttpClient httpClient;
@Autowired
private RequestConfig config;

public ByteArrayInputStream download2(String url) {
CloseableHttpResponse response = null;
try {
url = url.replaceAll(" ", "%20");
LOGGER.info("download fileUrl:{}",url);
URIBuilder builder = new URIBuilder(url);
builder.setParameter("code", genGoogleCode());
HttpGet httpGet = new HttpGet(builder.build());
httpGet.setConfig(config);
response = this.httpClient.execute(httpGet);
} catch (Exception e) {
LOGGER.error("文件下载失败 {}", e);
throw new MultipartException("文件下载失败");
}
try (InputStream inputStream = response.getEntity().getContent()) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(IOUtils.toByteArray(inputStream));
return byteArrayInputStream;
} catch (Exception e) {
LOGGER.error("文件服务器获取Outputstream 错误!{}", e);
throw new MultipartException("文件服务器获取Output Stream 错误");
}
}
}

 

Java 通过URL进行远程文件下载

原文:https://www.cnblogs.com/yhc-love-cl/p/13596256.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!