Ftp上传文件调用java代码

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.net.SocketException;
import java.util.List;

public class FTPmain {
private String ip = "";
private String username = "";
private String password = "";
private int port = -1;
public FTPmain(String serverIP, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
}

public FTPmain(String serverIP, int port, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
this.port = port;
}
/**
* FTP上传单个文件测试
*/
public void getAllFileByUser() {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;

try {
initFtp(ftpClient);
String[] list=ftpClient.listNames();
for(String s:list){
String ss= new String(s.getBytes("ISO-8859-1"),"GBK");
System.out.println(ss);
}

// File srcFile = new File("E:\\test.txt");
// fis = new FileInputStream(srcFile);
// //设置上传目录
// ftpClient.changeWorkingDirectory("/");
// ftpClient.setBufferSize(1024);
// ftpClient.setControlEncoding("GBK");
// //设置文件类型(二进制)
// ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// ftpClient.storeFile("2.txt", fis);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
}

/**
* FTP上传单个文件测试
*/
public void ftpUpload() {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;

try {
initFtp(ftpClient);

File srcFile = new File("E:\\test.txt");
fis = new FileInputStream(srcFile);
//设置上传目录
ftpClient.changeWorkingDirectory("/");
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
//设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile("2.txt", fis);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
}
private void initFtp(FTPClient ftpClient) throws SocketException,
IOException {
ftpClient.connect(ip);
ftpClient.login(username,password);
}

/**
* FTP下载单个文件测试
*/
public void ftpDownload(String uploadUrl,String saveUrl) {
FTPClient ftpClient = new FTPClient();
FileOutputStream fos = null;

try {
initFtp(ftpClient);

String remoteFileName = uploadUrl;
fos = new FileOutputStream(saveUrl);

ftpClient.setBufferSize(1024);
//设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.retrieveFile(remoteFileName, fos);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fos);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值