我使用DLFTPClient进行sftp上传。这是我的代码。
var connection: DLSFTPConnection = DLSFTPConnection(hostname: "192.168.1.1", port: 2222, username: "test", password: "test")
var remoteBasePath = "/test"
var localPath: String = Bundle.main.url(forResource: "test", withExtension: ".jpg")?.absoluteString
var request: DLSFTPRequest?
override func viewDidLoad() {
super.viewDidLoad()
var successBlock = {() -> Void in
DispatchQueue.main.async(execute: {() -> Void in
// login successful
print("loginSuccess")
})
} as? DLSFTPClientSuccessBlock
var failureBlock = {(_ error: Error?) -> Void in
DispatchQueue.main.async(execute: {() -> Void in
print(error)
})
print("error")
} as? DLSFTPClientFailureBlock
connection.connect(successBlock: successBlock, failureBlock: failureBlock)
}
@IBAction func startTapped(_ sender: Any) {
print("sending")
var successBlock = {(_ file: DLSFTPFile, _ startTime: Date, _ finishTime: Date) -> Void in
DispatchQueue.main.async(execute: {() -> Void in
var alertView = UIAlertView(title: "Upload completed", message: "", delegate: nil, cancelButtonTitle: "OK", otherButtonTitles: "")
alertView.show()
})
print("success")
} as? DLSFTPClientFileTransferSuccessBlock
var failureBlock = {(_ error: Error?) -> Void in
DispatchQueue.main.async(execute: {() -> Void in
print(error)
})
print("error")
} as? DLSFTPClientFailureBlock
var localFilename = "test.jpg"
var remotePath: String = URL(fileURLWithPath: remoteBasePath).appendingPathComponent(localFilename).absoluteString
request = DLSFTPUploadRequest(remotePath: remotePath, localPath: localPath, successBlock: successBlock, failureBlock: failureBlock, progressBlock: nil)
connection.submitRequest(request)
}
当我尝试登录时,一切似乎都很好,它会返回成功。但是当我尝试上传文件时,它返回错误:(错误域= SFTPClientErrorDomain代码= 29"本地文件不可读" UserInfo = {NSLocalizedDescription =本地文件不可读})
我的远程基本路径和本地路径有问题吗?非常感谢你。
答案 0 :(得分:2)
我使用.path而不是localPath和remotePath中的.absoluteString修复它。