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)