0

I want to make a program that downloads some files that are needed to run a gain. It's like a launcher that automatically downloads updates. This is the enum I have:

public class Configuration {

    public enum downloadFiles {
        load1 ("load1.png", "https://dl.dropboxusercontent.com/u/51947680/Xenolith/load1.png"),
        load2 ("load2.png", "https://dl.dropboxusercontent.com/u/51947680/Xenolith/load2.png"),
        load3 ("load3.png", "https://dl.dropboxusercontent.com/u/51947680/Xenolith/load3.png");

        public String fileName, URL;

        private downloadFiles(String fileName, String URL) {
            this.fileName = fileName;
            this.URL = URL;
        }

        public String getFileName() {
            return fileName;
        }

        public String getURL() {
            return URL;
        }

    }

}

I also have a class that downloads the files, which is:

public class DownloadUtility {
    private static final int BUFFER_SIZE = 4096;

    /**
     * Downloads a file from a URL
     * @param fileURL HTTP URL of the file to be downloaded
     * @param saveDir path of the directory to save the file
     * @throws IOException
     */
    public static void downloadFile(String fileName, String fileURL)
            throws IOException {
        URL url = new URL(fileURL);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        int responseCode = httpConn.getResponseCode();

        // always check HTTP response code first
        if (responseCode == HttpURLConnection.HTTP_OK) {
            String disposition = httpConn.getHeaderField("Content-Disposition");
            String contentType = httpConn.getContentType();
            int contentLength = httpConn.getContentLength();

            System.out.println("Content-Type = " + contentType);
            System.out.println("Content-Disposition = " + disposition);
            System.out.println("Content-Length = " + contentLength);
            System.out.println("fileName = " + fileName);

            // opens input stream from the HTTP connection
            InputStream inputStream = httpConn.getInputStream();
            String saveFilePath = System.getProperty("user.home") + "/Desktop" + File.separator + fileName;

            // opens an output stream to save into file
            FileOutputStream outputStream = new FileOutputStream(saveFilePath);

            int bytesRead = -1;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            outputStream.close();
            inputStream.close();

            System.out.println("File downloaded");
        } else {
            System.out.println("No file to download. Server replied HTTP code: " + responseCode);
        }
        httpConn.disconnect();
    }
}

So what I want to do is that the downloadFiles method downloads all the files in the enum sequentially. Btw is this a good way of doing this? If there is a better way, can you please tell me, because I'm trying to learn to code java in a neat way.

7
  • 1
    So which bit are you stuck on? It's not clear why you have an instance method called downloadFiles which takes a filename and a URL, despite the fact that the enum itself knows the file and URL... Commented Nov 18, 2015 at 20:06
  • I'm stuck on getting the downloadFile method to download the file from the link in the enum and saving it as the name that's in the enum. Commented Nov 18, 2015 at 20:09
  • 2
    Well it doesn't help that your enum has an unconventional name - that's what makes me think it was a method. But Enum.getValues is probably your friend... Commented Nov 18, 2015 at 20:12
  • What downloadFiles method? I see a downloadFiles enum, a downloadFiles constructor, and a downloadFile method. Is there another method? Where is it? Commented Nov 18, 2015 at 20:16
  • Okay, what should I name the enum? How should I make the for loop work? I'm thinking of something with for (i = 0; i < Configuration.DownloadFile.getLength; i ++), but that doesn't work. @RealSkeptic I meant downloadFiles method, should I name these things different? Commented Nov 18, 2015 at 20:16

1 Answer 1

1

You can do the following:

for(Configuration.downloadFiles df : Configuration.downloadFiles.values()){
    DownloadUtility.downloadFile(df.getFileName(), df.getURL());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.