Commit 2be7addd by libin

文件下载添加断点续传功能

parent 87796ed3
......@@ -126,14 +126,59 @@ public class FtpUtil {
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
ftp.enterLocalPassiveMode();
ftp.setFileType(FTP.BINARY_FILE_TYPE);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
String remotefileName = ff.getName();
if (remotefileName.equals(fileName)) {
File localFile = new File(localPath +"/" + remotefileName);
long remotesize = ff.getSize();
// 本地存在文件,进行断点下载
if(localFile.exists()){
System.out.println("文件已存在");
long localsize = localFile.length();
if(localsize>=remotesize){
//文件已下载完毕
System.out.println("文件已存在,无需下载");
continue;
}
// 进行断点续传,并记录状态
// 找出本地已经接收了多少
ftp.setRestartOffset(localsize);
FileOutputStream fos = null;
try {
System.out.println(localPath);
System.out.println(localFile.getName());
System.out.println(localFile.getAbsolutePath());
localFile.createNewFile();
fos = new FileOutputStream(localFile,true);
System.out.println("断点续传下载中:" + fileName);
ftp.retrieveFile(fileName, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}else{
OutputStream fos = null;
try {
System.out.println(localPath);
System.out.println(localFile.getName());
System.out.println(localFile.getAbsolutePath());
localFile.createNewFile();
fos = new FileOutputStream(localFile);
System.out.println("下载中:" + fileName);
ftp.retrieveFile(fileName, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
......@@ -146,6 +191,7 @@ public class FtpUtil {
try {
ftp.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
......@@ -155,7 +201,6 @@ public class FtpUtil {
public static boolean uploadFtp(InputStream in,String wjmc) {
//上传文件到ftp文件服务器begin20180806
/* String ip = ftpConfig.getServerip();
int port = Integer.parseInt(ftpConfig.getPortid());
......@@ -172,12 +217,13 @@ public class FtpUtil {
public static void main(String[] args) {
try {
FileInputStream in=new FileInputStream(new File("D:\\JCCJ-R1100000500002014070001.xml"));
// FileInputStream in=new FileInputStream(new File("D:\\JCCJ-R1100000500002014070001.xml"));
// boolean flag = uploadFile("47.92.129.99", 4546, "ftpuser", "fou3rfnder4SD1", "/home/ftpuser/","/2016/", "JCCJ-两岸三地R1100000500002014070001.xml", in);
boolean flag = downloadFile("47.92.129.99", 4546, "ftpuser", "fou3rfnder4SD1", "2016/", "appone.zip","D:\\");
boolean flag = downloadFile("47.92.129.99", 4546, "ftpuser", "fou3rfnder4SD1", "2016/", "appone.zip","C:/tmp");
System.out.println(flag);
} catch (FileNotFoundException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment