public static void downloadFile(String downloadUrl, String path, String fileName) {
log.info(fileName + "下载开始");
ReadableByteChannel rbc = null;
FileOutputStream fos = null;
try {
File pathF = new File(path);
if (!pathF.exists()) {
pathF.mkdirs();
}
URL url = new URL(downloadUrl);
rbc = Channels.newChannel(url.openStream());
fos = new FileOutputStream(path + "/" + fileName);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
log.info(fileName + "下载完成");
} catch (Exception e){
log.error(fileName + "下载失败", e);
} finally {
if (rbc != null) {
try {
rbc.close();
} catch (IOException e) {
log.error("", e);
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
log.error("", e);
}
}
}
}
java根据url下载文件nio
最新推荐文章于 2024-04-02 16:55:31 发布