参考:
https://www.cnblogs.com/IT-study/p/9239717.html
类似的实现方式:
public static boolean downRecord(String urlString,String filename,int timeout) { boolean ret = false; File file = new File(filename); try { if(file.exists()){ ret = true; }else{ System.out.println("文件下载操作"); // 构造URL URL url = new URL(urlString); // 打开连接 HttpURLConnection con = (HttpURLConnection )url.openConnection(); con.setConnectTimeout(timeout); con.setReadTimeout(timeout); con.connect(); int contentLength = con.getContentLength(); System.out.println("打印文件的长度"); System.out.println(contentLength); // 输入流 InputStream is = con.getInputStream(); // 1K的数据缓冲 byte[] bs = new byte[1024]; // 读取到的数据长度 int len; // 输出的文件流 File file2=new File(file.getParent()); file2.mkdirs(); if(file.isDirectory()){ }else{ file.createNewFile();//创建文件 } OutputStream os = new FileOutputStream(file); // 开始读取 while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } // 完毕,关闭所有链接 os.close(); is.close(); if(contentLength != file.length()){ file.delete(); ret = false; }else{ ret = true; } } } catch (IOException e) { file.delete(); ret = false; }finally { return ret; } }