网络编程之下载
来源:互联网 发布:java 图片加密 编辑:程序博客网 时间:2024/06/09 23:53
1、
package com.eduask.socketDeom;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.*;public class DownUtil{ private String path; private String targetFile; private int threadNum; private DownThread[] threads; private int fileSize; public DownUtil(String path, String targetFile, int threadNum) { this.path = path; this.threadNum = threadNum; threads = new DownThread[threadNum]; this.targetFile = targetFile; } public void download() throws Exception { //得到一个Ulr URL url = new URL(path); //根据url打开一个网络连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置超时时间为5S conn.setConnectTimeout(5 * 1000); //设置网络的请求方式为get请求方式 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"); //设置字符的编码方式是utf-8 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(); for (int i = 0; i < threadNum; i++) { //设置RandomAcccessFile的偏移量 int startPos = i * currentPartSize; RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw"); currentPart.seek(startPos); threads[i] = new DownThread(startPos, currentPartSize, currentPart); threads[i].start(); } } public double getCompleteRate() { int sumSize = 0; for (int i = 0; i < threadNum; i++) { sumSize += threads[i].length; } return sumSize * 1.0 / 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; } @Override 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; while (length < currentPartSize && (hasRead = inStream.read(buffer)) != -1) { currentPart.write(buffer, 0, hasRead); length += hasRead; } currentPart.close(); inStream.close(); } catch (Exception e) { e.printStackTrace(); } } }}
0 0
- 网络编程之下载
- Swift之网络编程-文件下载
- 异步网络编程之图片批量抓取下载(C#)
- java网络编程之Http多线程下载应用实例
- java网络编程之Http多线程下载应用实例
- C# 网络编程之网页简单下载实现
- 网络编程之get,post请求,文件上传及下载
- java网络编程之android多线程断点下载并提供下载进度(三)
- Android网络编程之——文件断点下载(暂停/继续/重新下载)
- 网络编程基础,多线程下载
- 网络编程,下载视频图片
- 网络编程--模拟迅雷下载
- 网络编程之编程日记
- 网络编程之TCP编程
- 网络编程之UDP编程
- 网络编程之Socket编程
- 网络编程之setsockopt
- 网络编程之初
- 函数式编程入门教程
- 前端静态资源版本更新与缓存之——通过gulp 在原html文件上自动化添加js、css版本号
- Drools6.4从入门到精通
- TCP/IP面试题
- mybatis和hibernate的区别
- 网络编程之下载
- Jenkins+svn+maven+SonarQube+MySQL
- iOS开发多线程篇--GCD
- Elasticsearch
- 文本多行超出省略
- [TCP/IP]三次握手与四次挥手
- PV原语操作详解
- 有些网站,禁用cookie就无法登录
- 使用Git在GitHub上管理代码