This guide covers the implementation of file upload and download operations using FastDFS with Java client.
Prerequisites
Before proceeeding, ensure you have FastDFS server deployed and running. You need to obtain the fastdfs-client-java library either by downloading the pre-built JAR or building from source.
Configuration Setup
Create a client configuration file named fdfs_client.conf:
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 80
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 192.168.206.139:22122
Replace the tracker server IP address with your actual FastDFS tracker server address.
Java Implementation
The following Java class demonstrates upload and download operations:
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
import org.apache.commons.io.IOUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test;
public class FastDFSOperations {
private static final String CONFIG_PATH = "src/test/resources/fdfs_client.conf";
private static final String TEST_IMAGE_PATH = "D:\\test_images\\sample.jpg";
@Test
public void testUpload() {
try {
ClientGlobal.init(CONFIG_PATH);
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String[] uploadResult = storageClient.upload_file(TEST_IMAGE_PATH, "jpg", null);
if (uploadResult != null) {
System.out.println("File uploaded successfully.");
System.out.println("Group: " + uploadResult[0]);
System.out.println("Path: " + uploadResult[1]);
}
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
@Test
public void testDownload() {
try {
ClientGlobal.init(CONFIG_PATH);
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String groupName = "group1";
String remotePath = "M00/00/00/wKjOi1gi0nOAcSo8AAA63pKOZZ0312.jpg";
byte[] fileContent = storageClient.download_file(groupName, remotePath);
if (fileContent != null) {
String outputFileName = UUID.randomUUID().toString() + ".jpg";
String outputPath = "D:/downloads/" + outputFileName;
IOUtils.write(fileContent, new FileOutputStream(outputPath));
System.out.println("File downloaded to: " + outputPath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Common Connection Issues
When testing, you may encounter connection timeout errors:
connect to server 192.168.206.139:22122 fail
java.net.SocketTimeoutException: connect timed out
This typically occurs because the FastDFS tracker port (22122) and storage port (23000) are blocked by the firewall on the Linux server.
Solution: Configure Firewall Rules
Option 1 - Disable firewall (not recommanded for production):
service iptables stop
Option 2 - Open required ports (recommended):
/sbin/iptables -I INPUT -p tcp --dport 22122 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 23000 -j ACCEPT
/etc/rc.d/init.d/iptables save
service iptables restart
Verification
After resolving firewall issues, execute the upload test. On successful upload, the console displays the file group and path information. You can then access the file through the nginx web server using the returned path to confirm successful upload.
For download operations, verify the file exists at the specified local path after execution completes.