java common net ftp,Java使用commons-net实现FTP文件上传

将文件或者图片保存在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();

}

}

测试

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

}

}

}

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com

特别注意:本站所有转载文章言论不代表本站观点!

本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值