这个一搜一大把,如:http://www.ibm.com/developerworks/cn/java/joy-down/

解决方案 »

  1.   

    给LZ贴一个http://wenku.baidu.com/view/f193c71d650e52ea551898d2.html
      

  2.   

    回复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
    就是这样,麻烦大神帮忙看看
      

  3.   

    连接问题,导致File Length is not known!,你看你连接的ip是否你想要连接的ip,还有此ip是否可以ping通。
      

  4.   

    socket链接异常有很多可能,如端口没有开,或是服务器根本就没有启动
      

  5.   

    /* 
     /*
     * 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();
    }
    }
      

  6.   

    在哪里初始化的SiteFileFetch(SiteInfoBean bean)这个,传入的SiteInfoBean bean有问题。
      

  7.   

    /* 
     **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;
    }
    }
      

  8.   

    我是说你new SiteFileFetch(bean)这个的时候有问题,这里面的SiteInfoBean bean在哪里创建的?
      

  9.   

    貌似没找到 我是在这个帖子上看到的 麻烦楼上的大神帮忙看一下,然后帮帮忙给我发一份能够用的 能够运行的
    http://www.ibm.com/developerworks/cn/java/joy-down/
    。谢谢楼上的大神了。
      

  10.   

    弄一份就没时间了,只不过我看你给的连接,找了了new的位置:SiteInfoBean bean = new SiteInfoBean("http://localhost/xx/weblogic60b2_win.exe",
         "L:\\temp","weblogic60b2_win.exe",5); 
    SiteFileFetch fileFetch = new SiteFileFetch(bean); 上面的http://localhost/xx/weblogic60b2_win.exe和L:\\temp这两路径,你改成你正确的路径了吗?可以直接访问的吗?
      

  11.   

    SiteInfoBean bean = new SiteInfoBean(
    "http://localhost/D/myeclipse-8.5.0-win32.exe", "E:\\temp",
    "myeclipse-8.5.0-win32.exe", 5);
    这个是我修改做测试的,D盘的原路径能够通过迅雷下载的,后面的temp临时文件夹也是我设置的,但是就是不知道问题出在哪里?望大神解答,分就给你了。
      

  12.   

    楼主,你好!单纯看代码错误的话(Connection refused),肯定是路径问题,你自己用tomcat开一个服务尝试下,你给的代码我也运行了,没有报异常。
      

  13.   

    我的TOMCAT能够连接localhost啊,而且我输入的下载文件也能通过迅雷进行下载,就是不知道出现什么问题,下载后的文件一个是0KB 一个是1KB 不知道为什么
      

  14.   

    把localhost改成127.0.0.1或是你电脑的ip地址试试呢。
      

  15.   

    import java.io.File;
    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();
    }
    }
    }
    }
      

  16.   

    我那个也测试过127.0.0.1或者自己主机的IP 都不好用。就是FILE LENGTH IS NOT KNOWN 我也不知道问题出在哪里了。源代码就是IBM上我粘的那个网址的 希望大神们帮帮忙啊。快纠结死了。
      

  17.   

    真没看到其它什么问题了。其实我还是怀疑http://localhost/D/myeclipse-8.5.0-win32.exe这个路径。你的tomcat端口是80?tomcat下有个叫“D”的工程?“D”工程根目录下的那个exe文件?
      

  18.   

    我的端口是8080 然后我这个没涉及到工程 就是一个简单的JAVA PROJECT , 不然我把一个简单的JPG文件放到我的PROJECT里面在试试??
      

  19.   

    rul怎么设置啊  纠结好久  
      

  20.   

    url各种改 各种尝试 就是找不到 那个文件