zoukankan      html  css  js  c++  java
  • Java 读取网络资源文件 获取文件大小 MD5校验值

    Java 读取网络资源文件 获取文件大小 MD5校验值

    封装一个文件操作工具类:

    package c;
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    /**
     * @author Jayvee
     * @Description: todo 文件操作
     */
    
    public class FileUtils {
    
        /**
         * @author Jayvee
         * @Description: todo 获取网络文件的大小
         */
        public static int getFileLength(String url1) throws IOException {
            int length = 0;
            URL url;
            try {
                url = new URL(url1);
                HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
                //根据响应获取文件大小
                length = urlcon.getContentLength();
                urlcon.disconnect();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return length;
        }
    
    
        /**
         * 从输入流中获取字节数组
         * @author Jayvee
         * @param inputStream
         * @return
         * @throws IOException
         */
        public static byte[] readInputStream(InputStream inputStream) throws IOException {
            byte[] buffer = new byte[1024];
            int len = 0;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while ((len = inputStream.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            bos.close();
            return bos.toByteArray();
        }
    
    
    	/**
         * @author Jayvee
         * @Description: todo 
         */
        public static byte[] downLoadFromUrl(String urlStr) throws IOException {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            InputStream inputStream = conn.getInputStream();
            //获取自己数组
            byte[] getData = readInputStream(inputStream);
            return getData;
        }
    
    
    	/**
         * @author Jayvee
         * @Description: todo 
         */
        public static byte[] readFromByteFile(String pathname) throws IOException {
            File filename = new File(pathname);
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
            byte[] temp = new byte[1024];
            int size = 0;
            while ((size = in.read(temp)) != -1) {
                out.write(temp, 0, size);
            }
            in.close();
            byte[] content = out.toByteArray();
            return content;
        }
    }
    

    假设获取网络图片 http://pic.962.net/up/2018-1/2018191570320420.jpg 的大小和内容。
    在这里插入图片描述
    Java 代码:

            try {
                int fileSize = FileUtils.getFileLength("http://pic.962.net/up/2018-1/2018191570320420.jpg");  // 获取文件大小
                byte[] file = FileUtils.downLoadFromUrl("http://pic.962.net/up/2018-1/2018191570320420.jpg");  // 获取文件内容
                System.out.println("文件大小:" + fileSize + " 字节");
                System.out.println("文件内容:" + file);
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    执行结果:

    文件大小:4979 字节
    文件内容:[B@7dc5e7b4
    

    在这里插入图片描述

    获取网络文件的md5校验值:

            try {
                byte[] file = FileUtils.downLoadFromUrl("http://pic.962.net/up/2018-1/2018191570320420.jpg");  // 获取文件内容
                System.out.println(DigestUtils.md5DigestAsHex(file));  // 文件内容md5校验
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    执行结果:

    cc2fdb7b2366a086f30e5af3c63b230c
    
  • 相关阅读:
    结合中断上下文切换和进程上下文切换分析Linux内核的一般执行过程
    深入理解Linux系统调用
    基于mykernel 2.0编写一个操作系统内核
    如何评测软件工程知识技能水平?
    创新产品的需求分析:未来的图书会是什么样子?
    案例分析:设计模式与代码的结构特性
    业务领域建模Domain Modeling
    工程实践用例建模
    分析一套源代码的代码规范和风格并讨论如何改进优化代码
    面向对象第三单元作业反思与总结
  • 原文地址:https://www.cnblogs.com/wjw1014/p/11677684.html
Copyright © 2011-2022 走看看