实现HTTP文件流下载限速与MD5校验的Java方案

在Java中,如果你需要通过HTTP请求获取文件流,并对下载过程进行带宽限速,同时计算文件的MD5值,你可以按照以下步骤进行:

图片[1]_实现HTTP文件流下载限速与MD5校验的Java方案_知途无界
  1. 发送HTTP请求并获取文件流
    使用HttpURLConnection或更高级的库如Apache HttpClient来发送HTTP请求,并获取响应中的文件流。
  2. 实现带宽限速
    你可以通过控制从输入流读取数据的速率来实现带宽限速。这通常涉及到在读取数据时引入延迟或使用一个速率控制器。
  3. 计算MD5值
    在读取文件流的同时,使用MessageDigest类来计算数据的MD5值。

下面是一个使用HttpURLConnection的简单示例,它演示了如何获取文件流、限速(通过简单的sleep模拟)和计算MD5值:

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.CRC32;
public class DownloadWithLimitAndMD5 {
// 模拟带宽限速的读取方法
private static byte[] readWithLimit(InputStream in, int limit) throws IOException, InterruptedException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
long totalBytesRead = 0;
long startTime = System.currentTimeMillis();
while ((bytesRead = in.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
// 简单的限速逻辑:根据已读字节和时间差来计算是否需要限速
long currentTime = System.currentTimeMillis();
if ((currentTime - startTime) * limit / 1000 < totalBytesRead) {
Thread.sleep(100); // 如果超过限速,则休眠一段时间
}
// 重置开始时间以计算下一个限速周期
if (totalBytesRead % (limit / 10) == 0) { // 每读取limit/10的字节,重置一次时间(这个值可以根据需要调整)
startTime = System.currentTimeMillis();
}
}
return baos.toByteArray();
}
// 计算MD5值的方法
private static String calculateMD5(byte[] data) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(data);
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
public static void main(String[] args) {
String fileUrl = "http://example.com/file.zip"; // 替换为实际的文件URL
int downloadLimitKbps = 100; // 设置下载限速,单位:Kbps(千比特每秒)
try {
URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
try (InputStream in = conn.getInputStream()) {
byte[] fileData = readWithLimit(in, downloadLimitKbps * 1024 / 8); // 转换为字节每秒
String md5 = calculateMD5(fileData);
System.out.println("File MD5: " + md5);
// 这里你可以将fileData保存到文件或进行其他处理
}
} catch (IOException | NoSuchAlgorithmException | InterruptedException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.CRC32;

public class DownloadWithLimitAndMD5 {

    // 模拟带宽限速的读取方法
    private static byte[] readWithLimit(InputStream in, int limit) throws IOException, InterruptedException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        long totalBytesRead = 0;
        long startTime = System.currentTimeMillis();

        while ((bytesRead = in.read(buffer)) != -1) {
            baos.write(buffer, 0, bytesRead);
            totalBytesRead += bytesRead;

            // 简单的限速逻辑:根据已读字节和时间差来计算是否需要限速
            long currentTime = System.currentTimeMillis();
            if ((currentTime - startTime) * limit / 1000 < totalBytesRead) {
                Thread.sleep(100); // 如果超过限速,则休眠一段时间
            }

            // 重置开始时间以计算下一个限速周期
            if (totalBytesRead % (limit / 10) == 0) { // 每读取limit/10的字节,重置一次时间(这个值可以根据需要调整)
                startTime = System.currentTimeMillis();
            }
        }

        return baos.toByteArray();
    }

    // 计算MD5值的方法
    private static String calculateMD5(byte[] data) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(data);
        byte[] digest = md.digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String fileUrl = "http://example.com/file.zip"; // 替换为实际的文件URL
        int downloadLimitKbps = 100; // 设置下载限速,单位:Kbps(千比特每秒)

        try {
            URL url = new URL(fileUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");

            try (InputStream in = conn.getInputStream()) {
                byte[] fileData = readWithLimit(in, downloadLimitKbps * 1024 / 8); // 转换为字节每秒
                String md5 = calculateMD5(fileData);

                System.out.println("File MD5: " + md5);
                // 这里你可以将fileData保存到文件或进行其他处理
            }
        } catch (IOException | NoSuchAlgorithmException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.zip.CRC32; public class DownloadWithLimitAndMD5 { // 模拟带宽限速的读取方法 private static byte[] readWithLimit(InputStream in, int limit) throws IOException, InterruptedException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int bytesRead; long totalBytesRead = 0; long startTime = System.currentTimeMillis(); while ((bytesRead = in.read(buffer)) != -1) { baos.write(buffer, 0, bytesRead); totalBytesRead += bytesRead; // 简单的限速逻辑:根据已读字节和时间差来计算是否需要限速 long currentTime = System.currentTimeMillis(); if ((currentTime - startTime) * limit / 1000 < totalBytesRead) { Thread.sleep(100); // 如果超过限速,则休眠一段时间 } // 重置开始时间以计算下一个限速周期 if (totalBytesRead % (limit / 10) == 0) { // 每读取limit/10的字节,重置一次时间(这个值可以根据需要调整) startTime = System.currentTimeMillis(); } } return baos.toByteArray(); } // 计算MD5值的方法 private static String calculateMD5(byte[] data) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(data); byte[] digest = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b)); } return sb.toString(); } public static void main(String[] args) { String fileUrl = "http://example.com/file.zip"; // 替换为实际的文件URL int downloadLimitKbps = 100; // 设置下载限速,单位:Kbps(千比特每秒) try { URL url = new URL(fileUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); try (InputStream in = conn.getInputStream()) { byte[] fileData = readWithLimit(in, downloadLimitKbps * 1024 / 8); // 转换为字节每秒 String md5 = calculateMD5(fileData); System.out.println("File MD5: " + md5); // 这里你可以将fileData保存到文件或进行其他处理 } } catch (IOException | NoSuchAlgorithmException | InterruptedException e) { e.printStackTrace(); } } }

注意事项

  1. 限速逻辑:上面的限速逻辑是非常简单的,并且不是非常精确。在实际应用中,你可能需要更复杂的逻辑来更精确地控制下载速率,比如使用ScheduledExecutorService来定期检查并调整读取速率。
  2. 异常处理:在实际应用中,你应该添加更多的异常处理逻辑来确保程序的健壮性。
  3. 资源释放:确保所有资源(如输入流、输出流等)在使用完毕后都被正确关闭。
  4. MD5计算:上面的示例中计算的是整个文件的MD5值。如果你只需要文件的某个部分的MD5值,你可以调整读取和计算的逻辑。
  5. 性能考虑:对于大文件,上述代码可能会占用大量内存。在实际应用中,你可能需要考虑使用分块读取和计算MD5的方法,以减少内存使用。
© 版权声明
THE END
喜欢就点个赞,支持一下吧!
点赞84 分享
Not all of us can offord to be romantic.
并不是我们所有的人都会拥有浪漫
评论 抢沙发
头像
欢迎您留下评论!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容