激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java使用SFTP上傳文件到資源服務器

java使用SFTP上傳文件到資源服務器

2021-06-22 13:22feifuzeng Java教程

這篇文章主要介紹了java使用SFTP上傳文件到資源服務器,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美成人午夜影院 | 精国产品一区二区三区 | 欧美在线成人影院 | 一级外国毛片 | xxxxxx免费| 国产亚洲欧美视频 | 精品一区二区三区免费 | 亚洲欧美国产视频 | 久久艹艹艹 | 亚洲午夜影院在线观看 | 国产精品刺激对白麻豆99 | 新久久久久久 | 一本到免费视频 | 欧美高清视频一区 | 手机av在线电影 | 欧美一级特黄a | 欧美成人一区二区三区电影 | 国产成人在线网站 | 亚洲码无人客一区二区三区 | 国产免费观看视频 | 午夜爽爽爽男女免费观看hd | 国产精品99免费视频 | 欧美一区二区三区久久综合 | av免费av| 国产一级免费视频 | 国产亚洲精彩视频 | 久久亚洲美女视频 | 亚洲欧美在线视频免费 | 亚洲国产精久久久久久久 | 国产在线观看91精品 | 国产午夜精品久久久久久久蜜臀 | 久久久久久久久久久久久久国产 | 高颜值美女啪啪 | 蜜桃视频网站在线观看 | 精品久久久久久久久久久下田 | 久久国产综合视频 | 成码无人av片在线观看网站 | 最新av在线播放 | 草久影院 | 日本黄色大片免费观看 | 精品一区二区三区毛片 |