📄 getfile.java
字号:
package com.network.flashget;
import java.net.*;
import javax.swing.JOptionPane;
import java.io.IOException;
import java.io.File;
import java.io.RandomAccessFile;
class GetFile
{
//定义关于文件的一些变量----------------------------------------
private long fileLength = 0;
private int threadNum = 0;
private URL url = null;
public static final long FileNotFound = 0;
public static final long FileSizeUnknown = 1;
private String saveTo = null;
private String fileName = null;
//构造函数,获得文件长度,下载线程数,存储路径及文件名称等--------------
GetFile(URL url,int threadNum,String saveTo,String fileName)
{
long result = 0;
try
{
URLConnection urlConn = url.openConnection();
result = urlConn.getContentLength();
}
catch(Exception e)
{
e.printStackTrace();
}
this.fileLength = result;
this.threadNum = threadNum;
this.url = url;
this.saveTo = saveTo;
this.fileName = fileName;
try
{
File file = new File(saveTo);
if (!file.exists())
{
file.createNewFile();
}
RandomAccessFile tempFile = new RandomAccessFile(file,"rw");
tempFile.setLength(fileLength);
tempFile.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//用来调用下载管理类TaskManage,并运行Manage--------------------------------------------
public void connectTaskManager()
{
MainWindow.taskCount++;
TaskManage taskManager = new TaskManage(url,saveTo,fileName,fileLength,threadNum);
System.out.println("Content Length:"+fileLength);
taskManager.assignMission();
MainWindow.taskDownList.add(taskManager);
System.out.println("TaskDownList Length:"+MainWindow.taskDownList.size());
//将该任务加到运行链表中----------------------------------------------------------------
MainWindow.runningList.add(new Boolean(true));
//说明该任务不是从文件读出来的----------------------------------------------------------
MainWindow.statusList.add(new Boolean(true));
//将任务情况加到tableDown表格中去-------------------------------------------------------
String tempStatus = "Downing";
String tempName = taskManager.fileName;
long tempLength = taskManager.fileLength;
float tempRate = taskManager.downRate;
String tempSave = taskManager.saveTo;
int tempThreadNum = taskManager.threadNum;
String tempURL = taskManager.url.toString();
Object[] info = {tempStatus,tempName,tempLength,0,tempRate,tempSave,tempThreadNum,tempURL};
MainWindow.tableDownModel.addRow(info);
//将任务情况加到tableDownDetail表格中去-------------------------------------------------
while(MainWindow.tableDownDetail.getRowCount()>0)
{
MainWindow.tableDownDetailModel.removeRow(0);
}
for(int j = 0;j < (taskManager.taskThreadList).size();j++)
{
int threadNo = j+1;
// String tempName = taskManager.fileName;
long tempStart = ((TaskThread)(taskManager.taskThreadList.get(j))).finalStart;
long tempEnd = ((TaskThread)(taskManager.taskThreadList.get(j))).endPosition;
long tempDown = ((TaskThread)(taskManager.taskThreadList.get(j))).downLength;
// String tempURL = taskManager.url.toString();
Object[] detailInfo = {threadNo,tempName,tempStart,tempEnd,tempDown,tempURL};
MainWindow.tableDownDetailModel.addRow(detailInfo);
}
taskManager.runSubThread();
taskManager.start();
System.out.println("Task Start");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -