public static boolean downloadFile(String relativePath) { if (Main.isOffline) return false; URL url = null; File tempFile = null; try { String mirrorUrl = MirrorUtils.getMirrorUrl(relativePath, null); url = new URL(mirrorUrl); URLConnection con = (url.openConnection()); System.setProperty("http.agent", ""); con.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30"); tempFile = File.createTempFile("Modpack", null); // Download to temporary file OutputStream baos = new FileOutputStream(tempFile); // new FileOutputStream(tempFile) if (GameUpdater.copy(con.getInputStream(), baos) <= 0) { Util.log("Download URL was empty: '%s'", url); return false; } // If no Exception then file loaded fine, copy to output file GameUpdater.copy(tempFile, new File(relativePath)); tempFile.delete(); return true; } catch (MalformedURLException e) { Util.log("Download URL badly formed: '%s'", url); e.printStackTrace(); } catch (FileNotFoundException e) { Util.log("Could not write to temp file: '%s'", tempFile); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; }
public static String getMirrorUrl( String mirrorURI, String fallbackUrl, DownloadListener listener) { if (Main.isOffline) return null; String mirror = "https://raw.github.com/" + Main.repoOwner + "/Technic/" + Main.repoBranch + "/" + mirrorURI; if (isAddressReachable(mirror)) { return mirror; } else { Util.log("Could not connect to repository"); return fallbackUrl; } }
public static Download downloadFile( String url, String output, String cacheName, String md5, DownloadListener listener) throws IOException { if (Main.isOffline) return null; int tries = SettingsUtil.getLoginTries(); File outputFile = new File(output); File tempfile = File.createTempFile("file", null, GameUpdater.tempDir); tempfile.mkdirs(); Download download = null; boolean areFilesIdentical = tempfile.getPath().equalsIgnoreCase(outputFile.getPath()); while (tries > 0) { Util.logi("Starting download of '%s', with %s trie(s) remaining", url, tries); tries--; download = new Download(url, tempfile.getPath()); download.setListener(listener); download.run(); if (!download.isSuccess()) { if (download.getOutFile() != null) { download.getOutFile().delete(); } Util.log("Download of " + url + " Failed!"); if (listener != null) { listener.stateChanged("Download Failed, retries remaining: " + tries, 0F); } } else { String fileMD5 = MD5Utils.getMD5(download.getOutFile()); if (md5 == null || fileMD5.equals(md5)) { Util.logi("Copying: %s to: %s", tempfile, outputFile); if (!areFilesIdentical) { GameUpdater.copy(tempfile, outputFile); } Util.logi("File Downloaded: %s", outputFile); } else if (md5 != null && !fileMD5.equals(md5)) { Util.log("Expected MD5: %s Calculated MD5: %s", md5, fileMD5); } break; } } if (cacheName != null) { if (tempfile.exists()) { GameUpdater.copy(tempfile, new File(GameUpdater.cacheDir, cacheName)); } else { Util.log("Could not copy file to cache: %s", tempfile); } } if (!areFilesIdentical) { tempfile.delete(); } return download; }