|
@@ -1,12 +1,10 @@
|
|
|
package com.github.zuihou.common.util;
|
|
|
|
|
|
+import ch.ethz.ssh2.*;
|
|
|
+
|
|
|
import java.io.*;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
-import java.util.logging.Level;
|
|
|
-import java.util.logging.Logger;
|
|
|
-
|
|
|
-import ch.ethz.ssh2.*;
|
|
|
|
|
|
public class Scpclient {
|
|
|
|
|
@@ -125,6 +123,57 @@ public class Scpclient {
|
|
|
bos.close();
|
|
|
}
|
|
|
|
|
|
+ private byte[] readFileBytes(InputStream is){
|
|
|
+ byte[] data = null;
|
|
|
+ try {
|
|
|
+ //严谨起见,一定要加上这个判断,不要返回data[]长度为0的数组指针
|
|
|
+ if(is.available()==0){
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+ data = new byte[is.available()];
|
|
|
+ is.read(data);
|
|
|
+ is.close();
|
|
|
+ return data;
|
|
|
+ } catch (IOException e) {
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void uploadFileIO(String localFile, String remoteFileName, String remoteTargetDirectory) {
|
|
|
+ Connection connection = null;
|
|
|
+ SCPOutputStream os = null;
|
|
|
+ try {
|
|
|
+
|
|
|
+ // 创建一个连接实例
|
|
|
+ connection = new Connection(ip, port);
|
|
|
+ // 连接
|
|
|
+ connection.connect();
|
|
|
+ // 认证
|
|
|
+ boolean isAuthenticated = connection.authenticateWithPassword(username, password);
|
|
|
+ // 创建一个会话
|
|
|
+ if(isAuthenticated){
|
|
|
+ SCPClient scpClient = connection.createSCPClient();
|
|
|
+ File localFileObj = new File(localFile);
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(localFileObj);
|
|
|
+ byte[] localFileByte = readFileBytes(fileInputStream);
|
|
|
+ os = scpClient.put(remoteFileName,localFileByte.length,remoteTargetDirectory,null);
|
|
|
+ os.write(localFileByte,0,localFileByte.length);
|
|
|
+ os.flush();
|
|
|
+ }
|
|
|
+ } catch (Exception e){
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ if(null != os){
|
|
|
+ try {
|
|
|
+ os.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void remove(String remoteFilePath, String fileName) {
|
|
|
Connection connection = null;
|
|
|
Session sess = null;
|