如何将文件下载到java中的特定文件夹中?

如何将文件下载到java中的特定文件夹中?,java,Java,我正在开发一个应用程序,它将第三方依赖项下载到特定文件夹,然后对其执行依赖项检查。下载的文件可以是任何类型,它们可以是zip、jar,也可以是文件夹。我试图找到一个代码示例,但似乎没有任何东西适合我。我尝试了java中的NIO,但它似乎只适用于写入特定的文件,而不适用于文件夹。下面是我使用NIO的代码 // Checking If The File Exists At The Specified Location Or Not Path filePathObj

我正在开发一个应用程序,它将第三方依赖项下载到特定文件夹,然后对其执行依赖项检查。下载的文件可以是任何类型,它们可以是zip、jar,也可以是文件夹。我试图找到一个代码示例,但似乎没有任何东西适合我。我尝试了java中的NIO,但它似乎只适用于写入特定的文件,而不适用于文件夹。下面是我使用NIO的代码


        // Checking If The File Exists At The Specified Location Or Not
        Path filePathObj = Paths.get(filePath);
        boolean fileExists = Files.exists(filePathObj);
        if(fileExists) {
            try {
                urlObj = new URL(sampleUrl);
                rbcObj = Channels.newChannel(urlObj.openStream());
                fOutStream = new FileOutputStream(filePath);

                fOutStream.getChannel().transferFrom(rbcObj, 0, Long.MAX_VALUE);
                System.out.println("! File Successfully Downloaded From The Url !");
            } catch (IOException ioExObj) {
                System.out.println("Problem Occured While Downloading The File= " + ioExObj.getMessage());
            } finally {
                try {
                    if(fOutStream != null){
                        fOutStream.close();
                    }
                    if(rbcObj != null) {
                        rbcObj.close();
                    }
                } catch (IOException ioExObj) {
                    System.out.println("Problem Occured While Closing The Object= " + ioExObj.getMessage());
                }
            }
        } else {
            System.out.println("File Not Present! Please Check!");
        }```
公共类CopyAndWrite{
公共静态最终字符串SOURCES=“C:\\Users\\Administrator\\Desktop\\resources”;
公共静态最终字符串TARGET=“C:\\Users\\Administrator\\Desktop\\111”;
公共静态void main(字符串[]args)引发IOException{
Path startingDir=Path.get(SOURCES);
walkFileTree(startingDir,newfindjavavisitor());
}
私有静态类FindJavaVisitor扩展了SimpleFileVisitor{
@凌驾
公共文件VisitResult preVisitDirectory(路径目录,基本文件属性属性属性)引发IOException{
如果(!StringUtils.equals(dir.toString(),SOURCES)){
Path targetPath=Path.get(TARGET+dir.toString().substring(SOURCES.length());
如果(!Files.exists(targetPath)){
Files.createDirectory(targetPath);
}
}
返回FileVisitResult.CONTINUE;
}
@凌驾
公共文件VisitResult visitFile(路径文件,基本文件属性属性属性)引发IOException{
Path targetPath=Path.get(TARGET+file.toString().substring(SOURCES.length());
copyFile(targetPath,Files.readAllBytes(file));
返回FileVisitResult.CONTINUE;
}
}
私有静态void copyFile(路径,字节[]字节){
//写入文件
试一试{
写入(路径,字节);
}捕获(IOE异常){
e、 printStackTrace();
}
}
}

使用OKHttpClient下载文件并放入文件夹

        Request request = new Request.Builder().url(downloadUrl).build();
        Response response;
        try {
            response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                fileName = abc.zip
                Path targetPath = new File(inDir + File.separator + fileName).toPath();
                try (FileOutputStream fos = new FileOutputStream(targetPath)) {
                    fos.write(response.body().bytes());
                }
                return 0;
            }
        } catch (IOException e) {
            logger.error(e.getMessage());
        }```

请阅读“如何创建一个应用程序”。然后使用链接改进您的问题(不要通过评论添加更多信息)。否则,我们无法回答您的问题并帮助您。严肃地说:所有允许您在“某处”编写文件的JavaAPI都包含一种定义“某处”的方法。因此,你的要求真的不清楚。你总是把文件放在某个文件夹里。你说的“但似乎什么都不适合我”是什么意思?你尝试了什么代码,你期望得到什么结果,你得到了什么结果?@GhostCat我想将任何扩展名的文件下载到一个特定的文件夹。从你的代码中可以清楚地看到“你传递了你想下载的文件的url”,这意味着。。你可以下载任何你喜欢的东西,只要你有网址。其次是一个
FileOutputStream
filePath
参数是写入文件的位置。这意味着您已经定义了文件夹(即完整路径减去文件名)。除此之外,还不清楚您的问题是什么,也不清楚您尝试了什么,也不清楚您是如何解决问题的如果您使用的是Java 11+并且远程设备使用HTTP,请查看模块,尤其是。
        Request request = new Request.Builder().url(downloadUrl).build();
        Response response;
        try {
            response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                fileName = abc.zip
                Path targetPath = new File(inDir + File.separator + fileName).toPath();
                try (FileOutputStream fos = new FileOutputStream(targetPath)) {
                    fos.write(response.body().bytes());
                }
                return 0;
            }
        } catch (IOException e) {
            logger.error(e.getMessage());
        }```