本文實例為大家分享了java實現sftp上傳文件到資源服務器工具類,供大家參考,具體內容如下
首先得創建連接sftp服務器的公共類mysftp.java:
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
package cn.test.util; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.text.simpledateformat; import java.util.date; import java.util.properties; import java.util.vector; import javax.servlet.http.httpservletrequest; import com.jcraft.jsch.channel; import com.jcraft.jsch.channelsftp; import com.jcraft.jsch.jsch; import com.jcraft.jsch.session; import com.jcraft.jsch.sftpexception; public class mysftp { /** * 連接sftp服務器 * @param host 主機 * @param port 端口 * @param username 用戶名 * @param password 密碼 */ public channelsftp connect(string host, int port, string username, string password) { channelsftp sftp = null ; try { jsch jsch = new jsch(); jsch.getsession(username, host, port); session sshsession = jsch.getsession(username, host, port); sshsession.setpassword(password); properties sshconfig = new properties(); sshconfig.put( "stricthostkeychecking" , "no" ); sshsession.setconfig(sshconfig); sshsession.connect(); channel channel = sshsession.openchannel( "sftp" ); channel.connect(); sftp = (channelsftp) channel; } catch (exception e) { } return sftp; } /** * 上傳文件 * * @param directory * 上傳的目錄 * @param uploadfile * 要上傳的文件 * @param sftp */ public void upload(string directory, string uploadfile, channelsftp sftp) { try { sftp.cd(directory); file file = new file(uploadfile); sftp.put( new fileinputstream(file), file.getname()); } catch (exception e) { e.printstacktrace(); } } /** * 下載文件 * * @param directory * 下載目錄 * @param downloadfile * 下載的文件 * @param savefile * 存在本地的路徑 * @param sftp */ public void download(string directory, string downloadfile, string savefile, channelsftp sftp) { try { sftp.cd(directory); file file = new file(savefile); sftp.get(downloadfile, new fileoutputstream(file)); } catch (exception e) { e.printstacktrace(); } } /** * 刪除文件 * * @param directory * 要刪除文件所在目錄 * @param deletefile * 要刪除的文件 * @param sftp */ public void delete(string directory, string deletefile, channelsftp sftp) { try { sftp.cd(directory); sftp.rm(deletefile); } catch (exception e) { e.printstacktrace(); } } public void uploadsftp(httpservletrequest request,string[] uploadfiles) throws exception { mysftp mysftp = new mysftp(); sftputil sftputil = new sftputil(); channelsftp sftp = null ; session session = null ; try { sftp = mysftp.connect(systemconstants.sftp_host, integer.parseint(systemconstants.sftp_port), systemconstants.sftp_username, systemconstants.sftp_password); for ( int i = 0 ; i < uploadfiles.length; i++) { date uploadtime = new date(); string url=request.getsession().getservletcontext().getrealpath( "" ).replaceall( "\\\\" , "/" ); string uploadfile =url.substring( 0 , url.lastindexof( "/" ))+uploadfiles[i]; // string savefile=""; simpledateformat sdf = new simpledateformat( "yyyymmdd" ); string ymd = sdf.format(uploadtime); string relativepath = ymd + "/" ; string directory = systemconstants.sftp_directory+ relativepath; sftputil.createdir(directory, sftp); mysftp.upload(directory, uploadfile, sftp); sftp.cd(directory); } } catch (exception e) { e.printstacktrace(); } finally { try { if (sftp != null ) { sftp.disconnect(); } } catch (exception e) { e.printstacktrace(); } try { if (session != null ) { session.disconnect(); } } catch (exception e) { e.printstacktrace(); } } } /** * 列出目錄下的文件 * * @param directory * 要列出的目錄 * @param sftp * @return * @throws sftpexception */ @suppresswarnings ( "rawtypes" ) public vector listfiles(string directory, channelsftp sftp) throws sftpexception { return sftp.ls(directory); } } |
上傳圖片時,調用sftputil類中的uploadmultipartfile方法即可。
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
|
package cn.test.util; import java.text.simpledateformat; import java.util.date; import java.util.random; import org.springframework.web.multipart.multipartfile; import com.jcraft.jsch.channelsftp; import com.jcraft.jsch.session; import com.jcraft.jsch.sftpattrs; import com.jcraft.jsch.sftpexception; /**文件上傳工具類*/ public class sftputil { /** * spring文件上傳方法 * */ public static string uploadmultipartfile(multipartfile file,string fileext) { sftputil sftputil = new sftputil(); string originalfilename = file.getoriginalfilename(); //生成文件名 date uploadtime = new date(); simpledateformat sdf = new simpledateformat( "yyyymmdd" ); string ymd = sdf.format(uploadtime); string relativepath = ymd+ "/" ; simpledateformat df = new simpledateformat( "yyyymmddhhmmss" ); string filename = df.format(uploadtime) + "_" + new random().nextint( 1000 ) + "." + fileext; // string filename = string.valueof(new date().gettime())+ new random().nextint(10000)+"."+originalfilename.substring(originalfilename.lastindexof(".") + 1); string directory=systemconstants.sftp_directory+relativepath; channelsftp sftp = null ; session session = null ; try { mysftp mysftp = new mysftp(); sftp = mysftp.connect(systemconstants.sftp_host, integer.parseint(systemconstants.sftp_port), systemconstants.sftp_username, systemconstants.sftp_password); session = sftp.getsession(); sftputil.createdir(directory, sftp); sftp.cd(directory); sftp.put(file.getinputstream(), filename); sftp.disconnect(); sftp.getsession().disconnect(); } catch (exception e) { system.out.println( "文件[" + originalfilename + "]上傳失敗,堆棧軌跡如下:" ); e.printstacktrace(); } finally { try { if (sftp != null ) { sftp.disconnect(); } } catch (exception e) { e.printstacktrace(); } try { if (session != null ) { session.disconnect(); } } catch (exception e) { e.printstacktrace(); } } string rename=systemconstants.sftp_httpbaseurl+relativepath+filename; return rename; } /** * 創建目錄 * @throws exception */ public void createdir(string createpath, channelsftp sftp) throws exception { try { if (isdirexist(sftp, createpath)) { sftp.cd(createpath); } string patharry[] = createpath.split( "/" ); stringbuffer filepath = new stringbuffer( "/" ); for (string path : patharry) { if (path.equals( "" )) { continue ; } filepath.append(path + "/" ); if (isdirexist(sftp, filepath.tostring())) { sftp.cd(filepath.tostring()); } else { // 建立目錄 sftp.mkdir(filepath.tostring()); // 進入并設置為當前目錄 sftp.cd(filepath.tostring()); } } sftp.cd(createpath); } catch (sftpexception e) { throw new exception(e.getmessage()); } } /** * 判斷目錄是否存在 */ public boolean isdirexist(channelsftp sftp,string directory) { boolean isdirexistflag = false ; try { sftpattrs sftpattrs = sftp.lstat(directory); isdirexistflag = true ; return sftpattrs.isdir(); } catch (exception e) { if (e.getmessage().tolowercase().equals( "no such file" )) { isdirexistflag = false ; } } return isdirexistflag; } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/feifuzeng/article/details/71159162