第一题:简单的URL获取资源下载
import java.net.URL;
import java.net.URLConnection;import java.io.InputStream;
import java.io.FileOutputStream;class DownloadDemo{public static void main(String[] args){String src_url = "http://one.jiangmin.com/jiangmin.zip";try{URL url = new URL(src_url);URLConnection conn = url.openConnection();String type = conn.getContentType();int length = conn.getContentLength();InputStream in = conn.getInputStream();FileOutputStream fos = new FileOutputStream("e:/jiangmin.zip");byte[] buf = new byte[1024];int len = 0;while((len = in.read(buf)) != -1){fos.write(buf);}fos.close();in.close();}catch(Exception e){e.printStackTrace();}}
}第二题:URL多线程下载
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JButton;import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;import java.net.URL;
import java.net.URLConnection;import java.io.InputStream;
import java.io.RandomAccessFile;import java.util.List;
import java.util.ArrayList;class Download{private static final long serialVersionUID = -6944831795769317874L;public static void main(String[] args){new DownloadWindow();}
}class DownloadWindow extends JFrame{private JTextArea url_text;private JTextArea local_text;public DownloadWindow(){ini();}public void ini(){this.setSize(600,400);this.setLocation(600,300);this.setLayout(null);JLabel url_label = new JLabel("URL : ");url_label.setBounds(10,10,100,20);this.add(url_label);url_text = new JTextArea();url_text.setEditable(true);url_text.setBounds(100,10,400,20);this.add(url_text);JLabel local_label = new JLabel("Local : ");local_label.setBounds(10,40,100,20);this.add(local_label);local_text = new JTextArea();local_text.setEditable(true);local_text.setBounds(100,40,400,20);this.add(local_text);JButton down_button = new JButton("Download");down_button.setBounds(200,110,200,50);down_button.addMouseListener(new MouseAdapter(){public void mouseClicked(MouseEvent e){String src_url = url_text.getText();System.out.println(src_url);String local_addr = local_text.getText();new DownloadManager(DownloadWindow.this,src_url,local_addr,5).start();}});this.add(down_button);this.setVisible(true);}
}class DownloadManager{private DownloadWindow window;private String src_url;private String local_addr;private int count;private DownloadInfo info;private List<DownloadInfo> info_list;public DownloadManager(DownloadWindow window,String src_url,String local_addr,int count){this.window = window;this.src_url = src_url;this.local_addr = local_addr;this.count = count;prepareDownload();}public void prepareDownload(){info_list = new ArrayList<DownloadInfo>();try{URL url = new URL(src_url);System.out.println(src_url);int total_length = url.openConnection().getContentLength();RandomAccessFile raf = new RandomAccessFile(local_addr,"rw");raf.setLength(total_length);raf.close();System.out.println(total_length);System.out.println(count);int block = total_length / count;DownloadInfo info = null;for(int i = 0;i < count;i++){info = new DownloadInfo();info.setSrcUrl(src_url);info.setLocalAddr(local_addr);if((count - 1) == i){info.setEndPos(total_length - 1);}else{info.setStartPos((i + 1) * block - 1);}info_list.add(info);}}catch(Exception e){e.printStackTrace();}}public void start(){int i = 0;for(DownloadInfo info : info_list){System.out.println(i);new DownloadThread(window,info).start();i++;}}
}class DownloadThread extends Thread{private DownloadWindow window;private DownloadInfo info;public DownloadThread(DownloadWindow window,DownloadInfo info){this.window = window;this.info = info;}public void run(){try{System.out.println(info.getSrcUrl());URL url = new URL(info.getSrcUrl());URLConnection connect = url.openConnection();connect.setRequestProperty("Range","bytes=" + info.getStartPos() + "-" + info.getEndPos());InputStream in = connect.getInputStream();RandomAccessFile raf = new RandomAccessFile(info.getLocalAddr(),"rw");raf.seek(info.getEndPos());byte[] buf = new byte[1024];int len = 0;while((len = in.read(buf)) != -1){raf.write(buf);}raf.close();}catch(Exception e){e.printStackTrace();}}
}class DownloadInfo{private String src_url;private String local_addr;private int start_pos;private int end_pos;public DownloadInfo(){}public DownloadInfo(String src_url,String local_addr,int start_pos,int end_pos){this.src_url = src_url;this.local_addr = local_addr;this.start_pos = start_pos;this.end_pos = end_pos;}public String getSrcUrl(){return src_url;}public void setSrcUrl(String src_url){this.src_url = src_url;}public String getLocalAddr(){return local_addr;}public void setLocalAddr(String local_addr){this.local_addr = local_addr;}public int getStartPos(){return start_pos;}public void setStartPos(int start_pos){this.start_pos = start_pos;}public int getEndPos(){return end_pos;}public void setEndPos(int end_pos){this.end_pos = end_pos;}
}

转载于:https://blog.51cto.com/senlinmin/1790906

大数据Java基础第十九天作业相关推荐

  1. 【全集】大数据Java基础

    课程介绍 本课程是由猎豹移动大数据架构师,根据Java在公司大数据开发中的实际应用,精心设计和打磨的大数据必备Java课程.通过本课程的学习大数据新手能够少走弯路,以较短的时间系统掌握大数据开发必备语 ...

  2. 大数据java基础吗?

    大数据必备 关于大数据基础知识,以前浪尖写过一篇文章,也多次在 知识星球里分享过经验. 具体学习内容,可以参看如下文章: 入门大数据必读 这个可以看到做大数据的话java是必需品,因为基本所有的大数据 ...

  3. 大数据 -- java基础16 网络编程 TCP UDP

    1.网络编程的三要素:(1)IP地址(2)端口号(3)协议 2.IP地址 : 网络中计算机唯一标识. (1)IP地址在计算机中存储的时候是由二进制的形式进行存储的:IP:192.168.7.42 十进 ...

  4. 大数据 -- java基础8 接口特点和用法

    1.接口:interface关键字     (1)语句定义格式: interface 接口名{} (2)接口是如何实现的呢?         接口不能直接实例化,需要一个关键字供其他类实现该接口:im ...

  5. 大数据Java基础第十二天作业

    第一题:HashMap内部实现原理 HashMap存的是key => value 对的集合,每一对就是一个entry(条目),key和value存的都是对象的引用.key不能存重复的值,key的 ...

  6. JAVA大数据的第五十九天——The authenticity of host ‘gree129 (192.168.**.129)‘ can‘t be established.

    在搭建hadoop高可通集群,启动yarn的时候遇到问题 [root@gree131 hadoop]# start-yarn.sh starting yarn daemons starting res ...

  7. 【01.23】大数据 -- JAVA基础 P15-P24

    复习: 注释:再生续重解释说明的文字 单行注释,多行注释,文本注释 计算机常量:整数.小数.字符.字符串.布尔.空 进制:二进制–0b 八进制–0 十六进制–0x 变量:先定义后使用,先赋值后使用,在 ...

  8. 大数据Java基础一

    DataType -------------  基本数据类型   100 int  1.2   1.byte short int long float double   2.boolean[true, ...

  9. 【大数据Java基础-JAVA 数据结构04】java常用类(四)比较器以及其他类

    比较器 1.Java比较器的使用背景: Java中的对象,正常情况下,只能进行比较:== 或 != .不能使用 > 或 < 的 但是在开发场景中,我们需要对多个对象进行排序,言外之意,就需 ...

最新文章

  1. 5.7-基于Binlog+Position的复制搭建
  2. 王茂霖:数据挖掘提分三板斧!(附PPT下载)
  3. MySQL输入密码后闪退
  4. python第五十一课——__slots
  5. Sed教程(五):管理模式、正则表达式、使用功能
  6. Unity物理投射相关问题整理
  7. bert 是单标签还是多标签 的分类_标签感知的文档表示用于多标签文本分类(EMNLP 2019)...
  8. MFC工作笔记0005---::在vc++中是什么意思
  9. python 基础 信息量很大很好,适合复习
  10. Android NDK JNI C++ 3
  11. pdf怎么解除限制打印
  12. 重邮学报和计算机工程与应用,重庆邮电大学学报
  13. 深入理解计算机系统_3e 第四章家庭作业(部分) CS:APP3e chapter 4 homework
  14. 我的世界ess服务器信息,我的世界ess指令怎么用 ess指令大全及用法详解
  15. linux tig不支持中文,解决git log 以及tig命令中文乱码问题
  16. 未来的智能制造,或许会往这些方向推进
  17. 仿QQ浏览器mac版官网主页 html+css3特效
  18. My97pickerdate设置默认开始日期为当天
  19. vs + BabeLua + Cocos2d-x 3.10配置
  20. C++中tan、atan、sin、cos等三角函数用法的代码演示及结果,注意角度和弧度的转换!

热门文章

  1. mac搜索服务器文件,ProFind——文件搜索神器
  2. 空间滤波器是怎么来的
  3. 中国K12教育行业运营动向及未来发展战略分析报告2022年版
  4. 全球及中国智能食品秤行业发展预测及市场规模预测报告2021年版
  5. 细说 #pragma pack(n)
  6. 巨头扎堆“当农民”-丰收节交易会·万祥军:谋定生机
  7. [搬运工系列]-JMeter(二十四)搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)
  8. MyBatis参数传入集合之foreach动态sql
  9. Java Swing界面编程(28)---复选框:JCheckBox
  10. Android之使用Android-AQuery异步加载图片(一)