根据网络地址下载图片及文件

根据网络地址下载图片及文件

绿林寻猫
2021-12-08 / 0 评论 / 64 阅读 / 正在检测是否收录...

    /**
     * @Author LiuYong
     * @Date 2019/12/13  17:44
     * @Description TODO
     * @param urlList url下载地址
     * @param path 保存地址
     * @return
     **/
    private static void downloadPicture(String urlList,String path) {
        URL url = null;
        try {
            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;
            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

0

评论 (0)

取消