为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

拉风的咖菲猫 2023-08-23 15:07:36
我正在开发一个应用程序,该应用程序会将第三方依赖项下载到特定文件夹,然后对其执行依赖项检查。下载的文件可以是任何类型,可以是 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!");        }```
查看完整描述

2 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

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

        }```


查看完整回答
反对 回复 2023-08-23
?
SMILET

TA贡献1796条经验 获得超4个赞

public Class CopyAndWrite {


    public static final String SOURCES = "C:\\Users\\Administrator\\Desktop\\resources";

    public static final String TARGET = "C:\\Users\\Administrator\\Desktop\\111";


    public static void main (String[]args) throws IOException {


        Path startingDir = Paths.get(SOURCES);


        Files.walkFileTree(startingDir, new FindJavaVisitor());

    }


    private static class FindJavaVisitor extends SimpleFileVisitor<Path> {


        @Override

        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {

            if (!StringUtils.equals(dir.toString(), SOURCES)) {

                Path targetPath = Paths.get(TARGET + dir.toString().substring(SOURCES.length()));

                if (!Files.exists(targetPath)) {

                    Files.createDirectory(targetPath);

                }

            }

            return FileVisitResult.CONTINUE;

        }


        @Override

        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

            Path targetPath = Paths.get(TARGET + file.toString().substring(SOURCES.length()));

            copyFile(targetPath, Files.readAllBytes(file));


            return FileVisitResult.CONTINUE;

        }

    }


    private static void copyFile (Path path,byte[] bytes){

        // write file

        try {

            Files.write(path, bytes);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}


查看完整回答
反对 回复 2023-08-23
  • 2 回答
  • 0 关注
  • 111 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信