我想编写一个程序,用Java将一个文件从linux机器复制/创建到另一台linux / windows机器。我尝试了下面的代码,它将在另一台Windows计算机上创建一个文件。
import java.io.File;
import java.io.IOException;
public class Example2 {
public static void main(String[] args) {
String path = "\\\\10.15.0.166"+File.separator+"test";
String fname= path+File.separator+"Sample.pdf";
File file = new File(fname);
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Exists"+file.exists());
file.getParentFile().mkdirs();
}
}
它在Windows的Windows中工作。但是,当我从linux机器尝试时,它是在linux机器本身中创建文件夹。有人可以帮我解决这个问题吗?
您应该使用类似FTP
在UNIX机器之间移动文件的方法。
JSCH是一个很好的API。
您将需要一些用户身份验证。
这是一个例子:
JSch jsch = new JSCH();
Session session = jsch.getSession(config.getUsername(), config.getHostname(), config.getPort()); //port is usually 22
session.setPassword(config.getPassword());
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp cFTP = (ChannelSftp) channel;
String sourceFile = "---", targetFile = "---";
cFTP.put(sourceFile , targetFile );
cFTP.disconnect();
session.disconnect();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句