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
5bc8c6af
Commit
5bc8c6af
authored
Dec 21, 2020
by
libin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
文件上传添加断点续传功能,添加删除ftp文件功能
parent
2be7addd
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
260 additions
and
16 deletions
+260
-16
src/main/java/com/founder/util/FtpUtil.java
+260
-16
No files found.
src/main/java/com/founder/util/FtpUtil.java
View file @
5bc8c6af
...
@@ -4,12 +4,9 @@ import org.apache.commons.net.ftp.FTP;
...
@@ -4,12 +4,9 @@ import org.apache.commons.net.ftp.FTP;
import
org.apache.commons.net.ftp.FTPClient
;
import
org.apache.commons.net.ftp.FTPClient
;
import
org.apache.commons.net.ftp.FTPFile
;
import
org.apache.commons.net.ftp.FTPFile
;
import
org.apache.commons.net.ftp.FTPReply
;
import
org.apache.commons.net.ftp.FTPReply
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
java.io.*
;
import
java.io.*
;
import
java.text.SimpleDateFormat
;
import
java.net.SocketException
;
import
java.util.Date
;
/**
/**
* ftp上传下载工具类
* ftp上传下载工具类
...
@@ -199,27 +196,274 @@ public class FtpUtil {
...
@@ -199,27 +196,274 @@ public class FtpUtil {
}
}
public
static
boolean
uploadFtp
(
InputStream
in
,
String
wjmc
)
{
/**
* 上传文件到ft,支持断点续传,文件名称和ftp路径应使用utf-8编码,避免乱码
* @param host
* @param port
* @param username
* @param password
* @param remotePath 远程ftp文件路径,,支持多级文件路径,格式如"/dir1/dir2/"或者"/dir/",必须以"/"结尾
* @param file
* @return
*/
public
static
boolean
uploadFileToFtp
(
String
host
,
int
port
,
String
username
,
String
password
,
String
remotePath
,
File
file
)
{
FTPClient
ftpClient
=
new
FTPClient
();
String
LOCAL_CHARSET
=
"GBK"
;
boolean
result
=
false
;
try
{
ftpClient
.
connect
(
host
,
port
);
if
(
FTPReply
.
isPositiveCompletion
(
ftpClient
.
sendCommand
(
"OPTS UTF8"
,
"ON"
)))
{
LOCAL_CHARSET
=
"UTF-8"
;
}
ftpClient
.
setControlEncoding
(
LOCAL_CHARSET
);
ftpClient
.
login
(
username
,
password
);
if
(!
FTPReply
.
isPositiveCompletion
(
ftpClient
.
getReplyCode
()))
{
System
.
out
.
println
(
"未连接到FTP,用户名或密码错误。"
);
ftpClient
.
disconnect
();
return
false
;
}
else
{
System
.
out
.
println
(
"FTP连接成功。"
);
}
ftpClient
.
setFileType
(
FTPClient
.
BINARY_FILE_TYPE
);
ftpClient
.
enterLocalPassiveMode
();
//ftpClient.enterLocalActiveMode();
ftpClient
.
setRemoteVerificationEnabled
(
false
);
//本地部署时注释掉该行
}
catch
(
SocketException
e
)
{
e
.
printStackTrace
();
System
.
out
.
println
(
"FTP的IP地址可能错误,请正确配置。"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
System
.
out
.
println
(
"FTP的端口错误,请正确配置。"
);
}
//上传文件到ftp文件服务器begin20180806
String
remoteFileName
=
file
.
getName
();
/* String ip = ftpConfig.getServerip();
try
{
int port = Integer.parseInt(ftpConfig.getPortid());
String username = ftpConfig.getUser();
String password = ftpConfig.getPass();
String basepath = ftpConfig.getBathPath();*/
SimpleDateFormat
sf
=
new
SimpleDateFormat
(
"yyyyMM"
);
String
dateString
=
sf
.
format
(
new
Date
());
//boolean flag = FtpUtil.uploadFile(ip, port, username, password, basepath,"/"+dateString, wjmc, in);
remoteFileName
=
new
String
(
remoteFileName
.
getBytes
(
"UTF-8"
),
"iso-8859-1"
);
//上传文件到ftp文件服务器end
remotePath
=
new
String
(
remotePath
.
getBytes
(
"UTF-8"
),
"iso-8859-1"
);
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
// 对远程目录的处理
if
(
remotePath
.
contains
(
"/"
))
{
// 创建服务器远程目录结构,创建失败直接返回
String
directory
=
remotePath
.
substring
(
0
,
remotePath
.
lastIndexOf
(
"/"
)
+
1
);
try
{
if
(!
directory
.
equalsIgnoreCase
(
"/"
)&&
!
ftpClient
.
changeWorkingDirectory
(
directory
))
{
// 如果远程目录不存在,则递归创建远程服务器目录
int
start
=
0
;
int
end
=
0
;
if
(
directory
.
startsWith
(
"/"
))
{
start
=
1
;
}
else
{
start
=
0
;
}
end
=
directory
.
indexOf
(
"/"
,
start
);
while
(
true
)
{
String
subDirectory
=
remotePath
.
substring
(
start
,
end
);
if
(!
ftpClient
.
changeWorkingDirectory
(
subDirectory
))
{
if
(
ftpClient
.
makeDirectory
(
subDirectory
))
{
ftpClient
.
changeWorkingDirectory
(
subDirectory
);
}
else
{
System
.
out
.
println
(
"创建目录失败"
);
return
false
;
}
}
start
=
end
+
1
;
end
=
directory
.
indexOf
(
"/"
,
start
);
// 检查所有目录是否创建完毕
if
(
end
<=
start
)
{
break
;
}
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
}
// 检查远程是否存在文件
try
{
FTPFile
[]
files
=
ftpClient
.
listFiles
(
remoteFileName
);
if
(
files
.
length
==
1
)
{
long
remoteSize
=
files
[
0
].
getSize
();
long
localSize
=
file
.
length
();
System
.
out
.
println
(
"远程文件大小:"
+
remoteSize
+
",本地文件大小:"
+
localSize
);
if
(
remoteSize
==
localSize
)
{
System
.
out
.
println
(
"文件已上传,无需重复上传"
);
return
true
;
return
true
;
}
else
if
(
remoteSize
>
localSize
)
{
System
.
out
.
println
(
"远程文件大小大于本地文件"
);
return
true
;
}
// 尝试移动文件内读取指针,实现断点续传
result
=
uploadFile
(
remoteFileName
,
file
,
ftpClient
,
remoteSize
);
// 如果断点续传没有成功,则删除服务器上文件,重新上传
if
(!
result
){
if
(!
ftpClient
.
deleteFile
(
remoteFileName
))
{
return
false
;
}
result
=
uploadFile
(
remoteFileName
,
file
,
ftpClient
,
0
);
}
}
else
{
result
=
uploadFile
(
remoteFileName
,
file
,
ftpClient
,
0
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
return
result
;
}
private
static
boolean
uploadFile
(
String
remoteFileName
,
File
localFile
,
FTPClient
ftpClient
,
long
remoteSize
)
throws
IOException
{
boolean
result
=
false
;
// 显示进度的上传
long
step
=
localFile
.
length
()
/
100
;
long
process
=
0
;
long
localreadbytes
=
0L
;
RandomAccessFile
raf
=
new
RandomAccessFile
(
localFile
,
"r"
);
OutputStream
out
=
ftpClient
.
appendFileStream
(
remoteFileName
);
// 断点续传
if
(
remoteSize
>
0
)
{
ftpClient
.
setRestartOffset
(
remoteSize
);
process
=
remoteSize
/
step
;
raf
.
seek
(
remoteSize
);
localreadbytes
=
remoteSize
;
System
.
out
.
println
(
"进行断点续传"
);
}
byte
[]
bytes
=
new
byte
[
10240
];
int
c
;
while
((
c
=
raf
.
read
(
bytes
))
!=
-
1
)
{
out
.
write
(
bytes
,
0
,
c
);
localreadbytes
+=
c
;
if
(
localreadbytes
/
step
!=
process
)
{
process
=
localreadbytes
/
step
;
System
.
out
.
println
(
"上传进度:"
+
process
);
}
}
out
.
flush
();
raf
.
close
();
out
.
close
();
result
=
ftpClient
.
completePendingCommand
();
if
(
remoteSize
>
0
)
{
//status = result ? UploadStatus.Upload_From_Break_Success : UploadStatus.Upload_From_Break_Failed;
System
.
out
.
println
(
result
?
"Upload_From_Break_Success"
:
"Upload_From_Break_Failed"
);
}
else
{
//status = result ? UploadStatus.Upload_New_File_Success : UploadStatus.Upload_New_File_Failed;
System
.
out
.
println
(
result
?
"Upload_New_File_Success"
:
"Upload_New_File_Failed"
);
}
return
result
;
}
/**
* 删除远程ftp服务器上文件,文件名称和ftp路径应使用utf-8编码,避免乱码
* @param host
* @param port
* @param username
* @param password
* @param remotePath 远程ftp文件路径,,支持多级文件路径,格式如"/dir1/dir2/"或者"/dir/",必须以"/"结尾
* @param file
*/
public
static
void
deleteFtpFile
(
String
host
,
int
port
,
String
username
,
String
password
,
String
remotePath
,
File
file
){
FTPClient
ftpClient
=
new
FTPClient
();
String
LOCAL_CHARSET
=
"GBK"
;
boolean
result
=
false
;
try
{
ftpClient
.
connect
(
host
,
port
);
if
(
FTPReply
.
isPositiveCompletion
(
ftpClient
.
sendCommand
(
"OPTS UTF8"
,
"ON"
)))
{
LOCAL_CHARSET
=
"UTF-8"
;
}
ftpClient
.
setControlEncoding
(
LOCAL_CHARSET
);
ftpClient
.
login
(
username
,
password
);
if
(!
FTPReply
.
isPositiveCompletion
(
ftpClient
.
getReplyCode
()))
{
System
.
out
.
println
(
"未连接到FTP,用户名或密码错误。"
);
ftpClient
.
disconnect
();
return
;
}
else
{
System
.
out
.
println
(
"FTP连接成功。"
);
}
ftpClient
.
setFileType
(
FTPClient
.
BINARY_FILE_TYPE
);
ftpClient
.
enterLocalPassiveMode
();
//ftpClient.enterLocalActiveMode();
ftpClient
.
setRemoteVerificationEnabled
(
false
);
//本地部署时注释掉该行
}
catch
(
SocketException
e
)
{
e
.
printStackTrace
();
System
.
out
.
println
(
"FTP的IP地址可能错误,请正确配置。"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
System
.
out
.
println
(
"FTP的端口错误,请正确配置。"
);
}
String
remoteFileName
=
file
.
getName
();
try
{
remoteFileName
=
new
String
(
remoteFileName
.
getBytes
(
"UTF-8"
),
"iso-8859-1"
);
remotePath
=
new
String
(
remotePath
.
getBytes
(
"UTF-8"
),
"iso-8859-1"
);
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
// 对远程目录的处理
if
(
remotePath
.
contains
(
"/"
))
{
// 创建服务器远程目录结构,创建失败直接返回
String
directory
=
remotePath
.
substring
(
0
,
remotePath
.
lastIndexOf
(
"/"
)
+
1
);
try
{
if
(!
directory
.
equalsIgnoreCase
(
"/"
)&&
!
ftpClient
.
changeWorkingDirectory
(
directory
))
{
// 如果远程目录不存在,则递归创建远程服务器目录
int
start
=
0
;
int
end
=
0
;
if
(
directory
.
startsWith
(
"/"
))
{
start
=
1
;
}
else
{
start
=
0
;
}
end
=
directory
.
indexOf
(
"/"
,
start
);
while
(
true
)
{
String
subDirectory
=
remotePath
.
substring
(
start
,
end
);
if
(!
ftpClient
.
changeWorkingDirectory
(
subDirectory
))
{
if
(
ftpClient
.
makeDirectory
(
subDirectory
))
{
ftpClient
.
changeWorkingDirectory
(
subDirectory
);
}
else
{
System
.
out
.
println
(
"创建目录失败"
);
return
;
}
}
start
=
end
+
1
;
end
=
directory
.
indexOf
(
"/"
,
start
);
// 检查所有目录是否创建完毕
if
(
end
<=
start
)
{
break
;
}
}
}
System
.
out
.
println
(
"删除文件"
);
ftpClient
.
deleteFile
(
remoteFileName
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
}
}
}
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
try
{
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 = 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"
,
"C:/tmp"
);
//boolean flag = downloadFile("47.92.129.99", 4546, "ftpuser", "fou3rfnder4SD1", "2016/", "appone.zip","C:/tmp");
File
file
=
new
File
(
new
String
(
"C:/Users/bt/Downloads/ideaIU-2019.3.3 (1).exe"
.
getBytes
(
"UTF-8"
),
"UTF-8"
));
String
remptePath
=
new
String
(
"/2017/10/测试1/"
.
getBytes
(
"UTF-8"
),
"UTF-8"
);
boolean
flag
=
uploadFileToFtp
(
"47.92.129.99"
,
4546
,
"ftpuser"
,
"fou3rfnder4SD1"
,
remptePath
,
file
);
//deleteFtpFile("47.92.129.99", 4546, "ftpuser", "fou3rfnder4SD1", remptePath, file);
System
.
out
.
println
(
flag
);
System
.
out
.
println
(
flag
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
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