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();
                }
            }
        }
    }

Java_下载图片

时间: 2024-09-15 00:53:30

Java_下载图片的相关文章

我的第一个python爬虫程序(从百度贴吧自动下载图片)

这个学期开设了编译原理和形式语言与自动机,里面都有介绍过正则表达式,今天自己学了学用python正则表达式写爬虫 一.网络爬虫的定义 网络爬虫,即Web Spider,是一个很形象的名字. 把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛. 网络蜘蛛是通过网页的链接地址来寻找网页的. 从网站某一个页面(通常是首页)开始,读取网页的内容,找到在网页中的其它链接地址, 然后通过这些链接地址寻找下一个网页,这样一直循环下去,直到把这个网站所有的网页都抓取完为止. 如果把整个互联网当成

一个失误导致微信下载图片接口Token失效

公司的应用调了一个微信上传下载图片的接口,本来在线上跑的好好的,什么问题没有,但是这两天总是不定时的出现下载下来的图片损坏,拿着Token和serverid去接口测试网页验证,返回的是Token失效了,返回信息:"errcode": 40001, "errmsg": "invalid credential, access_token is invalid or not latest hint: [17ZG50664ent3].我是把Token和ticket

利用Node 搭配uglify-js压缩js文件,批量下载图片到本地

Node的便民技巧-- 压缩代码 下载图片 压缩代码 相信很多前端的同学都会在上线前压缩JS代码,现在的Gulp Webpack Grunt......都能轻松实现.但问题来了,这些都不会,难道就要面对几十个JS文件一遍遍来回“复制-压缩-创建-粘贴”,这样太不人性化了. 于是可以借助Node + uglify-js 轻松实现.(前提你会点node操作) 1.首先看一下目录: |--uglifyJS |--js |--test1.js |--test2.js |--uglify.js   //这

【iOS开发之旅】AFNetworking与SDWebImage下载图片

AFNetworking与SDWebImage下载图片 // // ViewController.m // AFNetworkingDemo // // Created by ChenQianPing on 16/1/31. // Copyright © 2016年 chenqp. All rights reserved. // #import "ViewController.h" #import "AFNetworking.h" #import "UII

安卓AsyncTask异步下载图片

package com.example.day7_practise; import java.io.IOException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.app.ProgressDialog;import android.graphics.Bitmap;imp

UI13-异步下载图片

1 使用封装好的工具类,进行加载数据,存在数组中,加载好后重新刷新表内容.异步下载图片. 2.在model类中,使用方法对图片的网址进行解析,讲自己的image的地址,转化为image图片/ 在cell加载的时候调用. 3.在加载cell中图片的时候,如果图片为空,且不是正在加载中,调用model的方法进行对m的image赋值,此时注册观察者,当image的值发生改变时 触发观察者方法,此时已经有值可以对cell进行赋值:::如果图片有值,直接对cell进行赋值. 4.触发的观察者方法,找到新改

Python 批量下载图片示例

使用Python find函数和urllib下载图片. #!/usr/bin/env python import time import urllib i = 0 url = ['']*10 name = ['']*10 con = urllib.urlopen('http://www.ithome.com/html/bizhi/164396.htm').read() src = con.find(r'/newsuploadfiles') end = con.find(r'.jpg',src)

Android下载图片显示问题

============问题描述============ 我用下面的代码从服务器端下载了图片,运行在新开的线程中 System.out.println("before bm = " + bm); String imageUrl1 = "http://10.0.2.2:8080/BBStuServlet/test01_upload_1.jpg"; bm = NetTool.downlodBitmap(imageUrl1); map2.put("image&q

java web 从服务器上下载图片资料

package com.Action; import java.io.File; 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 HttpUtils {