核心代码(下载网络图片到本地文件夹):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public class DownFileUtils { public static void downloadFile(String remoteFilePath, String localFilePath) { URL urlfile = null; HttpURLConnection httpUrl = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; File f = new File(localFilePath); try { urlfile = new URL(remoteFilePath); httpUrl = (HttpURLConnection) urlfile.openConnection(); httpUrl.connect(); bis = new BufferedInputStream(httpUrl.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream(f)); int len = 2048; byte[] b = new byte[len]; while ((len = bis.read(b)) != -1) { bos.write(b, 0, len); } bos.flush(); bis.close(); httpUrl.disconnect(); } catch (Exception e) { e.printStackTrace(); } finally { try { bis.close(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { //注意这个本地文件夹(localFileMkdir)必须要指定文件的名称如test.jpg,否则会出现下载失败(控制台会报错,访问拒绝)。
DownFileUtils.downloadFile(networkImgUrl,localFileMkdir);
// 例子 DownFileUtils.downloadFile("http://www.test.com/img/2020.jpg","D:\\test\\test.jpg"); } }
|