Java 文件上传到 SFTP 失败 (Apache VFS)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19809931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File upload to SFTP fails (Apache VFS)
提问by Darkpara
I have a problem with a SFTP (Windows with WinSSHD). I try to write a file in a folder with Apache Commons VFS. On a local SFTP I have no problem with the upload but on a second SFTP I always get the error below.
我在使用 SFTP(带有WinSSHD 的Windows )时遇到问题。我尝试使用Apache Commons VFS在文件夹中写入一个文件。在本地 SFTP 上,我上传没有问题,但在第二个 SFTP 上,我总是收到以下错误。
The FTP looks like this:
FTP 看起来像这样:
I need to upload into the folder "alis". What is strange is that it has no User/Group and 770 rights. However, with FileZilla the file upload works fine (same login used).
我需要上传到文件夹“alis”。奇怪的是它没有用户/组和770权限。但是,使用 FileZilla 文件上传工作正常(使用相同的登录名)。
Doing a "manager.resolveFile()" on the Folder "alis" (i try to upload to this folder) and printing the ".getType()" i get the information "File" and not as expected "Folder".
在文件夹“alis”上执行“manager.resolveFile()”(我尝试上传到此文件夹)并打印“.getType()”我得到信息“文件”而不是预期的“文件夹”。
Does anyone have an idea why VFS does recognize the folder as file or why the upload does not work?
有谁知道为什么 VFS 会将该文件夹识别为文件或为什么上传不起作用?
The exception when uploading the file to the SFTP:
将文件上传到 SFTP 时的异常:
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: (to run the example you need "jsch-0.1.50.jar")
源代码:(要运行示例,您需要“jsch-0.1.50.jar”)
import java.io.File;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
/**
*
* @author thbe
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
upload("host", "user", "password", "D:/Test/test.txt", "/../alis/test.txt");
}
public static FileSystemOptions createDefaultOptions()
throws FileSystemException {
// Create SFTP options
FileSystemOptions opts = new FileSystemOptions();
// SSH Key checking
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
// Root directory set to user home
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
// Timeout is count by Milliseconds
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
SftpFileSystemConfigBuilder.getInstance().setPreferredAuthentications(opts, "publickey,keyboard-interactive,password");
return opts;
}
public static void upload(String hostName, String username,
String password, String localFilePath, String remoteFilePath) {
File f = new File(localFilePath);
if (!f.exists()) {
throw new RuntimeException("Error. Local file not found");
}
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
// Create local file object
FileObject localFile = manager.resolveFile(f.getAbsolutePath());
System.out.println("open remote File");
FileObject remoteFile = manager.resolveFile(
createConnectionString(hostName, username, password,
remoteFilePath), createDefaultOptions());
System.out.println("exists:"+remoteFile.exists());
System.out.println("type:"+remoteFile.getType());
System.out.println("URL:"+remoteFile.getURL());
System.out.println("copy to remote File");
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
System.out.println("File upload success");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
}
public static String createConnectionString(String hostName,
String username, String password, String remoteFilePath) {
// result: "sftp://user:[email protected]/resume.pdf
return "sftp://" + username + ":" + password + "@" + hostName + "/"
+ remoteFilePath;
}
}
采纳答案by FiveO
We used another way to connect and upload files to some 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();
}
}
This approach should work with any SSH (SFTP) and not ask for any login information or other blocking stuff.
这种方法应该适用于任何 SSH (SFTP),并且不会要求任何登录信息或其他阻止内容。
回答by Darkpara
You mentioned:
你提到:
770 rights but no user and no group.
"Other" people do not have right to use the directory. The directory does not have user or group, therefore your SFTP client may have failed due to insufficient permission.
“其他”人无权使用该目录。该目录没有用户或组,因此您的 SFTP 客户端可能由于权限不足而失败。
What happens if you assign it a user/group?
如果你给它分配一个用户/组会发生什么?
回答by deadend
I faced the same issue. You are trying to creating folder in same location and file is exists in same location with same name.
我遇到了同样的问题。您正在尝试在同一位置创建文件夹,并且文件存在于同一位置且名称相同。