//绝对文件路径,文件目标路径 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 { SSLSocketFactory oldSocketFactory = null; HostnameVerifier oldHostnameVerifier = null; urlfile = new URL(remoteFilePath); httpUrl = (HttpURLConnection)urlfile.openConnection(); boolean useHttps = remoteFilePath.startsWith("https"); InputStream inputStream = null; if (useHttps) { HttpsURLConnection https = (HttpsURLConnection) httpUrl; oldSocketFactory = trustAllHosts(https); oldHostnameVerifier = https.getHostnameVerifier(); https.setHostnameVerifier(DO_NOT_VERIFY); inputStream = https.getInputStream(); } else { httpUrl.connect(); inputStream = httpUrl.getInputStream(); } bis = new BufferedInputStream(inputStream); 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(); } } }
java下载文件
最新推荐文章于 2022-06-21 15:20:19 发布