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)
For the debugging purposes you can try saving files to working directory (ie 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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.