当前位置:   article > 正文

多线程下载时HTTP response code: 416 解决方案_下载unity response status was 416

下载unity response status was 416

今天用java写的多线程下载报错,找了很久,发现是第一次请求服务器时响应码正常,开启多线程部分的响应码为416,响应码对应的意思可以去这里看看点击打开链接。主要意思就是public void setRequestProperty (String field, String newValue)出现了错误,设置的下载字节范围和访问文件大小有出入,要么就是上面确认各个线程的下载范围出现问题,要么就是该语句没用对。将之前的每个线程下载字节范围都打印出来发现没问题。后来查看setRequestProperty相关资料发现是setRequestProperty("Range", "bytes:" + threadStart + "-" + threadEnd)这里出错了,bytes后面应该是=号而不是:

附上没有完成断点下载的多线程下载代码.

(我用的是某马的74期的视频学习的,而他的代码用的是:却可以运行,有点郁闷,,还有他的getFileName()方法中获取/的索引后没有+1就截取文件名得到的是/feiq.exe, 那也是错误的,后面运行时会报错拒绝访问)


  1. package com.lyle.study;
  2. import java.io.File;
  3. import java.io.InputStream;
  4. import java.io.RandomAccessFile;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. public class UpDownload1 {
  8. public static String path = "http://192.168.56.1:8080/itheima74/feiq.exe";
  9. public static int threadCount = 3;
  10. public static void main(String[] args) {
  11. try {
  12. URL url = new URL(path);
  13. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  14. connection.setRequestMethod("GET");
  15. connection.setConnectTimeout(1000 * 10);
  16. if (connection.getResponseCode() == 200) {
  17. long length = connection.getContentLength();
  18. RandomAccessFile randomAccessFile = new RandomAccessFile(new File(getFileName()),
  19. "rw");
  20. randomAccessFile.setLength(length);
  21. randomAccessFile.close();
  22. long threadSize = length / threadCount;
  23. for (int i = 0; i < threadCount; i++) {
  24. int threadId = i;
  25. long threadStart = threadId * threadSize;
  26. long threadEnd = (threadId == threadCount - 1 ? length - 1 : (threadId + 1)
  27. * threadSize - 1);
  28. new DoThreadOpen(threadStart, threadEnd).start();
  29. }
  30. }
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. public static class DoThreadOpen extends Thread {
  36. private long threadStart;
  37. private long threadEnd;
  38. public DoThreadOpen(long threadStart, long threadEnd) {
  39. this.threadEnd = threadEnd;
  40. this.threadStart = threadStart;
  41. }
  42. @Override
  43. public void run() {
  44. try {
  45. URL url = new URL(path);
  46. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  47. connection.setRequestMethod("GET");
  48. connection.setConnectTimeout(1000 * 10);
  49. connection.setRequestProperty("Range", "bytes=" + threadStart + "-" + threadEnd);
  50. int code = connection.getResponseCode();
  51. if (code == 206) {
  52. InputStream inputStream = connection.getInputStream();
  53. RandomAccessFile randomAccessFile = new RandomAccessFile(
  54. new File(getFileName()), "rw");
  55. randomAccessFile.seek(threadStart);
  56. byte[] buffer = new byte[1024];
  57. int len;
  58. while ((len = inputStream.read(buffer)) != -1) {
  59. randomAccessFile.write(buffer, 0, len);
  60. }
  61. inputStream.close();
  62. randomAccessFile.close();
  63. }
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. }
  69. private static String getFileName() {
  70. return path.substring(path.lastIndexOf("/") + 1);
  71. }
  72. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/259685
推荐阅读
相关标签
  

闽ICP备14008679号