FTP上传文件
public boolean uploadFile(String fileName) throws IOException {
boolean upload=true;
FileInputStream f=null;
File file = new File(filePath + File.separator + fileName);
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ip, Integer.valueOf(port));
ftpClient.login(userName, passWord);
int replyCode = ftpClient.getReplyCode();
if(FTPReply.isPositiveCompletion(replyCode)){
try {
ftpClient.setBufferSize(1024);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
f=new FileInputStream(file);
ftpClient.storeFile(uploadFilePath + File.separator + file.getName(),f);
} catch (IOException e) {
upload=false;
e.printStackTrace();
}finally {
f.close();
ftpClient.disconnect();
}
}
return upload;
}
FTP获取文件
private FTPClient ftpClient;
private void init() {
try {
ftpClient = new FTPClient();
ftpClient.connect(hostName, Integer.valueOf(port));
ftpClient.login(userName, password);
int replyCode = ftpClient.getReplyCode();
Boolean a = FTPReply.isPositiveCompletion(replyCode);
if (a) {
logger.info("FTP登录成功!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public File getFileIO(String fileName) {
File file = null;
try {
if (ftpClient == null) {
init();
}
Boolean b = ftpClient.changeWorkingDirectory("detail");
if (b) {
logger.info("进入FTP工作目录===========>>" + "detail");
}
File packfile = new File(pathName);
if (!packfile.exists()) {
packfile.mkdirs();
}
String name = pathName + File.separator + fileName;
file = new File(name);
FileOutputStream os = new FileOutputStream(file);
ftpClient.retrieveFile(fileName, os);
os.close();
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}