如何使用 pem file.My 使用 scp 传输文件夹 程序没有显示错误但在服务器上没有显示任何内容(我的文件)
How to transfer folder using scp using pem file.My Program showing no error but showing nothing(my files) on server
我正在尝试使用 pem 文件使用 scp 传输我的文件夹
我的整个程序运行良好,但文件未在服务器上显示。
任何人都可以告诉我哪里做错了
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class jschclass {
public static void main(String[] args) {
String SFTPHOST = "xx.xxx.xx.xxx";
int SFTPPORT = 22;
String SFTPUSER = "myusername";
// String SFTPPASS = "password";
String SFTPWORKINGDIR = "/var/www/html/projects/demoo_reports/";
String Pemfilepath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"lib"+File.separator+"airtel.pem";
String targetFolder = System.getProperty("user.dir")+File.separator+"test-output";
//String Pemfilepath="./src/lib/airtel.pem";
//String targetFolder = "./test-output";
File folder = new File("./test-output");
System.out.println("folder=" + folder);
if (folder.listFiles() != null) {
List<File> listOfFiles = Arrays.asList(folder.listFiles());
for (File file : listOfFiles) {
// System.out.println("File name: " + file.getName());
// System.out.println("Full path: " + file.getAbsolutePath());
System.out.println("Target path: " + targetFolder + file.getName());
System.out.println();
}
}
/////////////////////
final JSch jsch = new JSch();
try {
jsch.addIdentity(Pemfilepath);
System.out.println("identity added ");
Session session = jsch.getSession(SFTPUSER,SFTPHOST,22 );
System.out.println("session created.");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("Connected");
////////////
SimpleDateFormat dateFormatForFoldername = new SimpleDateFormat("yyyy-MM-dd");//dd/MM/yyyy
Date currentDate = new Date();
String folderDateFormat = dateFormatForFoldername.format(currentDate);
String command = "scp -i "+Pemfilepath+" -r "+targetFolder+" "+"user@xx.xxx.xx.xxx:/var/www/html/projects/demo_reports/"+folderDateFormat+"/";
System.out.println(command);
Channel channel = session.openChannel("exec");
//channel.setCommand(command);
//channel.setErrStream(System.err);
channel.connect();
System.out.println("Files is up");
channel.disconnect();
session.disconnect();
System.out.println("Disconnected");
////////////
} catch (JSchException e) {
e.printStackTrace();
}
}
}
你正在做的是行不通的。您不能仅通过在远程服务器上执行命令来上传文件。服务器将从哪里获取文件内容?它无法神奇地访问您的本地文件。
有关 SCP 上传的正确实施,请参阅 official JSch ScpTo.java
example。
在另一种方法中,我直接从本地使用了 sh exector,而没有先连接到服务器
"echo "+pass+" | "+"sudo -S
为我工作
我的完整代码如下:-
String pass = "\"Yourpassword\"";
out.println("echo "+pass+" | "+"sudo -S scp -i "+Pemfilepath+" -r "+targetFolder+" "+"user@xx.xxx.xx.xxx:/var/www/html/projects/demoproject");
希望对您有所帮助:)
我正在尝试使用 pem 文件使用 scp 传输我的文件夹
我的整个程序运行良好,但文件未在服务器上显示。 任何人都可以告诉我哪里做错了
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class jschclass {
public static void main(String[] args) {
String SFTPHOST = "xx.xxx.xx.xxx";
int SFTPPORT = 22;
String SFTPUSER = "myusername";
// String SFTPPASS = "password";
String SFTPWORKINGDIR = "/var/www/html/projects/demoo_reports/";
String Pemfilepath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"lib"+File.separator+"airtel.pem";
String targetFolder = System.getProperty("user.dir")+File.separator+"test-output";
//String Pemfilepath="./src/lib/airtel.pem";
//String targetFolder = "./test-output";
File folder = new File("./test-output");
System.out.println("folder=" + folder);
if (folder.listFiles() != null) {
List<File> listOfFiles = Arrays.asList(folder.listFiles());
for (File file : listOfFiles) {
// System.out.println("File name: " + file.getName());
// System.out.println("Full path: " + file.getAbsolutePath());
System.out.println("Target path: " + targetFolder + file.getName());
System.out.println();
}
}
/////////////////////
final JSch jsch = new JSch();
try {
jsch.addIdentity(Pemfilepath);
System.out.println("identity added ");
Session session = jsch.getSession(SFTPUSER,SFTPHOST,22 );
System.out.println("session created.");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("Connected");
////////////
SimpleDateFormat dateFormatForFoldername = new SimpleDateFormat("yyyy-MM-dd");//dd/MM/yyyy
Date currentDate = new Date();
String folderDateFormat = dateFormatForFoldername.format(currentDate);
String command = "scp -i "+Pemfilepath+" -r "+targetFolder+" "+"user@xx.xxx.xx.xxx:/var/www/html/projects/demo_reports/"+folderDateFormat+"/";
System.out.println(command);
Channel channel = session.openChannel("exec");
//channel.setCommand(command);
//channel.setErrStream(System.err);
channel.connect();
System.out.println("Files is up");
channel.disconnect();
session.disconnect();
System.out.println("Disconnected");
////////////
} catch (JSchException e) {
e.printStackTrace();
}
}
}
你正在做的是行不通的。您不能仅通过在远程服务器上执行命令来上传文件。服务器将从哪里获取文件内容?它无法神奇地访问您的本地文件。
有关 SCP 上传的正确实施,请参阅 official JSch ScpTo.java
example。
在另一种方法中,我直接从本地使用了 sh exector,而没有先连接到服务器
"echo "+pass+" | "+"sudo -S
为我工作
我的完整代码如下:-
String pass = "\"Yourpassword\"";
out.println("echo "+pass+" | "+"sudo -S scp -i "+Pemfilepath+" -r "+targetFolder+" "+"user@xx.xxx.xx.xxx:/var/www/html/projects/demoproject");
希望对您有所帮助:)