📄 downloadthread.java
字号:
package DownloadManager;import java.io.*;import java.net.*;import java.util.*;/** this class create thread to start downloading. @author Liu Yu Yang */public class DownloadThread //extends Thread //implements Runnable extends Observable implements Runnable { // Max size of download buffer. private static final int MAX_BUFFER_SIZE = 1024; // These are the status names. public static final String STATUSES[] = {"Downloading", "Paused", "Complete", "Cancelled", "Error"}; // These are the status codes. public static final int DOWNLOADING = 0; public static final int PAUSED = 1; public static final int COMPLETE = 2; public static final int CANCELLED = 3; public static final int ERROR = 4; private InputStream randIn; private RandomAccessFile randOut; private URL url; //download URL private long block; //size of this thread private long localSize; // number of bytes downloaded private int threadId = -1; private boolean done = false; private int status; // current status of download private String currentThreadName; /** * * @param url File URL * @param out Output file * @param block The block of each thread for downloading * @param threadId Current thread number */ public DownloadThread(URL url, RandomAccessFile out, long block, int threadId) { this.url = url; this.randOut = out; this.block = block; this.threadId = threadId; this.localSize = -1; status = DOWNLOADING; download(); } /** @param url File URL @param out Output file @param block The block of each thread for downloading @param threadId Current thread number @param currentThreadName This is to mark the temp file name. */ public DownloadThread(URL url, RandomAccessFile out, long block, int threadId, String currentThreadName) { this.url = url; this.randOut = out; this.block = block; this.threadId = threadId; this.localSize = -1; this.currentThreadName = currentThreadName; status = DOWNLOADING; download(); } // Start or resume downloading. private void download() { Thread thread = new Thread(this); thread.start(); // run(); } public void pause() { status = PAUSED; stateChanged(); } // Resume this download. public void resume() { status = DOWNLOADING; stateChanged(); download(); } // Cancel this download. public void cancel() { status = CANCELLED; stateChanged(); } // Mark this download as having an error. private void error() { status = ERROR; stateChanged(); } //start thread public void run() { String localFile = currentThreadName; //file's path and name try { // Open connection to URL. HttpURLConnection http = (HttpURLConnection) url.openConnection(); // Specify what portion of file to download. http.setRequestProperty("Range", "bytes=" + block * (threadId - 1) + "-"); randIn = http.getInputStream(); //downloading byte[] buffer = new byte[MAX_BUFFER_SIZE]; int offset = 0; // file size reading from server into buffer each time System.out.println("Thread " + threadId + " start downloading");// bufferedInputStream = new BufferedInputStream(http.getInputStream());// fileOutputStream = new FileOutputStream(localFile_tp);// dataOutputStream = new DataOutputStream(fileOutputStream); System.out.println("锟斤拷锟节斤拷锟斤拷锟侥硷拷 " + localFile + " ..."); //if downloading while ((offset = randIn.read(buffer)) != -1 && localSize <= block) { // Write buffer to file. randOut.write(buffer, 0, offset); //dataOutputStream.write(buffer,0,offset); localSize += offset; stateChanged(); } } catch (Exception e) { System.err.println(e); } finally { // Close file. if (randOut != null) { try { randOut.close(); } catch (Exception e) { } } // Close connection to server. if (randIn != null) { try { randIn.close(); } catch (Exception e) { } } done = true; System.out.println("Thread " + threadId + " end downloading"); //this.interrupt(); } } public boolean isFinished() { return done; } // Notify observers that this download's status has changed. private void stateChanged() { setChanged(); notifyObservers(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -