zoukankan      html  css  js  c++  java
  • Java_下载图片

    1、定义图片地址

    private static String URL_PATH = "http://192.168.100.244:8080/HelloWorld/ZXC.gif";

    2、获得服务器端的数据,以InputStream形式返回

    *
     * 获得服务器端的数据,以InputStream形式返回
     * @return
     */
        public static InputStream getInputStream()
        {
            InputStream inputStream = null;
            HttpURLConnection httpURLConnection = null;
            try
            {
                URL url = new URL(URL_PATH);
                if (url != null)
                {
                    httpURLConnection = (HttpURLConnection) url.openConnection();// 打开链接
                    // 设置连接网络的超时时间
                    httpURLConnection.setConnectTimeout(3000);
                    httpURLConnection.setDoInput(true);
                    //
                    httpURLConnection.setRequestMethod("GET");
                    int resposeCode = httpURLConnection.getResponseCode();
                    if (resposeCode == 200)// 如果请求成功
                    {
                        // 从服务器中获得一个输入流
                        inputStream = httpURLConnection.getInputStream();
                    }
                }
            }
            catch (MalformedURLException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e)
            {
                // TODO: handle exception
                e.printStackTrace();
            }
            return inputStream;
        }

    3、保存代码

        public static void saveImageToDisk()
        {
            InputStream inputStream = getInputStream();
            byte[] data = new byte[1024];
            int len = 0;
            FileOutputStream fileOutputStream = null;
            try
            {
                fileOutputStream = new FileOutputStream("C:\test.gif");
                while ((len = inputStream.read(data)) != -1)
                {
                    fileOutputStream.write(data, 0, len);
                }
            }
            catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally
            {
                if (inputStream != null)
                {
                    try
                    {
                        inputStream.close();
                    }
                    catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if (fileOutputStream != null)
                {
                    try
                    {
                        fileOutputStream.close();
                    }
                    catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    Android 成长之路
  • 相关阅读:
    Java并发之Thread类的使用
    剑指Offer
    总结下2017之前的几年
    解决一个特定的负载均衡下定时任务执行多次的问题
    《MYSQL》----字符串的复杂函数,检索的七-天-排-重
    科学计数法的转换
    小伙伴自言自语发给我的聊天记录,一句都看不懂
    记录下一个让我调了一天的失误
    记录一个从没见过的bug
    吐槽下
  • 原文地址:https://www.cnblogs.com/liende/p/3885089.html
Copyright © 2011-2022 走看看