为什么总抛出空指针异常,请各位大师帮我改进一下,我将不胜感激

来源:互联网 发布:电脑屏幕扫描软件 编辑:程序博客网 时间:2024/05/02 02:14
package org.cjit.download.student;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.ProtocolException;import java.net.URL;public class MultiThreadDownloadFile {public static String getFileName(String path) {return path.substring(path.lastIndexOf("/") + 1);}public static void downloadFile(String path, int threadSize)throws Exception {URL url = new URL(path);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");// connection.setConnectTimeout(5*1000);int fileLength = connection.getContentLength(); // 获取文件的长度String decodePath = java.net.URLDecoder.decode(path, "GBK");File saveFile = new File("F:\\" + getFileName(decodePath));RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd");accessFile.setLength(fileLength); // 开始的时候就设定好要写入的文件的大小accessFile.close();int block = 0; // 记录每个线程下载文件的大小if ((fileLength / threadSize) == 0) {block = fileLength / threadSize;} else {block = fileLength / threadSize + 1;}for (int threadID = 0; threadID < threadSize; threadID++) {new DownloadThread(url, saveFile, block, threadID).start();}}private static final class DownloadThread extends Thread {private URL url;private File saveFile;private int block;private int threadID;public DownloadThread(URL url, File saveFile, int block, int threadID) {this.block = block;this.saveFile = saveFile;this.threadID = threadID;this.url = url;}@Overridepublic void run() {int startPosition = threadID * block;int endPosition = (threadID + 1) * block - 1;HttpURLConnection connection = null;RandomAccessFile accessFile = null;InputStream inputStream = null;try {accessFile = new RandomAccessFile(saveFile, "rwd");inputStream = connection.getInputStream();accessFile.seek(startPosition); // 设置从什么位置开始写入数据connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");// connection.setConnectTimeout(5*1000);connection.setRequestProperty("Range", "bytes=" + startPosition+ "-" + endPosition);// 指定从网络文件的开始位置到结束位置下载。byte[] buffer = new byte[1024];int length = 0;while ((length = inputStream.read(buffer)) != -1) {accessFile.write(buffer, 0, length);}System.out.println("线程id:" + threadID + "下载完成");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try { inputStream.close(); if(accessFile!=null){ accessFile.close();}} catch (IOException e) {e.printStackTrace();}}}}public static void main(String[] args) {String path = "http://zhangmenshiting.baidu.com/data/music/5789992/%E8%8D%B7%E5%A1%98%E6%9C%88%E8%89%B2.mp3"; try {MultiThreadDownloadFile.downloadFile(path, 3);} catch (Exception e) { e.printStackTrace();}}}
原创粉丝点击