Skip to content

Instantly share code, notes, and snippets.

@owainlewis
Created April 25, 2016 19:49
Show Gist options
  • Save owainlewis/06e8bdfa6c42acec2ef9dec756c05c2f to your computer and use it in GitHub Desktop.
Save owainlewis/06e8bdfa6c42acec2ef9dec756c05c2f to your computer and use it in GitHub Desktop.
Scala FTP
package io.forward.ftp
import java.io.{File, FileOutputStream, InputStream}
import org.apache.commons.net.ftp._
import scala.util.Try
final class FTP() {
private val client = new FTPClient
def login(username: String, password: String): Try[Boolean] = Try {
client.login(username, password)
}
def connect(host: String): Try[Unit] = Try {
client.connect(host)
client.enterLocalPassiveMode()
}
def connected: Boolean = client.isConnected
def disconnect(): Unit = client.disconnect()
def canConnect(host: String): Boolean = {
client.connect(host)
val connectionWasEstablished = connected
client.disconnect()
connectionWasEstablished
}
def listFiles(dir: Option[String] = None): List[FTPFile] =
dir.fold(client.listFiles)(client.listFiles).toList
def connectWithAuth(host: String,
username: String = "anonymous",
password: String = "") : Try[Boolean] = {
for {
connection <- connect(host)
login <- login(username, password)
} yield login
}
def cd(path: String): Boolean =
client.changeWorkingDirectory(path)
def filesInCurrentDirectory: Seq[String] =
listFiles().map(_.getName)
def downloadFileStream(remote: String): InputStream = {
val stream = client.retrieveFileStream(remote)
client.completePendingCommand()
stream
}
def downloadFile(remote: String): Boolean = {
val os = new FileOutputStream(new File(remote))
client.retrieveFile(remote, os)
}
def uploadFile(remote: String, input: InputStream): Boolean =
client.storeFile(remote, input)
}
Status API Training Shop Blog About
@Duongchantroi
Copy link

I can't use the downloadFile method because I don't understand exactly what the remote is. Please give me an example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment