我想从unix系统获取文件到本地系统,该文件位于使用java的窗口上。我对这个概念非常陌生。关于如何完成的任何想法?哪个是最适合这项任务的Java API?使用java将文件从Unix传输到Windows
回答
我发现JSch是非常有用的直序。以下是为了将所有.txt文件放在sftp服务器上的给定文件夹中而编写的代码片段。
public static void sftpConnection() {
// Object Declaration.
JSch jsch = new JSch();
Session session = null;
Channel channel = null;
// Variable Declaration.
String user = "foo";
String host = "10.9.8.7";
Integer port = 22;
String password = "test123";
String watchFolder = "\\localhost\textfiles";
String outputDir = "/remote/textFolder/";
String filemask = "*.txt";
try {
session = jsch.getSession(user, host, port);
/*
* StrictHostKeyChecking Indicates what to do if the server's host
* key changed or the server is unknown. One of yes (refuse connection),
* ask (ask the user whether to add/change the key) and no
* (always insert the new key).
*/
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp)channel;
// Go through watch folder looking for files.
File[] files = findFile(watchFolder, filemask);
for(File file : files) {
// Upload file.
putFile(file, sftpChannel, outputDir);
}
} finally {
sftpChannel.exit();
session.disconnect();
}
}
public static void putFile(File file, ChannelSftp sftpChannel, String outputDir) {
FileInputStream fis = null;
try {
// Change to output directory.
sftpChannel.cd(outputDir);
// Upload file.
fis = new FileInputStream(file);
sftpChannel.put(fis, file.getName());
fis.close();
} catch{}
}
public static File[] findFile(String dirName, final String mask) {
File dir = new File(dirName);
return dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String filename)
{ return filename.endsWith(mask); }
});
}
非常感谢 – user1585111
有多种选择可以做到这一点。首先在java客户端和服务器之间进行一个简单的套接字通信。如果你想要去使用这种方法,然后按照此:
http://mrbool.com/file-transfer-between-2-computers-with-java/24516
还有一些可以使用的,例如FTP,HTTP等其他高层次的协议实现
遵循相关的SO张贴java FTP客户端服务器:FTP client server model for file transfer in Java
谢谢,有一些很好的信息..有没有Java API让这些事情很简单? – user1585111
@ user1585111 java apis可用并广泛用于套接字通信。检查这个:http://download.oracle.com/javase/tutorial/networking/sockets/ –
可以帮助我实现这个任务? – user1585111
如果Unix机器支持SFTP,则可以使用JSch。你能适应下面的代码,以满足您的需求:
private static final String USER_PROMPT = "Enter [email protected]: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);
}
}
}
非常感谢你 – user1585111
很好地使用和非常优雅的方法。 –
channelSftp.get(“/ remotepath/remotefile.txt”,“/localpath/localfile.txt”);特别有帮助 – parishodak
- 1. 使用mule将文件从Unix服务器传输到Windows服务器
- 2. 使用PuTTY从Windows共享路径将文件传输到Unix服务器
- 3. 使用Jsch将文件从Unix传输到Winxp服务器
- 4. .txt文件的二进制FTP传输 - 从Unix到Windows
- 5. 如何使用ssh将文件从Windows传输到Linux?
- 6. 使用pscp将文件从windows传输到linux包装盒
- 7. 使用SCP将文件从Windows传输到Mac
- 8. 文件传输从Unix到Java的进度条
- 9. 使用php-java-bridge传输文件从php上传到java
- 10. 如何使用java将文件从url传输到ftp?
- 11. 使用脚本将文件从UNIX复制到Windows
- 12. 从Windows 8的SavePicker将文件流式传输到文件
- 13. Unix Shell脚本将文件从unix位置复制到windows FTP
- 14. 将文件传输到Windows服务
- 15. Unix Shell文件传输
- 16. 如何将文件从PC传输到Windows-mobile和从Windows-mobile到PC?
- 17. 使用Windows机器将.ipa文件传输到iPad中
- 18. 如何在UNIX下使用(C)将文件从一个文件夹传输到另一个文件夹?
- 19. 将文件从Windows远程传输到Mac会使文件名小写
- 20. 如何使用Java从Windows上传文件夹到Linux机器
- 21. Eclipse CDT .cproject文件从Windows到Unix
- 22. 使用java将文件上传到skydrive
- 23. 使用rsync从Windows传输到Linux
- 24. 通过USB将文件从iOS设备传输到Windows PC
- 25. 如何将文件从Linux(Fedora)传输到Windows Server 2003
- 26. 如何将文件从Windows传输到HortonWorks沙箱?
- 27. Hudson/Jenkins - 将文件从linux传输到windows
- 28. 如何将FTP文件从Windows机器传输到Linux机器
- 29. 将CouchDB .couch文件从Windows传输到Linux
- 30. Unix/Windows脚本帮助:将命令行参数从Windows传递到Unix
你能解释为什么你想特别使用Java吗? Samba或SFTP正在运行,可以立即安装的选项已经可以做到这一点。 – chrylis