第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

java中如何將文件下載到指定文件夾中?

java中如何將文件下載到指定文件夾中?

拉風(fēng)的咖菲貓 2023-08-23 15:07:36
我正在開(kāi)發(fā)一個(gè)應(yīng)用程序,該應(yīng)用程序會(huì)將第三方依賴項(xiàng)下載到特定文件夾,然后對(duì)其執(zhí)行依賴項(xiàng)檢查。下載的文件可以是任何類型,可以是 zip、jar 或可能是 ba 文件夾。我試圖找到一個(gè)代碼示例,但似乎沒(méi)有什么對(duì)我有用。我在java中嘗試過(guò)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貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊

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

        }```


查看完整回答
反對(duì) 回復(fù) 2023-08-23
?
SMILET

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

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

        }

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-08-23
  • 2 回答
  • 0 關(guān)注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)