我的应用程序服务器托管在具有 Tomcat 服务器的 Linux 中。我想通过我的 Windows 机器上运行的 Java 代码更改一些文件。我该怎么做?我知道如何通过 Java 连接到 Linux,但不知道用于写入、追加或清除文件的命令。
非常感谢!
最佳答案
您可以使用外部库来做到这一点 JSch . 下面应该做的工作。
JSch jsch = new JSch();
Session session = jsch.getSession("remote_user_name", "remote_host_or_ip", 22); // 22 for SFTP
session.setPassword("remote_password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(10000);
Channel channel = session.openChannel("sftp");
channel.connect();
System.out.println("Connection Opened");
ChannelSftp channelSftp = (ChannelSftp) channel;
InputStream inputStream = new FileInputStream("text_file.txt");
channelSftp.put(inputStream, "/remote/folder/file_to_be_rewritten.txt");
System.out.println("File should be uploaded");
channelSftp.disconnect();
session.disconnect();
关于java - 如何使用在我的 Windows 机器上运行的 Java 代码写入 Linux 中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50074057/