Kyle
Kyle

Reputation: 2379

Java download to target (target can't be found) and 0 byte file download

I am trying to use this code below on button click to download a file and save it to a location. However, it always returns that the target destination could not be found. It seems to not create the directories needed. Do I have to tell it to do that or is it supposed to do it for me? Below is the function I'm using:

public void actionPerformed(ActionEvent ae) {
            try {
                //ProgressBar/Install
                System.out.println("FILELOCATION:\n----------");
                System.out.println(filelocation.getText());
                String URL_LOCATION = "http://www.futureretrogaming.tk/gamefiles/ProfessorPhys.iso";
                String LOCAL_FILE = (filelocation.getText() + "\\ProfessorPhys\\");
                System.out.println("LOCALFILE:\n-------");
                System.out.println(LOCAL_FILE);
                URL website = new URL(URL_LOCATION);
                ReadableByteChannel rbc = Channels.newChannel(website.openStream());
                FileOutputStream fos = new FileOutputStream(LOCAL_FILE+"\\ProfessorPhys.iso\\");
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                System.out.println("--------\nDone Downloading\n---------");
            } catch (Exception e) {
                System.out.println(e);
            }

EDIT :
If I manually create the directories it downloads (kinda). Is there anyway to check if the directory already exists and if it doesn't create it? Also, when it downloads, it just makes the file it doesn't download it as the downloaded file that shows up is 0 bytes. How can I fix this?

EDIT:
I have created the below code that checks if the directory exist and if it does not, creates it. This does work however, the only thing that still does not work is the download. I have included that problem in the title now.

For anyone who wants the code that works it is below:

        String LOCAL_FILE = (filelocation.getText() + "\\ProfessorPhys\\");
        File localfile = new File(LOCAL_FILE);
        if (localfile.exists()) {
            System.out.println("Directory exists!");
        }
        else {
            System.out.println("Directory doesn't exist! Creating...");
            localfile.mkdir();
            if (localfile.exists())
                System.out.println("Directory created!");
        }

Upvotes: 0

Views: 363

Answers (2)

Kyle
Kyle

Reputation: 2379

This code worked for me:

//ProgressBar/Install
                System.out.println("FILELOCATION:\n----------");
                System.out.println(flocation);
                String URL_LOCATION = "http://www.futureretrogaming.tk/gamefiles/ProfessorPhys.iso";
                String LOCAL_FILE = (flocation + "\\ProfessorPhys\\");
                File localfile = new File(LOCAL_FILE);
                if (localfile.exists()) {
                    System.out.println("Directory exists!");
                }
                else {
                    System.out.println("Directory doesn't exist! Creating...");
                    localfile.mkdir();
                    if (localfile.exists())
                        System.out.println("Directory created!");
                }
                System.out.println("LOCALFILE:\n-------");
                System.out.println(LOCAL_FILE);
                URL website = new URL(URL_LOCATION);
                ReadableByteChannel rbc = Channels.newChannel(website.openStream());
                FileOutputStream fos = new FileOutputStream(LOCAL_FILE+"\\ProfessorPhys.iso\\");
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                System.out.println("--------\nDone Downloading\n---------");

Upvotes: 0

Daniel H.
Daniel H.

Reputation: 1842

You are using the transferFrom() method as a one-liner. However, I think you should check whether more data is available. From the API:

An attempt is made to read up to count bytes from the source channel and write them to this channel's file starting at the given position. An invocation of this method may or may not transfer all of the requested bytes; whether or not it does so depends upon the natures and states of the channels. Fewer than the requested number of bytes will be transferred if the source channel has fewer than count bytes remaining, or if the source channel is non-blocking and has fewer than count bytes immediately available in its input buffer.

A similar question was also posted that may help you.

Upvotes: 2

Related Questions