gpt4 book ai didi

java - 使用java将文件从unix传输到windows

转载 作者:可可西里 更新时间:2023-11-01 12:44:50 25 4
gpt4 key购买 nike

我想从 unix 系统获取一个文件到我在 windows 上使用 java 的本地系统。我对这个概念很陌生。关于如何完成的任何想法?哪种 Java API 最适合这项任务?

最佳答案

如果 Unix 机器支持 SFTP,JSch是一个选项。您可以修改以下代码以满足您的需要:

private static final String USER_PROMPT = "Enter username@hostname:port";
private static final boolean USE_GUI = true;

public static void main(final String[] arg) {
Session session = null;
ChannelSftp channelSftp = null;
try {
final JSch jsch = new JSch();

final String defaultInput = System.getProperty("user.name") + "@localhost:22";
String input = (USE_GUI) ? JOptionPane.showInputDialog(USER_PROMPT, defaultInput) : System.console().readLine("%s (%s): ", USER_PROMPT, defaultInput);
if (input == null || input.trim().length() == 0) {
input = defaultInput;
}
final int indexOfAt = input.indexOf('@');
final int indexOfColon = input.indexOf(':');
final String user = input.substring(0, indexOfAt);
final String host = input.substring(indexOfAt + 1, indexOfColon);
final int port = Integer.parseInt(input.substring(indexOfColon + 1));

jsch.setKnownHosts("/path/to/known_hosts");
// if you have set up authorized_keys on the server, using that identitiy
// with the code on the next line allows for password-free, trusted connections
// jsch.addIdentity("/path/to/id_rsa", "id_rsa_password");

session = jsch.getSession(user, host, 22);

final UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);
session.connect();
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get("/remotepath/remotefile.txt", "/localpath/localfile.txt");
} finally {
if (channelSftp != null) {
channelSftp.exit();
}
if (session != null) {
session.disconnect();
}
}
}

public static class MyUserInfo implements UserInfo {
private String password;

@Override
public String getPassword() {
return password;
}

@Override
public boolean promptYesNo(final String str) {
final Object[] options = {"yes", "no"};
final boolean yesNo = (USE_GUI) ? JOptionPane.showOptionDialog(null, str, "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]) == 0 : System.console().readLine("Enter y or n: ").equals("y");
return yesNo;
}

@Override
public String getPassphrase() {
return null;
}

@Override
public boolean promptPassphrase(final String message) {
return true;
}

@Override
public boolean promptPassword(final String message) {
if (!USE_GUI) {
password = new String(System.console().readPassword("Password: "));
return true;
} else {
final JTextField passwordField = new JPasswordField(20);
final Object[] ob = {passwordField};
final int result = JOptionPane.showConfirmDialog(null, ob, message, JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
password = passwordField.getText();
return true;
} else {
return false;
}
}
}

@Override
public void showMessage(final String message) {
if (!USE_GUI) {
System.console().printf(message);
} else {
JOptionPane.showMessageDialog(null, message);
}
}
}

关于java - 使用java将文件从unix传输到windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18033405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com