项目中最近操作ftp进行上传下载,之前很简单的操作,但是却遇到各种问题,经查发现ftp在jdk1.7环境下同jdk1.6略有不同,直接上代码如下:
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUtilByCommonsNet {
private final static Log logger = LogFactory.getLog(FtpUtil.class);
/**
* 获取FTPClient对象
*
* @param ftpHost
* FTP主机服务器
* @param ftpPassword
* FTP 登录密码
* @param ftpUserName
* FTP登录用户名
* @param ftpPort
* FTP端口 默认为21
* @return
*/
public static FTPClient getFTPClient(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
logger.info("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
} else {
logger.info("FTP连接成功。");
}
} catch (SocketException e) {
e.printStackTrace();
logger.info("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
logger.info("FTP的端口错误,请正确配置。");
}
return ftpClient;
}
/*
* 从FTP服务器下载文件
*
* @param ftpHost FTP IP地址
*
* @param ftpUserName FTP 用户名
*
* @param ftpPassword FTP用户名密码
*
* @param ftpPort FTP端口
*
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
*
* @param localPath 下载到本地的位置 格式:H:/download
*
* @param fileName 文件名称
*/
public static void downloadFtpFile(String ftpPath, String localPath) {
FTPClient ftpClient = null;
try {
ftpClient = getFTPClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
// ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setControlEncoding("GB2312");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
//ftpClient.changeWorkingDirectory(ftpPath);
File localFile = new File(localPath);
OutputStream os = new FileOutputStream(localFile);
// ftpClient.retrieveFile(ftpPath, os);
boolean ff = ftpClient.retrieveFile(new String(ftpPath.getBytes("GB2312"),"ISO-8859-1"), os);
System.out.println("上传文件成功"+ff);
os.close();
ftpClient.logout();
ftpClient.disconnect();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
e.printStackTrace();
}
}
/**
* 上传文件
* @param pathname ftp服务保存地址
* @param fileName 上传到ftp的文件名
* @param originfilename 待上传文件的名称(绝对地址) *
* @return
*/
public static void uploadFile( String pathname, String fileName,String originfilename){
InputStream inputStream = null;
FTPClient ftpClient = null;
try{
System.out.println("开始上传文件");
inputStream = new FileInputStream(new File(originfilename));
ftpClient = getFTPClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
ftpClient.changeWorkingDirectory(pathname);
boolean ff;
ff = ftpClient.storeFile(new String(fileName.getBytes("GB2312"),"ISO-8859-1"),inputStream);
//ff = ftpClient.storeFile(new String(pathname.getBytes("GB2312")), inputStream);
System.out.println("上传文件成功"+ff);
inputStream.close();
ftpClient.logout();
}catch (Exception e) {
System.out.println("上传文件失败");
e.printStackTrace();
}finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
if(null != inputStream){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static String ftpHost = "19.104.240.232";
//private static String ftpHost = "127.0.0.1";
private static String ftpUserName = "ces";
private static String ftpPassword = "ces";
private static int ftpPort = 23221;
public static void main(String[] args) {
// TODO Auto-generated method stub
// String ftpHost = "127.0.0.1";
// String ftpUserName = "ces";
// String ftpPassword = "ces";
// int ftpPort = 23221;
String ftpPath = "/ces/中和.zip";
String localPath = "C:\\111\\中和ssasdhf是否合适地方.zip";
FtpUtilByCommonsNet.uploadFile("/ces","中和是滴哦哈佛is地方-水电费 水电费.zip","C:\\111\\999-2018至2018-1.zip");
FtpUtilByCommonsNet.downloadFtpFile(ftpPath, localPath);
}
}