|
@@ -50,138 +50,105 @@ public class Scpclient {
|
|
|
this.password = passward;
|
|
|
}
|
|
|
|
|
|
- public void getFile(String remoteFile, String localTargetDirectory) {
|
|
|
- Connection conn = new Connection(ip, port);
|
|
|
+ /**
|
|
|
+ * 得到服务器上指定文件夹下所有子文件夹(第一级子文件夹)
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<String> getSubfolderName(String targetFolder,String localFolder) {
|
|
|
+ List<String> folderNameList = new ArrayList<String>();
|
|
|
+ Connection connection = null;
|
|
|
+ Session sess = null;
|
|
|
+ BufferedReader stdoutReader = null;
|
|
|
try {
|
|
|
- conn.connect();
|
|
|
- boolean isAuthenticated = conn.authenticateWithPassword(username,
|
|
|
- password);
|
|
|
- if (isAuthenticated == false) {
|
|
|
- System.err.println("authentication failed");
|
|
|
- }
|
|
|
- SCPClient client = new SCPClient(conn);
|
|
|
- client.get(remoteFile, localTargetDirectory);
|
|
|
- conn.close();
|
|
|
- } catch (IOException ex) {
|
|
|
- Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- public void putFile (String localFile, String remoteTargetDirectory){
|
|
|
- Connection conn = new Connection(ip, port);
|
|
|
- try {
|
|
|
- conn.connect();
|
|
|
- boolean isAuthenticated = conn.authenticateWithPassword(username,
|
|
|
- password);
|
|
|
- if (isAuthenticated == false) {
|
|
|
- System.err.println("authentication failed");
|
|
|
- }
|
|
|
- SCPClient client = new SCPClient(conn);
|
|
|
- client.put(localFile, remoteTargetDirectory);
|
|
|
- conn.close();
|
|
|
- } catch (IOException ex) {
|
|
|
- Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
- }
|
|
|
- }
|
|
|
+ // 创建一个连接实例
|
|
|
+ connection = new Connection(ip, port);
|
|
|
+ // 连接
|
|
|
+ connection.connect();
|
|
|
+ // 认证
|
|
|
+ boolean isAuthenticated = connection.authenticateWithPassword(username, password);
|
|
|
+ // 创建一个会话
|
|
|
+ if(isAuthenticated){
|
|
|
+ SCPClient scpClient = connection.createSCPClient();
|
|
|
+ sess = connection.openSession();
|
|
|
+ String command = "ls " + targetFolder;
|
|
|
+ sess.execCommand(command);
|
|
|
+ InputStream stdout = new StreamGobbler(sess.getStdout());
|
|
|
+ stdoutReader = new BufferedReader(new InputStreamReader(stdout));
|
|
|
+
|
|
|
+ while (true) {
|
|
|
+ String line = stdoutReader.readLine();
|
|
|
+ if (line == null){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ //获取文件名称
|
|
|
+ folderNameList.add(line);
|
|
|
+ SCPInputStream scpInputStream = scpClient.get(targetFolder + line);
|
|
|
+ // 下载文件
|
|
|
+ File file = new File(localFolder);
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.mkdirs();
|
|
|
+ }
|
|
|
+ downLoadFileIO(scpInputStream,localFolder + "/" + line);
|
|
|
+ }
|
|
|
|
|
|
- public void putFile (String localFile, String remoteFileName, String remoteTargetDirectory, String mode){
|
|
|
- Connection conn = new Connection(ip, port);
|
|
|
- try {
|
|
|
- conn.connect();
|
|
|
- boolean isAuthenticated = conn.authenticateWithPassword(username,
|
|
|
- password);
|
|
|
- if (isAuthenticated == false) {
|
|
|
- System.err.println("authentication failed");
|
|
|
}
|
|
|
- SCPClient client = new SCPClient(conn);
|
|
|
- if ((mode == null) || (mode.length() == 0)) {
|
|
|
- mode = "0600";
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace(System.err);
|
|
|
+ }finally {
|
|
|
+ if(sess!=null){
|
|
|
+ sess.close();
|
|
|
+ }
|
|
|
+ connection.close();
|
|
|
+ if(stdoutReader!=null){
|
|
|
+ try {
|
|
|
+ stdoutReader.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
- client.put(localFile, remoteFileName, remoteTargetDirectory, mode);
|
|
|
- //重命名
|
|
|
- ch.ethz.ssh2.Session sess = conn.openSession();
|
|
|
- String tmpPathName = remoteTargetDirectory + File.separator + remoteFileName;
|
|
|
- String newPathName = tmpPathName.substring(0, tmpPathName.lastIndexOf("."));
|
|
|
- sess.execCommand("mv " + remoteFileName + " " + newPathName);//重命名回来
|
|
|
- conn.close();
|
|
|
- } catch (IOException ex) {
|
|
|
- Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
}
|
|
|
+ return folderNameList;
|
|
|
}
|
|
|
- public static byte[] getBytes (String filePath){
|
|
|
- byte[] buffer = null;
|
|
|
- try {
|
|
|
- File file = new File(filePath);
|
|
|
- FileInputStream fis = new FileInputStream(file);
|
|
|
- ByteArrayOutputStream byteArray = new ByteArrayOutputStream(1024 * 1024);
|
|
|
- byte[] b = new byte[1024 * 1024];
|
|
|
- int i;
|
|
|
- while ((i = fis.read(b)) != -1) {
|
|
|
- byteArray.write(b, 0, i);
|
|
|
- }
|
|
|
- fis.close();
|
|
|
- byteArray.close();
|
|
|
- buffer = byteArray.toByteArray();
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+
|
|
|
+ private void downLoadFileIO(SCPInputStream scpInputStream,String localFolderFile) throws IOException {
|
|
|
+ // IO流下载文件
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(scpInputStream);
|
|
|
+ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(localFolderFile));
|
|
|
+
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+ int len;
|
|
|
+ while ((len = bis.read(bytes)) > 0) {
|
|
|
+ bos.write(bytes, 0, len);
|
|
|
}
|
|
|
- return buffer;
|
|
|
+ bis.close();
|
|
|
+ bos.close();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 得到服务器上指定文件夹下所有子文件夹(第一级子文件夹)
|
|
|
- * @return
|
|
|
- */
|
|
|
- public List<String> getSubfolderName(String targetFolder) {
|
|
|
- List<String> folderNameList = new ArrayList<String>();
|
|
|
+ public void remove(String remoteFilePath, String fileName) {
|
|
|
+ Connection connection = null;
|
|
|
+ Session sess = null;
|
|
|
try {
|
|
|
// 创建一个连接实例
|
|
|
- Connection connection = new Connection(ip, port);
|
|
|
+ connection = new Connection(ip, port);
|
|
|
// 连接
|
|
|
connection.connect();
|
|
|
// 认证
|
|
|
boolean isAuthenticated = connection.authenticateWithPassword(username, password);
|
|
|
// 创建一个会话
|
|
|
- Session sess = connection.openSession();
|
|
|
- sess.requestPTY("bash");
|
|
|
- sess.startShell();
|
|
|
- InputStream stdout = new StreamGobbler(sess.getStdout());
|
|
|
- InputStream stderr = new StreamGobbler(sess.getStderr());
|
|
|
- BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
|
|
|
- BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
|
|
|
- //向服务器上输入命令
|
|
|
- PrintWriter out = new PrintWriter(sess.getStdin());
|
|
|
- //进入文件存放的目录
|
|
|
- out.println("cd " + targetFolder);
|
|
|
- out.println("ll");
|
|
|
- out.println("exit");
|
|
|
- out.close();
|
|
|
- sess.waitForCondition(ChannelCondition.CLOSED|ChannelCondition.EOF | ChannelCondition.EXIT_STATUS,30000);
|
|
|
-
|
|
|
- //本机查看远程操作的指令及状态
|
|
|
- System.out.println("输入指令后对应的显示信息:");
|
|
|
- while (true) {
|
|
|
- String line = stdoutReader.readLine();
|
|
|
- if (line == null){
|
|
|
- break;
|
|
|
- }
|
|
|
- //获取文件名称
|
|
|
- //取出正确的文件名称
|
|
|
- StringBuffer sb = new StringBuffer(line.substring(line.lastIndexOf(":")+3, line.length()));
|
|
|
- folderNameList.add(line);
|
|
|
+ if(isAuthenticated){
|
|
|
+ sess = connection.openSession();
|
|
|
+ String command = "cd " + remoteFilePath + "&&rm -rf "+ fileName;
|
|
|
+ sess.execCommand(command);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if(sess!=null){
|
|
|
+ sess.close();
|
|
|
}
|
|
|
- //关闭连接
|
|
|
- sess.close();
|
|
|
connection.close();
|
|
|
- stderrReader.close();
|
|
|
- stdoutReader.close();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace(System.err);
|
|
|
- System.exit(2);
|
|
|
}
|
|
|
- return folderNameList;
|
|
|
}
|
|
|
}
|
|
|
|