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

我正在开发一个应用程序,该应用程序会将第三方依赖项下载到特定文件夹,然后对其执行依赖项检查。下载的文件可以是任何类型,可以是 zip、jar 或可能是 ba 文件夹。我试图找到一个代码示例,但似乎没有什么对我有用。我在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!");

        }```


拉风的咖菲猫
浏览 160回答 2
2回答

慕容森

使用 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());        }```

SMILET

public Class CopyAndWrite {&nbsp; &nbsp; public static final String SOURCES = "C:\\Users\\Administrator\\Desktop\\resources";&nbsp; &nbsp; public static final String TARGET = "C:\\Users\\Administrator\\Desktop\\111";&nbsp; &nbsp; public static void main (String[]args) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; Path startingDir = Paths.get(SOURCES);&nbsp; &nbsp; &nbsp; &nbsp; Files.walkFileTree(startingDir, new FindJavaVisitor());&nbsp; &nbsp; }&nbsp; &nbsp; private static class FindJavaVisitor extends SimpleFileVisitor<Path> {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!StringUtils.equals(dir.toString(), SOURCES)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Path targetPath = Paths.get(TARGET + dir.toString().substring(SOURCES.length()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!Files.exists(targetPath)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Files.createDirectory(targetPath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return FileVisitResult.CONTINUE;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Path targetPath = Paths.get(TARGET + file.toString().substring(SOURCES.length()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; copyFile(targetPath, Files.readAllBytes(file));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return FileVisitResult.CONTINUE;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static void copyFile (Path path,byte[] bytes){&nbsp; &nbsp; &nbsp; &nbsp; // write file&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Files.write(path, bytes);&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java