java读取服务器⽂件_java实现读写服务器⽂件
package com.maxd.upload;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import Matcher;
import Pattern;
hz.ssh2.Connection;
hz.ssh2.SCPClient;
hz.ssh2.StreamGobbler;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.maxd.utils.Config;
public class LoginServer {
/**
* @param ip 服务器IP
* @param ur 服务器⽤户名
* @param pwd 服务器密码
* @param port 端⼝
* @param privateKeyPath 可为空
* @param passphra 可为空
* @param sourcePath 本地⽂件路径
* @param destinationPath 上传路径
*/
private static void downLoadFile(String ip, String ur, String pwd, String port, String privateKeyPath, String passphra, String sourcePath, String destinationPath) {
doWrite(ip, ur, pwd, port, privateKeyPath, passphra, sourcePath, destinationPath);
}
/**
* 设置配置
* @param fileName
* @return
*/
public static Properties getProperties(String fileName) {
Properties properties = new Properties();
properties.tProperty("ip", Config.hostname);
properties.tProperty("ur", Config.urname);
properties.tProperty("pwd", Config.password);
properties.tProperty("port", String.valueOf(Config.port));
properties.tProperty("sourcePath", Config.sourcePath + fileName);
properties.tProperty("destinationPath", Config.destinationPath);
return properties;
}
/**
* @param properties
左什么右什么的成语* @param isRead true表⽰读取 fal表⽰写⼊
*/
public static void login(Properties properties, boolean isRead) {
String ip = Property("ip");
String ur = Property("ur");
String pwd = Property("pwd");
String port = Property("port");
String privateKeyPath = Property("privateKeyPath");
String passphra = Property("passphra");
String sourcePath = Property("sourcePath");
String destinationPath = Property("destinationPath");
if (!isRead) {
//写⼊本地⽂件到远程服务器
doWrite(ip, ur, pwd, port, privateKeyPath, passphra, sourcePath, destinationPath); } el {
//读取远程⽂件到本地
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);
猪配什么生肖最好String read = null;
int count = 0;
while ((read = adLine()) != null) {
result = read + "\r\n";
count++;
}
if (bufferedReader != null) {
bufferedReader.clo();
}
if (fileReader != null) {
fileReader.clo();
}
return result;
}
/
**
* @throws UnsupportedEncodingException
* @throws IOException
* @description 写⽂件
*/
public static boolean writeTxtFile(String content, File fileName) throws UnsupportedEncodingException, IOException { FileOutputStream o = null;
o = new FileOutputStream(fileName);
o.Bytes("UTF-8"));
o.clo();
return true;
有趣的数学知识}
大的英文怎么读private static void doWrite(String ip, String ur, String pwd, String port, String privateKeyPath, String passphra, String sourcePath, String destinationPath) {
if (ip != null && !ip.equals("") && ur != null && !ur.equals("") && port != null && !port.equals("") && sourcePath != null && !sourcePath.equals("") && destinationPath != null && !destinationPath.equals("")) {
if (privateKeyPath != null && !privateKeyPath.equals("")) {
sshSftp2(ip, ur, Integer.parInt(port), privateKeyPath,
passphra, sourcePath, destinationPath);
} el if (pwd != null && !pwd.equals("")) {
sshSftp(ip, ur, pwd, Integer.parInt(port), sourcePath,
destinationPath);
移动梦网
} el {
Console console = sole();
System.out.print("Enter password:");
char[] readPassword = adPassword();
sshSftp(ip, ur, new String(readPassword),
Integer.parInt(port), sourcePath, destinationPath);
}
} el {
System.out.println("请先设置配置⽂件");
}
}
/**
* 密码⽅式登录
*
* @param ip
* @param ur
* @param psw
* @param port
* @param sPath
* @param dPath
咳嗽能吃什么
*/
private static void sshSftp(String ip, String ur, String psw, int port,
String sPath, String dPath) {
System.out.println("password login");
Session ssion = null;
JSch jsch = new JSch();
try {
if (port <= 0) {
// 连接服务器,采⽤默认端⼝
ssion = Session(ur, ip);
} el {
// 采⽤指定的端⼝连接服务器
ssion = Session(ur, ip, port);
}
/
/ 如果服务器连接不上,则抛出异常
if (ssion == null) {
throw new Exception("ssion is null");
}
// 设置登陆主机的密码
ssion.tPassword(psw);// 设置密码
// 设置第⼀次登陆的时候提⽰,可选值:(ask | yes | no) ssion.tConfig("StrictHostKeyChecking", "no");
// 设置登陆超时时间
UpLoadFile.upLoadFile(ssion, sPath, dPath);
//DownLoadFile.downLoadFile(ssion, sPath, dPath); } catch (Exception e) {
e.printStackTrace();
}
System.out.println("success");
}
/**
* 密匙⽅式登录
*
* @param ip
钙片g网* @param ur
* @param port
* @param privateKey
* @param passphra