zoukankan      html  css  js  c++  java
  • 获取网络资源保存本地

    public void saveToFile(String destUrl) {
           FileOutputStream fos = null;
           BufferedInputStream bis = null;
           HttpURLConnection httpUrl = null;
           URL url = null;
           intBUFFER_SIZE = 1024;
           byte[] buf = newbyte[BUFFER_SIZE];
           int size = 0;
           try {
               url = new URL(destUrl);
               httpUrl = (HttpURLConnection) url.openConnection();
               httpUrl.connect();
               bis = new BufferedInputStream(httpUrl.getInputStream());
               String fileName = destUrl.substring(destUrl.lastIndexOf("/"));
               System.out.println(fileName);
               fos = new FileOutputStream("e:\haha.jpg");
               while ((size = bis.read(buf)) != -1) {
                  fos.write(buf, 0, size);
               }
               fos.flush();
           } catch (IOException e) {
               e.printStackTrace();
           } catch (ClassCastException e) {
               e.printStackTrace();
           } finally {
               try {
                  fos.close();
                  bis.close();
                  httpUrl.disconnect();
               } catch (IOException e) {
                  e.printStackTrace();
               } catch (NullPointerException e) {
                  e.printStackTrace();
               }
           }
        }
  • 相关阅读:
    组合继承
    包装明星——封装
    多种添加公用方法的方式
    专有扩展
    插入标记
    mac 命令操作
    php(apache)切换版本
    SqlServer索引+约束篇章
    sqlserver 常用语法
    C# 通用数据访问类
  • 原文地址:https://www.cnblogs.com/gavinYang/p/11196425.html
Copyright © 2011-2022 走看看