日期:2014-05-20  浏览次数:20803 次

Java实现ftp 断点 上传 [在线等][实现立即给分]

关键字:JAVA , FTP, 断点, 上传


我用的是RandomAccessFile 去定位文件, 从上传了的位置继续写文件 , 但是遇到一个问题给卡住了.
见下面代码, 求助.. 或给个思路... 或者有其他方式.. 谢谢了.  
比较急, 下班前结贴...
单独聊的请加QQ:54406439

Java code

    public boolean upload(String remote, String fileName, String local) throws IOException {
        ftp.enterLocalPassiveMode();
        if(fileName.endsWith(".rar") || fileName.endsWith(".zip") || fileName.endsWith(".jar")) {
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE); //2进制文件时,防止文件损坏
        }
        boolean result;
        
        ftp.changeWorkingDirectory(remote); //进入ftp目录
        boolean isExists = false;
        FTPFile[] files = ftp.listFiles();
        for (int i = 0; i < files.length; i++) {
            FTPFile file = files[i];
            if(fileName.equals(file.getName())) {
                isExists = true;
                break;
            }
        }

//        String ftpdirpath = CCRUTIL.getEmisProp("FTPDIRPATH");
        File fRemote = null;
        fRemote = new File("\\\\Zhntm01\\Download\\"+remote.substring(1)+fileName);  [color=#FF0000]// (这里要new file吗? 用机器名的方式可以找到文件, 用"ftp://....的方式找不到文件")[/color]
//        isExists = false;
        if(isExists) {
            //文件存在时, 使用断点上传方式
            long fileSize = fRemote.length(); //未下载完成的文件大小
            System.out.println("fileSize:" + fileSize);
            
            RandomAccessFile randomAccessFileRemote = new RandomAccessFile(fRemote, "rw");  //随机方位读取   [color=#FF0000]  (文件禁止访问读写 报错)[/color]
            randomAccessFileRemote.seek(fileSize);  //定位指针到fileSize位置
            
            File fLocal = new File(local);
            RandomAccessFile randomAccessFileLocal = new RandomAccessFile(fLocal, "r");
            randomAccessFileLocal.seek(fileSize);
            
            int len = 0;
            byte[] bt = new byte[1024];
            while ((len = randomAccessFileLocal.read(bt)) > 0) {
                randomAccessFileRemote.write(bt, 0, len);
            }
            randomAccessFileLocal.close();
            randomAccessFileRemote.close();
            result = true;
        } else {
            //不存在时, 直接上传
            File f = new File(local);
            FileInputStream in = new FileInputStream(f);
            result = ftp.storeFile(remote + fileName, in);
            in.close();
        }
        return result;
    }



------解决方案--------------------
看ftp的协议,tcp头可以指定文件读取的位置!
这样你就可以实现断点续传了!
------解决方案--------------------
http://www.javaeye.com/wiki/topic/185529
看看这里
------解决方案--------------------

------解决方案--------------------
帮顶!
------解决方案--------------------
上传取决于服务器。不取决于客户端、

我还不知道支持断点续传的ftp服务器。大部分都得重新上传才行。
------解决方案--------------------
帮顶
------解决方案--------------------
up!
------解决方案--------------------
看ftp的协议,tcp头可以指定文件读取的位置! 
这样你就可以实现断点续传了!
------解决方案--------------------
帮顶那