zoukankan      html  css  js  c++  java
  • Java之下载网络图片到本地文件夹

    核心代码(下载网络图片到本地文件夹):

    public class DownFileUtils {
        public static void downloadFile(String remoteFilePath, String localFilePath) {
            URL urlfile = null;
            HttpURLConnection httpUrl = null;
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            File f = new File(localFilePath);
            try {
                urlfile = new URL(remoteFilePath);
                httpUrl = (HttpURLConnection) urlfile.openConnection();
                httpUrl.connect();
                bis = new BufferedInputStream(httpUrl.getInputStream());
                bos = new BufferedOutputStream(new FileOutputStream(f));
                int len = 2048;
                byte[] b = new byte[len];
                while ((len = bis.read(b)) != -1) {
                    bos.write(b, 0, len);
                }
                bos.flush();
                bis.close();
                httpUrl.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bis.close();
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        public static void main(String[] args) {
       
    //注意这个本地文件夹(localFileMkdir)必须要指定文件的名称如test.jpg,否则会出现下载失败(控制台会报错,访问拒绝)。 
    
    DownFileUtils.downloadFile(networkImgUrl,localFileMkdir);
    
    //  例子  DownFileUtils.downloadFile("http://www.test.com/img/2020.jpg","D:\test\test.jpg");
        
        }
    }
  • 相关阅读:
    java.lang.ArrayIndexOutOfBoundsException异常分析及解决
    Android_开发片段(Part 2)
    保存错误日志回传服务器之回传错误“信息文件”
    node.js
    拼接json
    CommonJS / Node.js/ Vue学习资料
    合并PDF
    java 多线程
    linux 运行jar包
    mvn 命令
  • 原文地址:https://www.cnblogs.com/youcong/p/14015632.html
Copyright © 2011-2022 走看看