资源简介
主要实现的是登录服务器操作服务器的中的文件数据,支持读写的操作。主要使用的方法getProperties是设置配置的login(参数一是访问服务器的配置,参数二是设置读还是写)方法是读写连接服务器
代码片段和文件信息
package com.maxd.upload;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.StreamGobbler;
import com.jcraft.jsch.*;
import com.maxd.utils.Config;
public class LoginServer {
/**
* @param ip 服务器IP
* @param user 服务器用户名
* @param pwd 服务器密码
* @param port 端口
* @param privateKeyPath 可为空
* @param passphrase 可为空
* @param sourcePath 本地文件路径
* @param destinationPath 上传路径
*/
private static void downLoadFile(String ip String user String pwd String port String privateKeyPath String passphrase String sourcePath String destinationPath) {
doWrite(ip user pwd port privateKeyPath passphrase sourcePath destinationPath);
}
/**
* 设置配置
*
* @param fileName
* @return
*/
public static Properties getProperties(String fileName) {
Properties properties = new Properties();
properties.setProperty(“ip“ Config.hostname);
properties.setProperty(“user“ Config.username);
properties.setProperty(“pwd“ Config.password);
properties.setProperty(“port“ String.valueOf(Config.port));
properties.setProperty(“sourcePath“ Config.sourcePath + fileName);
properties.setProperty(“destinationPath“ Config.destinationPath);
return properties;
}
/**
* @param properties
* @param isRead true表示读取 false表示写入
*/
public static void login(Properties properties boolean isRead) {
String ip = properties.getProperty(“ip“);
String user = properties.getProperty(“user“);
String pwd = properties.getProperty(“pwd“);
String port = properties.getProperty(“port“);
String privateKeyPath = properties.getProperty(“privateKeyPath“);
String passphrase = properties.getProperty(“passphrase“);
String sourcePath = properties.getProperty(“sourcePath“);
String destinationPath = properties.getProperty(“destinationPath“);
if (!isRead) {
//写入本地文件到远程服务器
doWrite(ip user pwd port privateKeyPath passphrase sourcePath destinationPath);
} else {
//读取远程文件到本地
readConnect();
}
}
/**
* @throws IOException
* @description 读文件
*/
public static String readTxtFile(File fileName) throws IOException {
String result = null;
FileReader fileReader = null;
BufferedReader bufferedReader = null;
fileReader = new FileReader(fileName);
InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName) “UTF-8“);
BufferedReader bufferedReader1 = new BufferedReader(isr);
评论
共有 条评论