日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 连接 sftp失败_java – 文件上传到SFTP失败(Apache VFS)

發布時間:2025/3/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 连接 sftp失败_java – 文件上传到SFTP失败(Apache VFS) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我有一個SFTP問題(

Windows

WinSSHD).我嘗試用

Apache Commons VFS在一個文件夾中寫一個文件.在本地SFTP上我沒有上傳的問題,但在第二個SFTP上我總是得到以下錯誤.

FTP看起來像這樣:

我需要上傳到文件夾“alis”.奇怪的是它沒有User / Group和770權限.但是,使用FileZilla文件上傳工作正常(使用相同的登錄).

在文件夾“alis”上執行“manager.resolveFile()”(我嘗試上傳到此文件夾)并打印“.getType()”我獲取信息“文件”而不是預期的“文件夾”.

有沒有人知道為什么VFS會將文件夾識別為文件或為什么上傳不起作用?

將文件上載到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

源代碼:(運行示例,您需要“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:123456@domainname.com/resume.pdf

return "sftp://" + username + ":" + password + "@" + hostName + "/"

+ remoteFilePath;

}

}

總結

以上是生活随笔為你收集整理的java 连接 sftp失败_java – 文件上传到SFTP失败(Apache VFS)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。