problem:
Sometimes we need to download some link files submitted by users to the local, this time we can use the following method
/**
* 从URL下载文件
*
* @param url 下载文件的路径
* @param dirLocation 保存下载文件的位置
* @return String 文件下载位置
*/
public static String downloadFileFromURL(String url, String dirLocation) throws OperationException {
String fileName = "";
try {
URL httpUrl = new URL(url);
String fileNameByUrl = ZStringUtils.getFileNameByUrl(url);
File file = new File(dirLocation.concat(fileNameByUrl));
file.createNewFile();
FileUtils.copyURLToFile(httpUrl, file);
return file.getPath();
} catch (Exception e) {
e.printStackTrace();
log.info("下载文件======{}=====失败", fileName);
}
throw new OperationException("无法获取远程文件");
}
ZStringUtils is a tool class to get the file name, you can get it yourself.