加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java使用commons-net实现FTP文件上传

发布时间:2020-12-14 23:52:47 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 将文件或者图片保存在FTP服务器上,使用commons-net包。 import java.io.File;import java.io.FileInputStream;import java.io.IOException;import ja

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

将文件或者图片保存在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,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,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,String remoteName) throws Exception {
        changeDirectory(ftpClient,remotePathName);
        uploadFile(ftpClient,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,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,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();
    }
}

测试

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();
        }
    }
}


以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读