断点下载器还在实现中。。。。。。

//////////////////////////////////界面///////////////////////////////////////////

package com.company.www.me.net;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class DownLoadTool extends JFrame {
 public static void main(String[] args) {
  final DownLoadTool tool = new DownLoadTool();
  try {
   UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
   SwingUtilities.updateComponentTreeUI(tool.getContentPane());
  } catch (Exception e) {
   e.printStackTrace();
  }
  tool.setResizable(false);
  tool.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  tool.setTitle("DownLoadTool");
  int x = Toolkit.getDefaultToolkit().getScreenSize().width/2-180;
  int y = Toolkit.getDefaultToolkit().getScreenSize().height/2-90;
  tool.setLocation(x,y);
  tool.setSize(430, 280);
  tool.setLayout(new GridBagLayout());
  JLabel label = new JLabel("URL: ");
  GridBagConstraints labelCtrl = new GridBagConstraints();
  labelCtrl.gridx = 0;
  labelCtrl.gridy = 0;
  labelCtrl.gridheight = 1;
  labelCtrl.gridwidth = 1;
  labelCtrl.anchor = GridBagConstraints.WEST;
  JScrollPane scrollPane = new JScrollPane();
  final JTextArea textArea = new JTextArea();
  scrollPane.setViewportView(textArea);
  scrollPane.setPreferredSize(new Dimension(320,33));
  GridBagConstraints scrollPaneCtrl = new GridBagConstraints();
  scrollPaneCtrl.gridx = 1;
  scrollPaneCtrl.gridy = 0;
  scrollPaneCtrl.gridheight = 1;
  scrollPaneCtrl.gridwidth = 2;
  scrollPaneCtrl.anchor = GridBagConstraints.EAST;
  scrollPaneCtrl.insets = new Insets(8,0,8,0);
  
  JLabel label2 = new JLabel("To: ");
  GridBagConstraints label2Ctrl = new GridBagConstraints();
  label2Ctrl.gridx = 0;
  label2Ctrl.gridy = 1;
  label2Ctrl.gridheight = 1;
  label2Ctrl.gridwidth = 1;
  label2Ctrl.anchor = GridBagConstraints.WEST;
  final JTextField textField = new JTextField(22);
  textField.setText("C:/Documents and Settings/wangbijian/デスクトップ/WebTest/DownLoad");
  GridBagConstraints textFieldCtrl = new GridBagConstraints();
  textFieldCtrl.gridx = 1;
  textFieldCtrl.gridy = 1;
  textFieldCtrl.gridheight = 1;
  textFieldCtrl.gridwidth = 1;
  JButton Tobtn = new JButton("Choose Path...");
  Tobtn.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent actionevent) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int returnVal = fileChooser.showOpenDialog(tool);
     if(returnVal == JFileChooser.APPROVE_OPTION) {
      textField.setText(fileChooser.getSelectedFile().toString());
        }
  }
   });
  GridBagConstraints TobtnCtrl = new GridBagConstraints();
  TobtnCtrl.gridx = 2;
  TobtnCtrl.gridy = 1;
  TobtnCtrl.gridheight = 1;
  TobtnCtrl.gridwidth = 1;
  TobtnCtrl.anchor = GridBagConstraints.EAST;
  
  JLabel label3 = new JLabel("Thread Count: ");
  GridBagConstraints label3Ctrl = new GridBagConstraints();
  label3Ctrl.gridx = 0;
  label3Ctrl.gridy = 2;
  label3Ctrl.gridheight = 1;
  label3Ctrl.gridwidth = 1;
  label3Ctrl.insets = new Insets(8,0,0,0);
  label3Ctrl.anchor = GridBagConstraints.WEST;
  
  final JTextField threadField = new JTextField(28);
  GridBagConstraints threadFieldCtrl = new GridBagConstraints();
  threadFieldCtrl.gridx = 1;
  threadFieldCtrl.gridy = 2;
  threadFieldCtrl.gridheight = 1;
  threadFieldCtrl.gridwidth = 2;
  threadFieldCtrl.insets = new Insets(8,0,0,0);
  threadFieldCtrl.anchor = GridBagConstraints.WEST;
  
  final JScrollPane progressPane = new JScrollPane();
  progressPane.setPreferredSize(new Dimension(380,80));
  final JPanel panel = new JPanel();
  progressPane.setViewportView(panel);
  GridBagConstraints progressPaneCtrl = new GridBagConstraints();
  progressPaneCtrl.gridx = 0;
  progressPaneCtrl.gridy = 4;
  progressPaneCtrl.gridheight = 1;
  progressPaneCtrl.gridwidth = 3;
  progressPaneCtrl.anchor = GridBagConstraints.CENTER;
  
  
  JButton btn = new JButton("DownLoad");
  GridBagConstraints btnCtrl = new GridBagConstraints();
  btnCtrl.gridx = 0;
  btnCtrl.gridy = 3;
  btnCtrl.gridheight = 1;
  btnCtrl.gridwidth = 3;
  btnCtrl.insets = new Insets(18,18,18,18);
  btnCtrl.anchor = GridBagConstraints.CENTER;
  btn.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent actionevent) {
    final JLabel progressLabel = new JLabel();
    final JProgressBar progressBar = new JProgressBar();
    progressBar.setVisible(false);
    panel.add(progressLabel);
    panel.add(progressBar);
    
    final String DownLoadPath = textArea.getText();
    final String Topath = textField.getText();
    int threadCount1 = 1;
    if (threadField.getText()!=null&&!threadField.getText().trim().equals(""))
    {
     threadCount1 = Integer.parseInt(threadField.getText());
    }
    final int threadCount = threadCount1;
    if (DownLoadPath==null||"".equals(DownLoadPath)) {
     JOptionPane.showMessageDialog(tool, "Please input URL!");
     return;
    }
    else if(Topath==null||"".equals(Topath)) {
     JOptionPane.showMessageDialog(tool, "Please select save path!");
     return;
    }
    else if(Topath!=null&&DownLoadPath!=null||"".equals(Topath.trim())&&"".equals(DownLoadPath.trim())) {
     new Thread(){
      public void run() {
       progressBar.setVisible(true);
       progressBar.setValue(0);
       new DownLoadToolClass(progressBar,DownLoadPath,Topath,threadCount).DownLoadFile(progressLabel);
      }
     }.start();
    }
   }
  });
  tool.add(label,labelCtrl);
  tool.add(scrollPane,scrollPaneCtrl);
  tool.add(label2,label2Ctrl);
  tool.add(textField,textFieldCtrl);
  tool.add(Tobtn,TobtnCtrl);
  tool.add(label3,label3Ctrl);
  tool.add(threadField,threadFieldCtrl);
  tool.add(progressPane,progressPaneCtrl);
  tool.add(btn,btnCtrl);
  tool.setVisible(true);
 }
}

//////////////////////////////关键下载类//////////////////////////////////////

package com.company.www.me.net;

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JLabel;
import javax.swing.JProgressBar;

public class DownLoadToolClass {
 String DownLoadPath;
 String Topath;
 int threadCount;
 JProgressBar progressBar;
 public DownLoadToolClass(JProgressBar progressBar,String downLoadPath, String topath,int threadCount) {
  this.progressBar = progressBar;
  this.DownLoadPath = downLoadPath;
  this.Topath = topath;
  this.threadCount = threadCount;
 }
 public void DownLoadFile(JLabel progressLabel){
  try {
   int size;
   int blockSize;
   int EndBlockSize = -1;
   URL url = new URL(DownLoadPath);
   URLConnection conn = url.openConnection();
   size = conn.getContentLength();
   File file = new File(Topath);
   RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
   randomAccessFile.setLength(size);
   randomAccessFile.close();
   if (0==size%threadCount)
    blockSize = size/threadCount;
   else{
    blockSize = size/threadCount;
    EndBlockSize = size%threadCount;
   }
   File file2 = new File(Topath+".wbj");
   if (file2.exists()) {
    file2.delete();
    file2.createNewFile();
   }
   RandomAccessFile os = new RandomAccessFile(file2,"rw");
   os.setLength(threadCount*32);
   os.close();
   this.progressBar.setIndeterminate(false);
   this.progressBar.setMaximum(size);
   for (int i = 0; i <= threadCount; i++) {
    if (i==threadCount && EndBlockSize!=-1) {
     if (i*blockSize<size) {
      multithreadDown(this.progressBar,progressLabel,i,i*blockSize,i*blockSize + EndBlockSize);
     }
    }
    else {
     if (i*blockSize<size) {
      multithreadDown(this.progressBar,progressLabel,i,i*blockSize,i*blockSize + blockSize);
     }
    }
    
   }
   
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 public void multithreadDown(final JProgressBar progressBar,final JLabel progressLabel,final int threadId,final int begin,final int end){
  new Thread(){
   public void run() {
    RandomAccessFile os = null;
    RandomAccessFile randomAccessFile = null;
    try {
     byte[] b = new byte[1024];
     int fontCount;
     String ll = begin + " " + (end - 1);
     int lenght2 = 0;
     File file = new File(Topath);
     File file2 = new File(Topath+".wbj");
     os = new RandomAccessFile(file2,"rw");
     randomAccessFile = new RandomAccessFile(file,"rw");
     randomAccessFile.seek(begin);
     URL url = new URL(DownLoadPath);
     URLConnection conn = url.openConnection();
     conn.setRequestProperty("RANGE", "bytes="+begin+"-"+(end -1));
     conn.connect();
     InputStream is = conn.getInputStream();
     while ((fontCount = is.read(b))!=-1) {
      randomAccessFile.write(b,0,fontCount);
      synchronized (progressBar) {
       float size = progressBar.getMaximum();
       int downAll = progressBar.getValue() + fontCount;
       float downD = downAll;
       float d = downD/size*100;
       String progress;
       if (String.valueOf(d).length()>4) {
        progress = String.valueOf(d).substring(0, 4) +"%";
       }
       else {
        progress = String.valueOf(d)+"%";
       }
       
       progressLabel.setText(progress);
       progressBar.setValue(downAll);
       if (downAll==size) {
        progressBar.setVisible(false);
        progressLabel.setText("Download complete");
       }
      }
      
      lenght2 +=fontCount;
      os.seek(threadId*32);
      os.write((ll + " " + lenght2 + "\r\n").getBytes());
     }
     os.close();
     randomAccessFile.close();
    } catch (Exception e) {
     e.printStackTrace();
    }
    finally{
     try {
      Thread.currentThread().stop();
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
   }
  }.start();
 }
}

java编写的Http协议的多线程下载器的更多相关文章

  1. <基于Qt与POSIX线程>多线程下载器的简易搭建

    原创博客,转载请联系博主! 本项目已托管到本人Git远程库:https://github.com/yue9944882/Snow 项目目标  Major Functionality 开发环境:  Ce ...

  2. 通过HTTP协议实现多线程下载

    1. 基本原理,每条线程从文件不同的位置开始下载,最后合并出完整的数据. 2. 使用多线程下载的好处     下载速度快.为什么呢?很好理解,以往我是一条线程在服务器上下载.也就是说,对应在服务器上, ...

  3. Java多线程下载器FileDownloader(支持断点续传、代理等功能)

    前言 在我的任务清单中,很早就有了一个文件下载器,但一直忙着没空去写.最近刚好放假,便抽了些时间完成了下文中的这个下载器. 介绍 同样的,还是先上效果图吧. Jar包地址位于 FileDownload ...

  4. Android版多线程下载器核心代码分享

    首先给大家分享多线程下载核心类: package com.example.urltest; import java.io.IOException; import java.io.InputStream ...

  5. python10min系列之多线程下载器

    今天群里看到有人问关于python多线程写文件的问题,联想到这是reboot的架构师班的入学题,我想了一下,感觉坑和考察的点还挺多,可以当成一个面试题来问,简单说一下我的想法和思路吧,涉及的代码和注释 ...

  6. 06-python进阶-多线程下载器练手

    我们需要用python 写一个多线程的下载器 我们要先获取这个文件的大小 然后将其分片 然后启动多线程 分别去下载 然后将其拼接起来 #!/usr/bin/env python#coding:utf- ...

  7. Ubuntu下的图形化多线程下载器XDM

    目录 1.下载 2.安装 3.浏览器支持 使用Ubuntu下载东西经常过于缓慢,因此需要多进程下载器. 1.下载 下载链接:http://xdman.sourceforge.net/#download ...

  8. Http协议的断点续传下载器,使用观察者模式监视下载进度,使用xml保存下载进度。

    下载使用Http协议,为了做到断点续传,在点击暂停后,将已下载的大小等数据通过Json存入xml中,当继续传输的时候从xml文件中读取大小继续下载(好几个月前写的,真的想不起来了) bool CHtt ...

  9. Android(java)学习笔记215:多线程断点下载的原理(JavaSE实现)

    1. 为什么需要多线程下载?     服务器的资源有限,同时的平均地分配给每个客户端.开启的线程越多抢占的服务的资源就越多,下载的速度就越块. 2. 下载速度的限制条件? (1)你的电脑手机宽带的带宽 ...

随机推荐

  1. iOS - 开发中加载本地word/pdf文档说明

    最近项目中要加载一个本地的word/pdf等文件比如<用户隐私政策><用户注册说明>,有两种方法加载 > 用QLPreviewController控制器实现 步骤 : & ...

  2. Linux学习之---Xshell

    经过两周的断断续续的学习,终于把慕课网上tony老师的<Linux网络管理>基础知识学完.tony老师讲课风趣幽默,深入浅出,为老师打call!! 课程地址:https://www.imo ...

  3. phpcms v9 的表单向导功能的使用方法 附多个案例

    本文主要介绍phpcms v9的表单向导功能是如何使用的,并副多个案例讲解: 先介绍一下v9 的表单向导如何使用 表单向导做的很实用,生成一个表单,常用的是把它作为一个留言板,或者在招聘栏目作为一个供 ...

  4. LeetCode 112 Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. 2018ACM-ICPC焦作区域赛【反思总结】

    摸银结束回来,整个人都轻松了. 自CCPC打铁以来的这两个月真的太痛苦了. 俱乐部退役的退役停训的停训,好冷清啊. 前期切题很稳,前四题两个小时1A. 过了四题之后好像心态有点飘,然后开题就慢了,想题 ...

  6. Luogu 1494 - 小Z的袜子 - [莫队算法模板题][分块]

    题目链接:https://www.luogu.org/problemnew/show/P1494 题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天 ...

  7. [No0000B0]ReSharper操作指南1/16-入门与简介

    安装指南 在安装之前,您可能需要检查系统要求. ReSharper是一个VisualStudio扩展.它支持VisualStudio2010,2012,2013,2015和2017.安装完成后,您将在 ...

  8. 用homebrew 升级安装python3.7 之后系统的python版本还是旧的怎么办

    mac 中安装了多个版本的python$ brew install python3 Updating Homebrew... Warning: python is already installed, ...

  9. 为什么“how to say”是错的?

    2018-04-26 15:53 英语口语 吉米老师前言:如果让老外评选十大Chinglish之最,老师猜"how to say"一定榜上有名.几乎每一位学习英语的童鞋,都曾有过脱 ...

  10. splash 安装

    搞定NVIDIA显卡后,开始弄splash 根据 https://github.com/paperManu/splash 提示 最简安装就是用apt sudo apt install flatpak ...