您的位置:首页 > 编程语言 > PHP开发

FTP 实践

2016-03-02 19:18 696 查看
public class FtpUtil

{

private Logger logger = LoggerFactory.getLogger(getClass());

private String url;

private int port;

private String username;

private String password;

private String remotePath;

private String fileName;

private String localPath;

private String path;

private InputStream input;

/**

* Description: 从FTP服务器下载文件

*

* @param url

* FTP服务器hostname

* @param port

* FTP服务器端口

* @param username

* FTP登录账号

* @param password

* FTP登录密码

* @param remotePath

* FTP服务器上的相对路径

* @param fileName

* 要下载的文件名

* @param localPath

* 下载后保存到本地的路径

* @return

*/

public boolean downFile()

{// String url, int port, String username,String

// password, String remotePath, String fileName,

// String localPath

// 初始表示下载失败

boolean success = false;

// 创建FTPClient对象

FTPClient ftp = new FTPClient();

try

{

int reply;

// 连接FTP服务器

// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器

ftp.connect(url, port);

// 登录ftp

ftp.login(username, password);

reply = ftp.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply))

{

ftp.disconnect();

return success;

} // 转到指定下载目录

ftp.changeWorkingDirectory(remotePath);

// 列出该目录下所有文件

FTPFile[] fs = ftp.listFiles();

// 遍历所有文件,找到指定的文件

for (FTPFile ff : fs)

{

String ftpFileName = StringUtils.unicodeToUtf8(ff.getName());

if (ftpFileName.equals(fileName))

{

// 根据绝对路径初始化文件

File localFile = new File(localPath + "/" + ftpFileName);

// 输出流

OutputStream is = new FileOutputStream(localFile);

// 下载文件

ftp.retrieveFile(ff.getName(), is);

// 删除原文件

ftp.deleteFile(fileName);

is.close();

// 成功下到文件

success = true;

}

}

// 退出ftp

ftp.logout();

} catch (IOException e)

{

logger.error("IOException", e);

} finally

{

if (ftp.isConnected())

{

try

{

ftp.disconnect();

} catch (IOException ioe)

{

ioe.printStackTrace();

}

}

}

return success;

}

/**

* Description: 向FTP服务器上传文件

*

* @param url

* FTP服务器hostname

* @param port

* FTP服务器端口

* @param username

* FTP登录账号

* @param password

* FTP登录密码

* @param path

* FTP服务器保存目录

* @param filename

* 上传到FTP服务器上的文件名

* @param input

* 输入流

* @return 成功返回true,否则返回false

*/

public boolean uploadFile()

{// String url, int port, String

// username,String

// password, String path, String filename,

// InputStream input

// 初始表示上传失败

boolean success = false;

// 创建FTPClient对象

FTPClient ftp = new FTPClient();

try

{

int reply;

// 连接FTP服务器

// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器

ftp.connect(url, port);

// 登录ftp

ftp.login(username, password);

// 看返回的值是不是230,如果是,表示登陆成功

reply = ftp.getReplyCode();

// 以2开头的返回值就会为真

if (!FTPReply.isPositiveCompletion(reply))

{

ftp.disconnect();

return success;

}

// 转到指定上传目录

ftp.changeWorkingDirectory(path);

// 将上传文件存储到指定目录

ftp.storeFile(StringUtils.utf8ToUnicode(fileName), input);

// 关闭输入流

input.close();

// 退出ftp

ftp.logout();

// 表示上传成功

success = true;

} catch (IOException e)

{

logger.error("FTP服务器连接异常!", e);

} finally

{

if (ftp.isConnected())

{

try

{

ftp.disconnect();

} catch (IOException ioe)

{

}

}

}

return success;

}

/**

*/

public static void main(String args[])

{

FtpUtil ftpUtil = new FtpUtil();

System.out.println("123123========");

// 下载FTP服务器上文件示例

ftpUtil.setUrl("12.72.24.14");

ftpUtil.setPort(21);

ftpUtil.setUsername("d14");

ftpUtil.setPassword("123456");

ftpUtil.setRemotePath("/err/filePath/");

ftpUtil.setFileName("df.txt");

ftpUtil.setLocalPath("f://jk//");

ftpUtil.downFile();

// 上传本地文件到FTP服务器上示例

// ftpUtil.setUrl("11.12.1.2");

// ftpUtil.setPort(21);

// ftpUtil.setUsername("sdf");

// ftpUtil.setPassword("sdf");

ftpUtil.setPath("/unibssfilePath/");

ftpUtil.setFileName("shucl_RSP.txt");

String str = "2312|23|01|1231|11|23|3|01|2|123";

String[] strArr = str.split("\\|");

String path = "f://jk//sdf_RSP.txt";

File file = new File(path);

try

{

BufferedWriter ow = new BufferedWriter(new FileWriter(file));

for (String s : strArr)

{

System.out.println(s);

ow.write(s);

ow.write("\r\n");

System.out.println("sdf========11");

}

ow.close();

} catch (Exception e)

{

e.printStackTrace();

}

try

{

ftpUtil.setInput(new FileInputStream(path));

} catch (FileNotFoundException e)

{

e.printStackTrace();

}

ftpUtil.uploadFile();

}

/**

* 获得文件行记录

*

*/

public List<String> getFileLineRecords(File file)

{

List<String> lines = new ArrayList<String>();

FileReader fileReader = null;

try

{

fileReader = new FileReader(file);

} catch (FileNotFoundException e)

{

e.printStackTrace();

return null;

}

BufferedReader bufReader = new BufferedReader(fileReader);

String line = null;

try

{

// 行记录加载

while ((line = bufReader.readLine()) != null)

{

lines.add(line);

}

} catch (IOException e)

{

e.printStackTrace();

return null;

} finally

{

try

{

bufReader.close();

} catch (IOException e)

{

e.printStackTrace();

}

}

// 判断该文件是否有行记录

if (lines.size() > 0)

{

return lines;

} else

{

return null;

}

}

public String getUrl()

{

return url;

}

public void setUrl(String url)

{

this.url = url;

}

public int getPort()

{

return port;

}

public void setPort(int port)

{

this.port = port;

}

public String getUsername()

{

return username;

}

public void setUsername(String username)

{

this.username = username;

}

public String getPassword()

{

return password;

}

public void setPassword(String password)

{

this.password = password;

}

public String getRemotePath()

{

return remotePath;

}

public void setRemotePath(String remotePath)

{

this.remotePath = remotePath;

}

public String getFileName()

{

return fileName;

}

public void setFileName(String fileName)

{

this.fileName = fileName;

}

public String getLocalPath()

{

return localPath;

}

public void setLocalPath(String localPath)

{

this.localPath = localPath;

}

public String getPath()

{

return path;

}

public void setPath(String path)

{

this.path = path;

}

public InputStream getInput()

{

return input;

}

public void setInput(InputStream input)

{

this.input = input;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: