亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號