try {
File writeName = new File("D:\\data.txt"); // 相对路径,如果没有则要建立一个新的output.txt文件
if(!writeName.exists()) {
writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖 }
FileWriter writer = new FileWriter(writeName);
BufferedWriter out = new BufferedWriter(writer);
out.write(data);
out.flush(); // 把缓存区内容压入文件
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
File file = new File("D:\\data.txt");
if(!file.exists()) {
file.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖 }
FileOutputStream fos = new FileOutputStream(file,true);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(data);
bw.newLine();
bw.flush();
bw.close();
osw.close();
fos.close();
}catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
//将数组写入txt
package org.nms.fast.manage.Utils;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import org.junit.Test;
import org.nms.fast.common.snmp.OtdrData;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.*;
import java.math.BigDecimal;
/**
* ganymed的版本选择build210
* <dependency>
* <groupId>ch.ethz.ganymed</groupId>
* <artifactId>ganymed-ssh2</artifactId>
* <version>build210</version>
* </dependency>
* @author :maph
* @version :
* @date :Created in 2020-03-25 20:41
* @description:
* @modified By:
*/
@Component
public class ScpUtil {
private static String IP = "";
private static int PORT = 22;
private static String USER = "gzster";//登录用户名
private static String PASSWORD = "St@#()69";//生成私钥的密码和登录密码,这两个共用这个密码
private static Connection connection;
private static String PRIVATEKEY = "C:\\Users\\mph\\.ssh\\id_rsa";// 本机的私钥文件
private static boolean usePassword = true;// 使用用户名和密码来进行登录验证。如果为true则通过用户名和密码登录,false则使用rsa免密码登录
public void setParameter(String snmpdeviceIp) {
IP = snmpdeviceIp;
connection = new Connection(IP, PORT);
}
/**
* ssh用户登录验证,使用用户名和密码来认证
*
* @param user
* @param password
* @return
*/
public static boolean isAuthedWithPassword(String user, String password) {
try {
// connection.requestRemotePortForwarding("172.16.16.1", 22, "172.16.16.15", 22);
return connection.authentica