blob: 5053240cb2467ddb45881bcb4a227f0725248d68 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Embedding FtpServer in 5 minutes &mdash; Apache MINA</title>
<link href="/assets/css/common.css" rel="stylesheet" type="text/css"/>
<link href="/assets/css/ftpserver.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<script src="https://www.apachecon.com/event-images/snippet.js"></script>
<div id="container">
<div id="header">
<div id="subProjectsNavBar">
<a href="/">
Apache MINA Project
</a>
&nbsp;|&nbsp;
<a href="/mina-project/">
MINA
</a>
&nbsp;|&nbsp;
<a href="/asyncweb-project/">
AsyncWeb
</a>
&nbsp;|&nbsp;
<a href="/ftpserver-project/">
<strong>FtpServer</strong>
</a>
&nbsp;|&nbsp;
<a href="/sshd-project/">
SSHD
</a>
&nbsp;|&nbsp;
<a href="/vysper-project/">
Vysper
</a>
</div>
</div>
<div id="content">
<div id="leftColumn">
<div id="navigation">
<a class="acevent" data-format="wide" data-width="170"></a>
<h5>Social Networks</h5>
<ul>
<li><a href="https://fosstodon.org/@apachemina">Apache MINA Mastodon</a></li>
</ul>
<h5>Overview</h5>
<ul>
<li><a href="/ftpserver-project/index.html">Home</a> </li>
<li><a href="/ftpserver-project/features.html">Features</a> </li>
<li><a href="/ftpserver-project/download_1_1.html">FtpServer 1.1.4</a></li>
<li><a href="/ftpserver-project/download_1_2.html">FtpServer 1.2.0</a></li>
<li><a href="/ftpserver-project/old-downloads.html">Old Downloads</a></li>
<li><a href="/ftpserver-project/documentation.html">Documentation</a></li>
<li><a href="/ftpserver-project/gen-docs/latest-1.1/apidocs/index.html" class="external-link" rel="nofollow">API Javadoc 1.1.4</a></li>
<li><a href="/ftpserver-project/gen-docs/latest-1.2/apidocs/index.html" class="external-link" rel="nofollow">API Javadoc 1.2.0</a></li>
<li><a href="/ftpserver-project/getting_source.html">Sources</a></li>
<li><a href="/ftpserver-project/faq.html">FAQ</a></li>
<li><a href="/ftpserver-project/related_project.html">Related Project</a></li>
</ul>
<h5>Community</h5>
<ul>
<li><a href="/ftpserver-project/mailing_list.html">Mailing Lists</a></li>
<li><a href="/ftpserver-project/getting_involved.html">Getting Involved</a></li>
<li><a href="/ftpserver-project/reporting_bug.html">Reporting a Bug</a></li>
<li><a href="/ftpserver-project/contributors.html">Contributors</a></li>
<li><a href="https://www.apache.org/foundation/contributing.html">Contributing</a></li>
<li><a href="https://www.apache.org/licenses/">License</a></li>
<li><a href="https://www.apache.org/security/">Security</a></li>
</ul>
<h5>Sponsorship</h5>
<ul>
<li><a href="https://www.apache.org/foundation/thanks.html">Thanks</a></li>
<li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="https://www.apache.org/">apache.org</a></li>
</ul>
</div>
</div>
<div id="rightColumn">
<h1 id="embedding-ftpserver-in-5-minutes">Embedding FtpServer in 5 minutes</h1>
<p>FtpServer is designed to be easily embedded into your application. Getting a basic server up and running is as simple as</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">FtpServerFactory serverFactory <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> FtpServerFactory<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
FtpServer server <span style="color:#666">=</span> serverFactory<span style="color:#666">.</span><span style="color:#b44">createServer</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#080;font-style:italic">// start the server
</span><span style="color:#080;font-style:italic"></span>server<span style="color:#666">.</span><span style="color:#b44">start</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
</code></pre></div><p>To get this running, you need the following JAR files in your classpath:</p>
<ul>
<li>mina-core, 2.0-M3 or later</li>
<li>slf4j-api</li>
<li>A SLF4J implementation of your choice, for example slf4j-simple-1.5.3.jar</li>
<li>ftplet-api</li>
<li>ftpserver-core</li>
</ul>
<p>Now, you will probably like to configure the server for your specific needs. For example, you might want to run on a non-privileged port to get around running as a root on Linux/Unix. To do that you need to configure a listener. Listeners are the part of FtpServer where network management is done. By default, a listener named &ldquo;default&rdquo; is created but you can add as many listeners as you like, for example to provide one for use outside of your firewall and one on the inside.</p>
<p>Now, let&rsquo;s configure the port on which the default listener waits for connections.</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">FtpServerFactory serverFactory <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> FtpServerFactory<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
ListenerFactory factory <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ListenerFactory<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#080;font-style:italic">// set the port of the listener
</span><span style="color:#080;font-style:italic"></span>factory<span style="color:#666">.</span><span style="color:#b44">setPort</span><span style="color:#666">(</span>2221<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#080;font-style:italic">// replace the default listener
</span><span style="color:#080;font-style:italic"></span>serverFactory<span style="color:#666">.</span><span style="color:#b44">addListener</span><span style="color:#666">(</span><span style="color:#b44">&#34;default&#34;</span><span style="color:#666">,</span> factory<span style="color:#666">.</span><span style="color:#b44">createListener</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#080;font-style:italic">// start the server
</span><span style="color:#080;font-style:italic"></span>FtpServer server <span style="color:#666">=</span> serverFactory<span style="color:#666">.</span><span style="color:#b44">createServer</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
server<span style="color:#666">.</span><span style="color:#b44">start</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
</code></pre></div><p>Now, let&rsquo;s make it possible for a client to use FTPS (FTP over SSL) for the default listener.</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">FtpServerFactory serverFactory <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> FtpServerFactory<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
ListenerFactory factory <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ListenerFactory<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#080;font-style:italic">// set the port of the listener
</span><span style="color:#080;font-style:italic"></span>factory<span style="color:#666">.</span><span style="color:#b44">setPort</span><span style="color:#666">(</span>2221<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#080;font-style:italic">// define SSL configuration
</span><span style="color:#080;font-style:italic"></span>SslConfigurationFactory ssl <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> SslConfigurationFactory<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
ssl<span style="color:#666">.</span><span style="color:#b44">setKeystoreFile</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> File<span style="color:#666">(</span><span style="color:#b44">&#34;src/test/resources/ftpserver.jks&#34;</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
ssl<span style="color:#666">.</span><span style="color:#b44">setKeystorePassword</span><span style="color:#666">(</span><span style="color:#b44">&#34;password&#34;</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#080;font-style:italic">// set the SSL configuration for the listener
</span><span style="color:#080;font-style:italic"></span>factory<span style="color:#666">.</span><span style="color:#b44">setSslConfiguration</span><span style="color:#666">(</span>ssl<span style="color:#666">.</span><span style="color:#b44">createSslConfiguration</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
factory<span style="color:#666">.</span><span style="color:#b44">setImplicitSsl</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">true</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#080;font-style:italic">// replace the default listener
</span><span style="color:#080;font-style:italic"></span>serverFactory<span style="color:#666">.</span><span style="color:#b44">addListener</span><span style="color:#666">(</span><span style="color:#b44">&#34;default&#34;</span><span style="color:#666">,</span> factory<span style="color:#666">.</span><span style="color:#b44">createListener</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
PropertiesUserManagerFactory userManagerFactory <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> PropertiesUserManagerFactory<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
userManagerFactory<span style="color:#666">.</span><span style="color:#b44">setFile</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> File<span style="color:#666">(</span><span style="color:#b44">&#34;myusers.properties&#34;</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
serverFactory<span style="color:#666">.</span><span style="color:#b44">setUserManager</span><span style="color:#666">(</span>userManagerFactory<span style="color:#666">.</span><span style="color:#b44">createUserManager</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#080;font-style:italic">// start the server
</span><span style="color:#080;font-style:italic"></span>FtpServer server <span style="color:#666">=</span> serverFactory<span style="color:#666">.</span><span style="color:#b44">createServer</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
server<span style="color:#666">.</span><span style="color:#b44">start</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
</code></pre></div><p>There you have it, that&rsquo;s the basics that you usually need. For more advanced features, have a look at our configuration documentation.</p>
</div>
<div id="endContent"></div>
</div>
<div id="footer">
&copy; 2003-2024, <a href="https://www.apache.org">The Apache Software Foundation</a> - <a href="https://privacy.apache.org/policies/privacy-policy-public.html">Privacy Policy</a><br />
Apache MINA, MINA, Apache Vysper, Vysper, Apache SSHd, SSHd, Apache FtpServer, FtpServer, Apache AsyncWeb, AsyncWeb,
Apache, the Apache feather logo, and the Apache Mina project logos are trademarks of The Apache Software Foundation.
</div>
</div>
</body>
</html>