Skip to content

Instantly share code, notes, and snippets.

@kommen
Created January 21, 2023 21:32
Show Gist options
  • Save kommen/3777eea0e9bcb97cb46fee817dcd2818 to your computer and use it in GitHub Desktop.
Save kommen/3777eea0e9bcb97cb46fee817dcd2818 to your computer and use it in GitHub Desktop.
(ns cljftp
(:require [clojure.java.io :as io])
(:import [java.net URL]))
;; See also https://www.ietf.org/rfc/rfc1738.txt
;; a public FTP test server https://dlptest.com/ftp-test/
(def ftp-url "ftp://dlpuser:[email protected]/")
(def file-name "test.txt")
(let [file-url (URL. (str ftp-url file-name))
ls-url (URL. (str ftp-url ";type=d"))
up-conn (.openConnection file-url)
down-conn (.openConnection file-url)
list-conn (.openConnection ls-url)]
;; upload
(with-open [os (.getOutputStream up-conn)]
(io/copy "test file contents" os))
;; download
(with-open [is (.getInputStream down-conn)]
(io/copy is (io/file (str "/tmp/" file-name))))
;; remote directory listing
(with-open [is (.getInputStream list-conn)]
(println (slurp is))))
(slurp (str "/tmp/" file-name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment