zoukankan      html  css  js  c++  java
  • java 根据URL下载文件

     /**
         * 根据地址获得数据的输入流
         *
         * @param strUrl 网络连接地址
         * @return url的输入流
         */
        public static InputStream getInputStreamByUrl(String strUrl) {
            HttpURLConnection conn = null;
            try {
                URL url = new URL(strUrl);
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(20 * 1000);
                final ByteArrayOutputStream output = new ByteArrayOutputStream();
                IOUtils.copy(conn.getInputStream(), output);
                return new ByteArrayInputStream(output.toByteArray());
            } catch (Exception e) {
                logger.error(e + "");
            } finally {
                try {
                    if (conn != null) {
                        conn.disconnect();
                    }
                } catch (Exception e) {
                    logger.error(e + "");
                }
            }
            return null;
        }
    
    喜欢出发、喜欢离开、喜欢不一样的事物。——May
  • 相关阅读:
    C语言中标识符的作用域、命名空间、链接属性、生命周期、存储类型
    循环练习
    ArrayList集合

    方法
    表单标签
    HTML基础
    二维数组
    一维数组
    switch选择结构
  • 原文地址:https://www.cnblogs.com/I-Say/p/14794254.html
Copyright © 2011-2022 走看看