Java之图片下载

package com.example.java_algorithms.javaBasic.img;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * 下载图片:字节流写入,三种方法
 */
public class picture {
   private static String filePath="https://img3.doubanio.com/view/photo/l/public/p2647478420.webp";
    public static void main(String[] args) throws Exception {
//        testDownload01();
//        testDownload02();
        testDownload03();
    }

    //下载-URLConnection
    public static void testDownload01 () throws Exception {
        URL url = new URL(filePath);//定义一个url对象
        URLConnection urlConnection=url.openConnection();
        InputStream inputStream=urlConnection.getInputStream();
        OutputStream outputStream=new FileOutputStream("Copy04.jpg");
        int temp=0;
        //循环
        while ((temp=inputStream.read())!=-1){
            outputStream.write(temp);
        }
        outputStream.close();
        inputStream.close();
    }

    //下载-HttpURLConnection
    public static void testDownload02() throws Exception {
        URL url = new URL(filePath);//定义一个url对象
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();//打开链接
        conn.setRequestMethod("GET");//设置请求方式为get请求
        conn.setConnectTimeout(10 * 100);//设置请求超时为10秒
        InputStream is = conn.getInputStream();//通过数据流获取图片
        byte[] data = readInputStream(is);//得到图片数据流二进制数据
        File imageFile = new File("Copy03.jpg");//创建文件保存图片

        FileOutputStream outputStream = new FileOutputStream(imageFile);//创建输出流
        outputStream.write(data);//写入数据
        outputStream.close();//关闭数据流
    }
    //用的是apache的包commons-io
    public static void testDownload03() throws Exception {
        System.out.println(filePath);
        //拷贝一个网页文件到一个文件
        try {
            FileUtils.copyURLToFile(new URL(filePath),new File("test_02_1.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常,downloader方法异常!");
        }
    }
public static byte[] readInputStream(InputStream inSream) throws Exception{
        ByteArrayOutputStream outStream=new ByteArrayOutputStream();
    byte[] buffer=new byte[6024];//创建一个buffer字符串
    int len;
    //使用一个输入流从buffer里把数据读取出来
    while ((len =inSream.read(buffer)) != -1) {
        //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
        outStream.write(buffer, 0, len);
    }
    //关闭输入流
    inSream.close();
    //把outStream里的数据写入内存
    return outStream.toByteArray();
}
}





  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锐行织梦者

谢谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值