zoukankan      html  css  js  c++  java
  • 使用org.jsoup.Jsoup下载网络中的图片

    package com.enation.newtest;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import org.jsoup.Connection;
    import org.jsoup.Jsoup;
    
    public class JsoupTest {
    
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            String imageSrc = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";
            Connection.Response response = Jsoup.connect(imageSrc).execute();
            byte[] img = response.bodyAsBytes();
            System.out.println(img.length);
            savaImage(img, "D:\360Downloads", "test.png");    
        }
    
        public static void savaImage(byte[] img,String filePath,String fileName) {
            BufferedOutputStream bos = null;
            FileOutputStream fos = null;
            File file = null;
            File dir = new File(filePath);
            try {
                //判断文件目录是否存在
                if(!dir.exists() && dir.isDirectory()){
                    dir.mkdir();
                }
                file = new File(filePath+"\"+fileName);
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos);
                bos.write(img);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(bos!=null){
                    try {
                        bos.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();
                    }
                }
            }
            
            
            
        }
    }

    很方便的下载图片,学习一下

  • 相关阅读:
    JMeter 分布式调度压测部署
    jmeter 命令行运行与生成报告
    Apache启动报错:Invalid command 'AuthType', perhaps misspelled or defined by a module not included in it
    【TestNG】TestNG使用教程详解
    这才是Tomcat内存配置的正确姿势
    Tomcat GC参数详解
    MANIFEST.MF文件详解
    CMD命令打包文件夹成jar
    常用MIME类型
    表单序列化
  • 原文地址:https://www.cnblogs.com/jiafuwei/p/6080776.html
Copyright © 2011-2022 走看看