javaftp重命名⽂件_java中ftp上传、下载、重命名、删除功能
实现
与尼尔森数据对接使⽤了ftp技术,特别留下来作为备⽤。哎,弄完这个深知java遮掩的东西太多了,什么都是调⽤调⽤就完了,得去深⼊了解下底层东西。
package com.voole.util;
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 org.apachemons.ftp.FTP;
import org.apachemons.ftp.FTPClient;
import org.apachemons.ftp.FTPFile;
import org.apachemons.ftp.FTPReply;
import org.apache.log4j.Logger;
public class FtpUtil {
private static FTPClient ftpClient = new FTPClient();
private static String encoding = Property("ding");
private static FtpUtil instance;
Logger log = Logger(FtpUtil.class);
/**
* 实例化FtpUtil
* @return
*/
public synchronized static FtpUtil getInstance(){
if( instance == null ){
instance = new FtpUtil();
}
return instance;
}
/**
*/
public boolean login(String url, int port, String username,
String password){
try {
int reply;
ftpClient.login(username, password); // 登录
ftpClient.setControlEncoding(encoding);
reply = ReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) { // 检验是否连接成功
log.info("FTP连接失败");
ftpClient.disconnect();
return false;
}
log.info("FTP连接成功");
}catch (IOException e) {
e.printStackTrace();
closeCon(); //关闭
}
return true;
}
/**
* Description: 向FTP服务器上传⽂件
*
* @Version1.0
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端⼝
* @param username
* FTP登录账号
* @param password
* @param path
* FTP服务器保存⽬录,如果是根⽬录则为“/”
* @param filename
* 上传到FTP服务器上的⽂件名
* @param input
* 本地⽂件输⼊流
* @return 成功返回true,否则返回false
*/
public boolean uploadFile(String ftpPath,String filePath,String fileName) {
boolean result = false;
try {
if( ftpClient!=null ){
FileInputStream input = new FileInputStream(new File(filePath+fileName));
boolean change = ftpClient.changeWorkingDirectory(ftpPath); // 转移⼯作⽬录⾄指定⽬录下ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
if (change) {
log.info("成功切换⽬录到"+ftpPath);
result = ftpClient.storeFile(new Bytes("GBK"),"iso-8859-1"), input);
if (result) {
log.info(fileName+",上传成功!");
}else{
log.info(fileName+",上传失败");
}
}
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 将本地⽂件上传到FTP服务器上
*
public static void testUpLoadFromDisk(String url, int port, String username, String password, String path, String filename,String filepath) {
try {
FileInputStream in = new FileInputStream(new File(filepath));
boolean flag = uploadFile(url, port, username,password, path, filename, in); System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/
/**
* 删除FTP上的⽂件
*/
public static boolean removeFile(String srcFname){
boolean flag = false;
if( ftpClient!=null ){
try {
flag = ftpClient.deleteFile(srcFname);
} catch (IOException e) {
e.printStackTrace();
closeCon();
}
}
return flag;
}
/
**
* 改名FTP上的⽂件
*/
public static boolean renameFile(String srcFname,String targetFname){ boolean flag = false;
if( ftpClient!=null ){
flag = ame(srcFname,targetFname);
} catch (IOException e) {
e.printStackTrace();下载apache
closeCon();
}
}
return flag;
}
/**
*
销毁ftp连接
*/
public static void closeCon(){
if(ftpClient !=null){
if(ftpClient.isConnected()){
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 下载单个⽂件
*/
public static boolean downloadFile(String ftpPath,String filePath,String fileName){
try {
if( ftpClient!=null ){
FileInputStream input = new FileInputStream(new File(filePath+fileName));
boolean change = ftpClient.changeWorkingDirectory(ftpPath); // 转移⼯作⽬录⾄指定⽬录下ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论