Java 文件上载到SFTP失败(Apache VFS)
我的SFTP(Windows with)有问题。我尝试在文件夹中写入一个文件。在本地SFTP上,我对上传没有问题,但在第二个SFTP上,我总是得到下面的错误 FTP如下所示: 我需要上传到文件夹“alis”。奇怪的是,它没有用户/组和770权限。但是,使用FileZilla,文件上传工作正常(使用相同的登录名) 在文件夹“alis”上执行“manager.resolveFile()”(我尝试上载到此文件夹)并打印“.getType()”,我得到的信息是“File”,而不是预期的“Folder” 有人知道为什么VFS会将文件夹识别为文件,或者为什么上传不起作用吗? 将文件上载到SFTP时出现异常:Java 文件上载到SFTP失败(Apache VFS),java,sftp,vfs,apache-commons-vfs,Java,Sftp,Vfs,Apache Commons Vfs,我的SFTP(Windows with)有问题。我尝试在文件夹中写入一个文件。在本地SFTP上,我对上传没有问题,但在第二个SFTP上,我总是得到下面的错误 FTP如下所示: 我需要上传到文件夹“alis”。奇怪的是,它没有用户/组和770权限。但是,使用FileZilla,文件上传工作正常(使用相同的登录名) 在文件夹“alis”上执行“manager.resolveFile()”(我尝试上载到此文件夹)并打印“.getType()”,我得到的信息是“File”,而不是预期的“Folder”
Exception in thread "main" java.lang.RuntimeException: org.apache.commons.vfs2.FileSystemException: Could not copy "file:///D:/Test/test.txt" to "sftp://user:***@host/.../alis/test.txt".
at test.Test.upload(Test.java:77)
at test.Test.main(Test.java:22)
Caused by: org.apache.commons.vfs2.FileSystemException: Could not copy "file:///D:/Test/test.txt" to "sftp://user:***@host/.../alis/test.txt".
at org.apache.commons.vfs2.provider.AbstractFileObject.copyFrom(AbstractFileObject.java:1062)
at test.Test.upload(Test.java:73)
... 1 more
Caused by: org.apache.commons.vfs2.FileSystemException: Could not create folder "sftp://user:***@host/.../alis" because it already exists and is a file.
at org.apache.commons.vfs2.provider.AbstractFileObject.createFolder(AbstractFileObject.java:968)
at org.apache.commons.vfs2.provider.AbstractFileObject.getOutputStream(AbstractFileObject.java:1424)
at org.apache.commons.vfs2.provider.DefaultFileContent.getOutputStream(DefaultFileContent.java:461)
at org.apache.commons.vfs2.provider.DefaultFileContent.getOutputStream(DefaultFileContent.java:441)
at org.apache.commons.vfs2.FileUtil.copyContent(FileUtil.java:111)
at org.apache.commons.vfs2.provider.AbstractFileObject.copyFrom(AbstractFileObject.java:1053)
... 2 more
Sourcecode:(要运行该示例,您需要“jsch-0.1.50.jar”)
你提到:
770 rights but no user and no group.
“其他”人无权使用该目录。该目录没有用户或组,因此您的SFTP客户端可能由于权限不足而失败
如果将其分配给用户/组,会发生什么情况?我们使用另一种方式将文件连接并上载到某些SFTP:
public static void main(String[] args) {
putFile("user", "host", "passwd", "/../test.txt", "C:/test.txt");
}
public static void putFile(String username, String host, String password, String remotefile, String localfile){
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(username, host, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.put(localfile, remotefile);
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
这种方法应该适用于任何SSH(SFTP),而不要求任何登录信息或其他阻止信息。我也面临同样的问题。您正在尝试在同一位置创建文件夹,并且文件存在于同名的同一位置 但是为什么它与FileZilla一起工作?我无法更改文件夹的格式。请尝试在FileZilla中使用此用户进行复制/删除,好吗?你有权编辑吗?是的,我可以创建文件夹,创建文件,编辑文件,删除文件夹,用FileZilla ans删除文件同一个用户/密码对不起,有很多问题,但我认为你的代码是正确的。你有权复制本地文件吗?你能测试一下吗?谢谢你的时间。我有权复制localFile(刚刚测试过)。这似乎真的是SFTP在制造truble:/
public static void main(String[] args) {
putFile("user", "host", "passwd", "/../test.txt", "C:/test.txt");
}
public static void putFile(String username, String host, String password, String remotefile, String localfile){
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(username, host, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.put(localfile, remotefile);
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}