使用java下载http形式的图片

本文介绍了如何使用Java自定义DownUtil类下载HTTP形式的图片,提供了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用于下载http形式的图片

以下是自定义的DownUtil类:

import java.io.*;
import java.net.*;

public class DownUtil {

    private String path;
    private String targetFile;
    private int threadNum;
    private DownThread[] threads;
    private int fileSize;
    private class DownThread extends Thread{
        private int startPos;
        private int currentPartSize;
        private RandomAccessFile currentPart;
        public int length;
        public DownThread(int startPos, int currentPartSize, RandomAccessFile currentPart){
            this.startPos = startPos;
            this.currentPartSize = currentPartSize;
            this.currentPart = currentPart;
        }
        public void run(){
            try{
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5 * 1000);
                conn.setRequestMethod("GET");
                conn.setRequestProperty(
                    "Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, "
                    + "application/x-shockwave-flash, application/xaml+xml, "
                    + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "
                    + "application/x-ms-application, application/vnd.ms-excel, "
                    + "application/vnd.ms-powerpoint, application/msword, */*");
                conn.setRequestProperty("Accept-Language", "zh-CN");
                conn.setRequestProperty("Charset", "UTF-8");
                InputStream inStream = conn.getInputStream();
                inStream.skip(this.startPos);
                byte[] buffer = new byte[1024];
                int hasRead = 0;
                // Read network data and write to a local file
                while(length < currentPartSize && (hasRead = inStream.read(buffer)) !=  -1){
                    currentPart.write(buffer, 0, hasRead);
                    length += hasRead;
                }
                currentPart.close();
                inStream.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    public DownUtil(String path, String targetFile, int threadNum) {
        this.path = path;
        this.threadNum = threadNum;
        this.threads = new DownThread[threadNum];
        this.targetFile = targetFile;
    }

    public void download() throws Exception{
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod("GET");
        conn.setRequestProperty(
                "Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, "
                + "application/x-shockwave-flash, application/xaml+xml, "
                + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "
                + "application/x-ms-application, application/vnd.ms-excel, "
                + "application/vnd.ms-powerpoint, application/msword, */*");
        conn.setRequestProperty("Accept-Language", "zh-CN");
        conn.setRequestProperty("Charset", "UTF-8");
        conn.setRequestProperty("Connection", "Keep-Alive");

        fileSize = conn.getContentLength();
        conn.disconnect();
        int currentPartSize = fileSize / threadNum + 1;
        RandomAccessFile file = new RandomAccessFile(targetFile, "rw");
        file.setLength(fileSize);
        file.close();
        // Multi-threaded download
        for(int i = 0; i < threadNum; ++i){
            int startPos = i * currentPartSize;
            RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");
            currentPart.seek(startPos);
            threads[i] = new DownThread(startPos, currentPartSize, currentPart);
            threads[i].start();
        }
    }
    // Gets the percentage of completion of the download
    public double getCompleteRate(){
        int sumSize = 0;
        for(int i = 0; i < threadNum; ++i){
            sumSize += threads[i].length;
        }
        return sumSize * 1.0 / fileSize;
    }
}

这是主程序:

public class MultiThreadedDownload {

    public static void main(String[] args) throws Exception{
        DownUtil downUtil = new DownUtil("http://imgsrc.baidu.com/baike/pic" +
                "/item/8644ebf81a4c510f64b65cfa6559252dd52aa5e4.jpg", "liuyan.jpg", 4);
        downUtil.download();
        new Thread(()->{
            while(downUtil.getCompleteRate() < 1){
                System.out.println("已完成:" + downUtil.getCompleteRate());
                try{
                    Thread.sleep(1000);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
}


谢谢浏览!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值