Java使用commons-net實現FTP文件上傳

lpki123 10年前發布 | 3K 次閱讀 Java

將文件或者圖片保存在FTP服務器上,使用commons-net包。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile;

public class FTPUploader {

public String username = "";
public String password = "";
public String ip = "";
public Integer port = 21;

public FTPUploader(String username, String password, String ip, int port) {
    this.username = username;
    this.password = password;
    this.ip = ip;
    this.port = port;
}

public void upload(File file, String remoteFileName, String remoteFilePath)
        throws Exception {
    upload(ip, port, username, password, file, remoteFilePath,
            remoteFileName);
}

private void upload(String ip, int port, String userName, String password,
        File file, String remotePathName, String remoteName)
        throws Exception {
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(ip, port);
        ftpClient.login(userName, password);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        upload(ftpClient, file, remotePathName, remoteName);
    } catch (Exception e) {
        throw new Exception("upload to ftp faild");
    } finally {
        if (ftpClient != null && ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
            }
        }
    }
}

private void upload(FTPClient ftpClient, File file, String remotePathName,
        String remoteName) throws Exception {
    changeDirectory(ftpClient, remotePathName);
    uploadFile(ftpClient, file, remotePathName, remoteName);
    backToRootDirectory(ftpClient);
}

private void changeDirectory(FTPClient ftpClient, String path)
        throws IOException {
    int nextSeperator = path.indexOf("/", 1);
    String currentPath = null;
    if (nextSeperator < 0) {
        nextSeperator = path.length();
        currentPath = path.substring(1, nextSeperator);
        changeDirectory0(ftpClient, currentPath);
        return;
    } else {
        currentPath = path.substring(1, nextSeperator);
        changeDirectory0(ftpClient, currentPath);
        changeDirectory(ftpClient, path.substring(nextSeperator));
    }
}

private void changeDirectory0(FTPClient ftpClient, String path)
        throws IOException {
    if (!ftpClient.changeWorkingDirectory(path)) {
        ftpClient.makeDirectory(path);
    }
    ftpClient.changeWorkingDirectory(path);
}

private void backToRootDirectory(FTPClient ftpClient) throws IOException {
    ftpClient.changeWorkingDirectory("/");
}

private void uploadFile(FTPClient ftpClient, File file,
        String remotePathName, String remoteName) throws Exception {
    String localPathName = file.getAbsolutePath();
    FTPFile[] files = ftpClient.listFiles(remoteName);
    if (files.length == 1) {
        if (!ftpClient.deleteFile(remoteName)) {
            throw new Exception("fail to delete remote file ["
                    + remotePathName + "] before uploading");
        }
    }
    File f = new File(localPathName);
    InputStream is = new FileInputStream(f);
    if (!ftpClient.storeFile(remoteName, is)) {
        is.close();
    }
    is.close();
}

}</pre>

測試

import java.io.File;

import org.junit.Before; import org.junit.Test;

import com.isharec.jutils.ftp.FTPUploader;

public class FTPUploaderTest {

private FTPUploader ftpUploader;

@Before
public void before() {
    ftpUploader = new FTPUploader("username", "password",
            "ip", 21);
}

@Test
public void testUploader() {
    File f = new File("E:\\123.txt");
    try {
        ftpUploader.upload(f, "123.txt", "/aaa/test/");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}</pre>

 本文由用戶 lpki123 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!