尝试下载可执行文件时出现NoSuchFileException

时间:2018-05-11 14:15:41

标签: java filenotfoundexception downloading

我正在为Malwarebytes,Adware Cleaner等创建个人实用程序下载程序。但我以前从未使用过类似的东西。我四处搜索并找到了一些关于如何从URL下载文件到目录的文档,但我还没有能够让它工作。它第一次将目录转换为一个不合适的文件,现在我已经更改了URL,因为底部列出的错误导致它无法下载。有人能指出我正确的方向还是告诉我我做错了什么?

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;
    }
}

我目前正在收到这些错误

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)

编辑:对于上面的错误,结果是没有创建目录。但现在我收到一个新错误

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)

1 个答案:

答案 0 :(得分:-1)

如果您要下载文件,则应使用FTP服务器,而不是HTTP,以防您拥有可执行文件。 但是,如果你已经在网络上有一个HTTP链接,它可以调用可下载的.exe(就像你的情况一样),你真的不需要下载方法。您只需要向导航器发送一个http请求(最好是如果它是一个Web应用程序),例如:

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

或者您可以使用Apache Common IO's FileUtils下载该文件:

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

或者您可以查看此回复using Java NIO