Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
X
xzxt-api-report
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
雷紫添
xzxt-api-report
Commits
2be7addd
Commit
2be7addd
authored
Dec 17, 2020
by
libin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
文件下载添加断点续传功能
parent
87796ed3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
9 deletions
+55
-9
src/main/java/com/founder/util/FtpUtil.java
+55
-9
No files found.
src/main/java/com/founder/util/FtpUtil.java
View file @
2be7addd
...
...
@@ -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
(
FileNotFound
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment