import java.net.URL;
import java.net.MalformedURLException;
import org.apache.commons.io.FileUtils;
public class FileDownloadUtil {
/**
* 下载文件---返回下载后的文件存储路径
*
* @param url 文件路径
* @param dir 目标存储目录
* @param fileName 存储文件名
* @return
*/
public static void downloadHttpUrl(String url, String dir, String fileName) throws BusinessException {
try {
URL httpurl = new URL(url);
File dirfile = new File(dir);
if (!dirfile.exists()) {
dirfile.mkdirs();
}
FileUtils.copyURLToFile(httpurl, new File(dir+fileName));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); }
}
public static boolean deleteFile(File file) {
if (file.exists()) {
return file.delete();
}
return false;
}
}
参考文章:https://www.cnblogs.com/nemowang1996/p/11603848.html