0

I am working on creating a personal utility downloader for things like Malwarebytes, Adware Cleaner, and etc. But I have never worked with anything like this before. I searched around and found some documentation on how to download files from a URL into a directory, but I haven't been able to get it to work yet. The first time it turned the directory into a file that was unsuable and now that I have changed the URL it is failing to download due to the errors listed at the bottom. Could someone point me in the right direction or tell me what I am doing wrong?

package com.kcc;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class Testing2 {
    public static String testURL;
    public static String saveDir;
    public static void main(String[] args) throws IOException {
        testURL ="https://download.bleepingcomputer.com/dl/a652734ff3304da2530acb93754c1bf7/5af5a320/windows/security/security-utilities/a/adwcleaner/AdwCleaner.exe";
                //"https://download.toolslib.net/download/file/1/1511?s=2LPvu8kniU2T794QD0FXSN21jxnJOqLP";
        saveDir = "C:\\Users\\Austin\\Desktop\\kccutil";
        download(testURL, saveDir);
    }

    private static Path download(String sourceURL, String targetDirectory) throws IOException
    {
        URL url = new URL(sourceURL);
        String fileName = sourceURL.substring(sourceURL.lastIndexOf('/') + 1, sourceURL.length());
        Path targetPath = new File(targetDirectory + File.separator + fileName).toPath();
        Files.copy(url.openStream(), targetPath, StandardCopyOption.REPLACE_EXISTING);
        return targetPath;
    }
}

I am currently getting these errors

Exception in thread "main" java.nio.file.NoSuchFileException: C:\Users\Austin\Desktop\kccutil\AdwCleaner.exe
    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
    at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:434)
    at java.nio.file.Files.newOutputStream(Files.java:216)
    at java.nio.file.Files.copy(Files.java:3016)
    at com.kcc.Testing2.download(Testing2.java:25)
    at com.kcc.Testing2.main(Testing2.java:17)

EDIT: For the error above, turns out the directory wasn't created. But now I am receiving a new error

Exception in thread "main" java.io.FileNotFoundException: https://download.bleepingcomputer.com/dl/a652734ff3304da2530acb93754c1bf7/5af5a320/windows/security/security-utilities/a/adwcleaner/AdwCleaner.exe
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1872)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at java.net.URL.openStream(URL.java:1045)
    at com.kcc.Testing2.download(Testing2.java:25)
    at com.kcc.Testing2.main(Testing2.java:17)
Auti
  • 23
  • 3
  • 1
    Does the folder "C:\Users\Austin\Desktop\kccutil\" already exist? – assylias May 11 '18 at 14:18
  • In case it doesn't, you may use `Files.createDirectories(targetPath.getParent());` before calling `Files.copy` . – Arnaud May 11 '18 at 14:22
  • So I quickly manually created it and ran the program again, it was changed to a file the first time around and I didn't recreate it. Now the errors I am getting are; Exception in thread "main" java.io.FileNotFoundException: https://download.bleepingcomputer.com/dl/a652734ff3304da2530acb93754c1bf7/5af5a320/windows/security/security-utilities/a/adwcleaner/AdwCleaner.exe I'll add the full new error to the post. – Auti May 11 '18 at 14:35

2 Answers2

0

For the debugging purposes you can try saving files to working directory (i.e. refer to . folder). Using this approach you can access file by it's filename.

For the future I recommend you using Java 7 NIO Api: Paths.get() - for originally building path from parts, path.parent() - to refer to parent directory, path.resolve() - to build child path.

schaffe
  • 399
  • 2
  • 16
-1

If you want to download a file you should use FTP server and not HTTP in case you have the executable. But if you already have an HTTP link on web who calls a downloadable .exe (Like in your case), you don't really need a download method. You just need to send an http request to navigator (preferably if it is a web app), something like:

File htmlFile = new File(url);
Desktop.getDesktop().browse(htmlFile.toURI());

Or you can download that file using Apache Common IO's FileUtils:

import org.apache.commons.io.FileUtils;
FileUtils.copyURLToFile(url, file_destination);

Or you can check this response using Java NIO