zoukankan      html  css  js  c++  java
  • http协议get方式获取图片

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class GetImage {
        private static String PATH = "http://192.168.1.119:8080/ok/point_one.png";
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            savaDisk();
        }
        //存到本地
        public static void savaDisk(){
            InputStream in = getInputStream();
            FileOutputStream fos = null;
            byte[] data = new byte[1024];
            int len = 0;
            try {
                fos = new FileOutputStream("D:\test.png");
                while((len = in.read(data))!=-1){
                    fos.write(data, 0, len);
                }
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(in!=null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(fos!=null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                }
            }
        }
        
        public static InputStream getInputStream() {
            InputStream in = null;
            HttpURLConnection httpconn = null;
            try {
                // 声明url
                URL url = new URL(PATH);
                if (url != null) {
                    // 建立客户端与服务器端链接
                    httpconn = (HttpURLConnection) url.openConnection();
                    // 设置网路超时时间,如果超过这个时间自动断开
                    httpconn.setConnectTimeout(3000);
                    // 打开输入流
                    httpconn.setDoInput(true);
                    // 设置请求方式
                    httpconn.setRequestMethod("GET");
                    // 获取响应的状态码
                    int responseCode = httpconn.getResponseCode();
                    // 判断是否是ok
                    if (responseCode == 200) {
                        // 从服务器获得一个输入流
                        in = httpconn.getInputStream();
    
                    }
    
                }
    
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return in;
        }
    
    }

    android4.0以下可以用

    package com.example.getimg;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    
    import java.net.URL;
    
    public class MyActivity extends Activity {
        /**
         * Called when the activity is first created.
         */
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String path = "http://192.168.1.119:8080/ok/point_one.png";
                    //uri
                    try {
                        URL url = new URL(path);
                        //获得链接
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        //设置参数
                        conn.setRequestMethod("GET");
                        conn.setConnectTimeout(5000);
                        conn.setReadTimeout(5000);
                        //正式链接
                        conn.connect();
                        if(conn.getResponseCode()==200){
                            InputStream is = conn.getInputStream();
                            //把InputStream通过bitmap转化成图片
                            Bitmap bitmap = BitmapFactory.decodeStream(is);
                           ImageView img = (ImageView) findViewById(R.id.img);
                            img.setImageBitmap(bitmap);
    
                        }else{
                            Toast.makeText(MyActivity.this,"没有响应",Toast.LENGTH_SHORT).show();
                        }
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
  • 相关阅读:
    js 前端词典对象的属性和值读取
    Vue 标签Style 动态三元判断绑定
    Java搭建MapReduce输出到DB具体步骤之多输入
    Java搭建MapReduce输出到DB具体步骤
    Java搭建MapReduce完成二次排序步骤
    新生迎新
    Java搭建MapReduce具体步骤
    Java访问HDFS集群Java语句以及 Linux操作HDFS集群常用命令
    Linux操作HDFS常用命令
    Linux操作HDFS集群常用命令
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/4906751.html
Copyright © 2011-2022 走看看