回复1楼: 我找到的代码就是这个IBM的。 但是我出BUG了 java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:519) at java.net.Socket.connect(Socket.java:469) at sun.net.NetworkClient.doConnect(NetworkClient.java:163) at sun.net.www.http.HttpClient.openServer(HttpClient.java:394) at sun.net.www.http.HttpClient.openServer(HttpClient.java:529) at sun.net.www.http.HttpClient.<init>(HttpClient.java:233) at sun.net.www.http.HttpClient.New(HttpClient.java:306) at sun.net.www.http.HttpClient.New(HttpClient.java:323) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:852) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:793) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:718) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1041) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373) at NetFox.SiteFileFetch.getFileSize(SiteFileFetch.java:107) at NetFox.SiteFileFetch.run(SiteFileFetch.java:40) -1 File Length is not known! Thread 0 , nStartPos = 0, nEndPos = 0 Thread 1 , nStartPos = 0, nEndPos = 0 Thread 2 , nStartPos = 0, nEndPos = 0 Thread 3 , nStartPos = 0, nEndPos = 0 Thread 4 , nStartPos = 0, nEndPos = 0 就是这样,麻烦大神帮忙看看
连接问题,导致File Length is not known!,你看你连接的ip是否你想要连接的ip,还有此ip是否可以ping通。
我找到的代码就是这个IBM的。
但是我出BUG了
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:323)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:852)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:793)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:718)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1041)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373)
at NetFox.SiteFileFetch.getFileSize(SiteFileFetch.java:107)
at NetFox.SiteFileFetch.run(SiteFileFetch.java:40)
-1
File Length is not known!
Thread 0 , nStartPos = 0, nEndPos = 0
Thread 1 , nStartPos = 0, nEndPos = 0
Thread 2 , nStartPos = 0, nEndPos = 0
Thread 3 , nStartPos = 0, nEndPos = 0
Thread 4 , nStartPos = 0, nEndPos = 0
就是这样,麻烦大神帮忙看看
/*
* SiteFileFetch.java
*/
package NetFox;import java.io.*;
import java.net.*;public class SiteFileFetch extends Thread {
SiteInfoBean siteInfoBean = null;
long[] nStartPos;
long[] nEndPos;
FileSplitterFetch[] fileSplitterFetch;
long nFileLength;
boolean bFirst = true;
boolean bStop = false;
File tmpFile;
DataOutputStream output; public SiteFileFetch(SiteInfoBean bean) throws IOException {
siteInfoBean = bean;
// tmpFile = File.createTempFile ("zhong","1111",new
// File(bean.getSFilePath()));
tmpFile = new File(bean.getSFilePath() + File.separator
+ bean.getSFileName() + ".info");
if (tmpFile.exists()) {
bFirst = false;
read_nPos();
} else {
nStartPos = new long[bean.getNSplitter()];
nEndPos = new long[bean.getNSplitter()];
}
} public void run() {
try {
if (bFirst) {
nFileLength = getFileSize();
if (nFileLength == -1) {
System.err.println("File Length is not known!");
} else if (nFileLength == -2) {
System.err.println("File is not access!");
} else {
for (int i = 0; i < nStartPos.length; i++) {
nStartPos[i] = (long) (i * (nFileLength / nStartPos.length));
}
for (int i = 0; i < nEndPos.length - 1; i++) {
nEndPos[i] = nStartPos[i + 1];
}
nEndPos[nEndPos.length - 1] = nFileLength;
}
}
fileSplitterFetch = new FileSplitterFetch[nStartPos.length];
for (int i = 0; i < nStartPos.length; i++) {
fileSplitterFetch[i] = new FileSplitterFetch(siteInfoBean
.getSSiteURL(), siteInfoBean.getSFilePath()
+ File.separator + siteInfoBean.getSFileName(),
nStartPos[i], nEndPos[i], i);
Utility.log("Thread " + i + " , nStartPos = " + nStartPos[i]
+ ", nEndPos = " + nEndPos[i]);
fileSplitterFetch[i].start();
}
// fileSplitterFetch[nPos.length-1] = new
// FileSplitterFetch(siteInfoBean.getSSiteURL(),siteInfoBean.getSFilePath()
// + File.separator +
// siteInfoBean.getSFileName(),nPos[nPos.length-1],nFileLength,nPos.length-1);
// Utility.log("Thread " +(nPos.length-1) +
// ",nStartPos = "+nPos[nPos.length-1]+",nEndPos = " + nFileLength);
// fileSplitterFetch[nPos.length-1].start();
// int count = 0;
boolean breakWhile = false;
while (!bStop) {
write_nPos();
Utility.sleep(500);
breakWhile = true;
for (int i = 0; i < nStartPos.length; i++) {
if (!fileSplitterFetch[i].bDownOver) {
breakWhile = false;
break;
}
}
if (breakWhile)
break;
// count++;
// if(count>4)
// siteStop();
}
System.err.println("文件下载结束");
} catch (Exception e) {
e.printStackTrace();
}
}
public long getFileSize() {
int nFileLength = -1;
try {
URL url = new URL(siteInfoBean.getSSiteURL());
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
httpConnection.setRequestProperty("User-Agent", "NetFox");
int responseCode = httpConnection.getResponseCode();
if (responseCode >= 400) {
processErrorCode(responseCode);
return -2; // -2 represent access is error
}
String sHeader;
for (int i = 1;; i++) {
// DataInputStream in = new
// DataInputStream(httpConnection.getInputStream ());
// Utility.log(in.readLine());
sHeader = httpConnection.getHeaderFieldKey(i);
if (sHeader != null) {
if (sHeader.equals("Content-Length")) {
nFileLength = Integer.parseInt(httpConnection
.getHeaderField(sHeader));
break;
}
} else
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Utility.log(nFileLength);
return nFileLength;
}
private void write_nPos() {
try {
output = new DataOutputStream(new FileOutputStream(tmpFile));
output.writeInt(nStartPos.length);
for (int i = 0; i < nStartPos.length; i++) {
// output.writeLong(nPos[i]);
output.writeLong(fileSplitterFetch[i].nStartPos);
output.writeLong(fileSplitterFetch[i].nEndPos);
}
output.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private void read_nPos() {
try {
DataInputStream input = new DataInputStream(new FileInputStream(
tmpFile));
int nCount = input.readInt();
nStartPos = new long[nCount];
nEndPos = new long[nCount];
for (int i = 0; i < nStartPos.length; i++) {
nStartPos[i] = input.readLong();
nEndPos[i] = input.readLong();
}
input.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} private void processErrorCode(int nErrorCode) {
System.err.println("Error Code : " + nErrorCode);
} // 停止拷贝
public void siteStop() {
bStop = true;
for (int i = 0; i < nStartPos.length; i++)
fileSplitterFetch[i].splitterStop();
}
}
**SiteInfoBean.java
*/
package NetFox;public class SiteInfoBean {
private String sSiteURL; // Site's URL下载地址
private String sFilePath; // Saved File's Path保存路径
private String sFileName; // Saved File's Name保存名称
private int nSplitter; // Count of Splited Downloading File最大文件下载量 public SiteInfoBean() {
// default value of nSplitter is 5
this("", "", "", 5);
} public SiteInfoBean(String sURL, String sPath, String sName, int nSpiltter) {
sSiteURL = sURL;
sFilePath = sPath;
sFileName = sName;
this.nSplitter = nSpiltter;
} public String getSSiteURL() {
return sSiteURL;
} public void setSSiteURL(String value) {
sSiteURL = value;
} public String getSFilePath() {
return sFilePath;
} public void setSFilePath(String value) {
sFilePath = value;
} public String getSFileName() {
return sFileName;
} public void setSFileName(String value) {
sFileName = value;
} public int getNSplitter() {
return nSplitter;
} public void setNSplitter(int nCount) {
nSplitter = nCount;
}
}
http://www.ibm.com/developerworks/cn/java/joy-down/
。谢谢楼上的大神了。
"L:\\temp","weblogic60b2_win.exe",5);
SiteFileFetch fileFetch = new SiteFileFetch(bean); 上面的http://localhost/xx/weblogic60b2_win.exe和L:\\temp这两路径,你改成你正确的路径了吗?可以直接访问的吗?
"http://localhost/D/myeclipse-8.5.0-win32.exe", "E:\\temp",
"myeclipse-8.5.0-win32.exe", 5);
这个是我修改做测试的,D盘的原路径能够通过迅雷下载的,后面的temp临时文件夹也是我设置的,但是就是不知道问题出在哪里?望大神解答,分就给你了。
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;//断点续传
public class DownLoad { public static void down(String URL, long nPos, String savePathAndFile) {
try {
URL url = new URL(URL);
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
// 设置User-Agent
httpConnection.setRequestProperty("User-Agent", "NetFox");
// 设置断点续传的开始位置
httpConnection.setRequestProperty("RANGE", "bytes=" + nPos);
// 获得输入流
InputStream input = httpConnection.getInputStream();
RandomAccessFile oSavedFile = new RandomAccessFile(savePathAndFile,
"rw");
// 定位文件指针到nPos位置
oSavedFile.seek(nPos);
byte[] b = new byte[1024];
int nRead;
// 从输入流中读入字节流,然后写到文件中
while ((nRead = input.read(b, 0, 1024)) > 0) {
(oSavedFile).write(b, 0, nRead);
}
httpConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public static long getRemoteFileSize(String url) {
long size = 0;
try {
HttpURLConnection conn = (HttpURLConnection) (new URL(url))
.openConnection();
size = conn.getContentLength();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return size;
} public static void main(String[] args) {
String url = "http://dl_dir.qq.com/invc/cyclone/QQDownload_Setup_39_708_p1.exe";
String savePath = "D:\\";
String fileName = url.substring(url.lastIndexOf("/"));
String fileNam = fileName;
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) (new URL(url)).openConnection();
} catch (Exception e) {
e.printStackTrace();
}
File file = new File(savePath + fileName);
// 获得远程文件大小
long remoteFileSize = getRemoteFileSize(url);
System.out.println("远程文件大小=" + remoteFileSize);
int i = 0;
if (file.exists()) {
// 先看看是否是完整的,完整,换名字,跳出循环,不完整,继续下载
long localFileSize = file.length();
System.out.println("已有文件大小为:" + localFileSize); if (localFileSize < remoteFileSize) {
System.out.println("文件续传");
down(url, localFileSize, savePath + fileName);
} else {
System.out.println("文件存在,重新下载");
do {
i++;
fileName = fileNam.substring(0, fileNam.indexOf(".")) + "("
+ i + ")" + fileNam.substring(fileNam.indexOf(".")); file = new File(savePath + fileName);
} while (file.exists());
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
down(url, 0, savePath + fileName);
}
// 下面表示文件存在,改名字 } else {
try {
file.createNewFile();
System.out.println("下载中");
down(url, 0, savePath + fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}