日期:2014-05-20 浏览次数:20847 次
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; }