Java使用sftp定时下载文件的示例代码

2025-05-29 0 78

sftp简介

sftp是secure file transfer protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的网络的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。sftp 为 ssh的其中一部分,是一种传输档案至 blogger 伺服器的安全方式。其实在ssh软件包中,已经包含了一个叫作sftp(secure file transfer protocol)的安全文件信息传输子系统,sftp本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接和答复操作,所以从某种意义上来说,sftp并不像一个服务器程序,而更像是一个客户端程序。sftp同样是使用加密传输认证信息和传输的数据,所以,使用sftp是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的ftp要低得多,如果您对网络安全性要求更高时,可以使用sftp代替ftp。

添加依赖

?

1

2

3

4

5
<dependency>

<groupid>com.jcraft</groupid>

<artifactid>jsch</artifactid>

<version>0.1.54</version>

</dependency>

增加配置

?

1

2

3

4

5

6

7

8
sftp:

ip: 192.168.1.60

port: 22

timeout: 60000

retrytime: 3

admin:

username: admin

password: 2k3xryjbd930.

代码示例

每天凌晨1点在多个用户目录中下载csv文件至本地tmp目录

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133
@service

public class sftptask extends thread {

private channelsftp sftp;

private session session;

@value("${sftp.admin.username}")

private string username;

@value("${sftp.admin.password}")

private string password;

@value("${sftp.host}")

private string host;

@value("${sftp.port}")

private integer port;

private sftpservice sftpservice;

public etlsftptask (sftpservice sftpservice) {

this.sftpservice = sftpservice;

}

/**

* 建立sftp连接

*/

private void connect(){

try {

jsch jsch = new jsch();

session = jsch.getsession(username, host, port);

session.setpassword(password);

session.setconfig("stricthostkeychecking", "no");

session.connect();

channel channel = session.openchannel("sftp");

channel.connect();

sftp = (channelsftp) channel;

}catch (jschexception e) {

e.printstacktrace();

}

}

/**

* 关闭sftp连接

*/

public void close(){

try {

if (sftp != null) {

if (sftp.isconnected()) sftp.disconnect();

}

if(session != null){

if (session.isconnected()) session.disconnect();

}

} catch (exception e) {

e.printstacktrace();

}

}

/**

* 下载文件到本地

*

* @param source 源文件

* @param target 目标文件

* @throws sftpexception 异常

* @throws filenotfoundexception 异常

*/

private void download(string source, string target) throws sftpexception, filenotfoundexception {

sftp.get(source, new fileoutputstream(new file(target)));

}

/**

* 处理用户数据文件

*

* @param root 数据文件根目录

* @param lasttime 上次处理文件的最后的时间

* @return 本次处理文件的最后的时间

*/

private integer handle(string root, integer lasttime) {

string directory = root + "/event/";

vector files;

try {

files = sftp.ls(directory + "event_*.csv");

} catch (exception e) {

e.printstacktrace();

return 0;

}

// 文件名

string filename;

// 临时文件

string tmpfile;

// 文件更新时间

integer mtime;

// 文件最后更新时间

integer maxtime = lasttime;

// 处理用户文件

for(object o: files) {

try {

channelsftp.lsentry f = (channelsftp.lsentry) o;

// 文件更新时间

mtime = f.getattrs().getmtime();

if (mtime <= lasttime) continue;

// 文件名

filename = f.getfilename();

// 最后处理事件

maxtime = math.max(maxtime, mtime);

// 下载文件

tmpfile = "/tmp/" + filename;

download(directory + filename, tmpfile);

} catch (exception e) {

// todo 错误日志

e.printstacktrace();

}

}

// 返回文件最后的处理时间

return maxtime;

}

/**

* 每天凌晨1点开始执行

*/

@scheduled(cron = "0 0 1 * * *")

public void task () {

// 获取sftp连接

connect();

string root;

integer lasttime;

long cid;

integer maxtime = lasttime;

// 获取用户列表

for (sftpdto sftpdto: sftpservice.findall()) {

// 用户主目录

root = sftpdto.getsftproot();

// 上次处理文件的最后时间

lasttime = sftpdto.getlasttime();

maxtime = math.max(maxtime, handle(root, lasttime));

// 更新最后处理时间

if (!maxtime.equals(lasttime)) {

sftpdto.setlasttime(maxtime);

sftpservice.update(sftpdto);

}

}

// 释放sftp资源

close();

}

}

总结

以上所述是小编给大家介绍的java使用sftp定时下载文件的示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:https://my.oschina.net/tianshl/blog/1811017

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java使用sftp定时下载文件的示例代码 https://www.kuaiidc.com/111973.html

相关文章

发表评论
暂无评论