https://github.com/lightweight-component/aj-ftp
A Lightweight FTP Client
https://github.com/lightweight-component/aj-ftp
ftp ftp-client
Last synced: 12 months ago
JSON representation
A Lightweight FTP Client
- Host: GitHub
- URL: https://github.com/lightweight-component/aj-ftp
- Owner: lightweight-component
- License: apache-2.0
- Created: 2024-06-13T01:00:39.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-25T04:06:41.000Z (almost 2 years ago)
- Last Synced: 2025-01-16T21:25:08.001Z (over 1 year ago)
- Topics: ftp, ftp-client
- Language: Java
- Size: 32.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://central.sonatype.com/artifact/com.ajaxjs/aj-ftp)
[](https://javadoc.io/doc/com.ajaxjs/aj-ftp)
[](https://deepwiki.com/lightweight-component/aj-ftp)
[](http://www.apache.org/licenses/LICENSE-2.0.txt)
[](mailto:frank@ajaxjs.com)
# A Lightweight FTP Client
This is a simple and lightweight FTP component with the following features:
- Simple & Lightweight: Easy to use and minimal in size.
- Based on JDK's `sun.*` extension package: Most standard JDK distributions already include built-in FTP capabilities. However, some environments (such as Android) do not support the `sun.*` packages — that's where this component comes in handy.
- Supports Progress Tracking: Adds the ability to monitor upload/download progress.
## Source code
[Github](https://github.com/lightweight-component/aj-ftp) | [Gitcode](https://gitcode.com/lightweight-component/aj-ftp)
## Usage
Requires Java8+.
### Install
```xml
com.ajaxjs
aj-ftp
1.1
```
Examples of uploading and downloading files via FTP are shown below. The server `speedtest.tele2.net` is a publicly available, free, and anonymous FTP service found online.
```java
public class TestFTP {
@Test
public void testUpload() throws IOException {
UploadFtp client = new UploadFtp("speedtest.tele2.net", 21);
client.login("anonymous", "anonymous");
client.upload("c:\\temp\\re.zip", "/upload/re.zip");
client.closeServer();
}
@Test
public void testDownload() throws IOException {
UploadFtp ftp = new UploadFtp("speedtest.tele2.net", 21);
ftp.login("anonymous", "anonymous");
ftp.getFile("/1KB.zip", "c:\\temp\\re.zip");
}
}
```
## Key Source Code
The main class is `UploadFtp`, which extends the base `FtpClient` class and is used to implement FTP file upload and download functionality.
```java
package com.ajaxjs.net.ftp;
import com.ajaxjs.net.ftp.sun.TelnetInputStream;
import com.ajaxjs.net.ftp.sun.TelnetOutputStream;
import com.ajaxjs.net.ftp.sun.ftp.FtpClient;
import java.io.*;
import java.nio.file.Files;
/**
* FTP 文件上传
*/
public class UploadFtp extends FtpClient {
public UploadFtp(String server, int port) throws IOException {
super(server, port);
}
/**
* 用书上传本地文件到 FTP 服务器上
*
* @param source 上传文件的本地路径
* @param target 上传到 FTP 的文件路径
*/
public void upload(String source, String target) {
try {
binary();
try (TelnetOutputStream ftp = put(target);
InputStream file = Files.newInputStream(new File(source).toPath())) {
BufferedInputStream in = new BufferedInputStream(file);
new ProgressListener().copy(in, new BufferedOutputStream(ftp), in.available());
System.out.print("put file suc from " + source + " to " + target + "\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 从 FTP 上下载所需要的文件
*
* @param source 在 FTP 上路径及文件名
* @param target 要保存的本地的路径
*/
public void getFile(String source, String target) {
try {
binary();
try (TelnetInputStream ftp = get(source);
OutputStream file = Files.newOutputStream(new File(target).toPath())) {
ProgressListener listener = new ProgressListener();
listener.setFileName(target);
listener.copy(new BufferedInputStream(ftp), new BufferedOutputStream(file), getFileSize(source, ftp));
System.out.print("get file suc from " + source + " to " + target + "\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 为了计算下载速度和百分比,读取 FTP 该文件的大小
*/
private int getFileSize(String source, TelnetInputStream ftp) throws IOException {
// 这里的组合使用是必须得 sendServer 后到 readServerResponse
sendServer("SIZE " + source + "\r\n");
if (readServerResponse() == 213) {
String msg = getResponseString();
try {
return Integer.parseInt(msg.substring(3).trim());
} catch (Exception e) {
}
}
return 0;
}
}
```
Tutorial(Chinese): https://blog.csdn.net/zhangxin09/article/details/134222511.