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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM