ftp uploadFileAction(重要)
TelnetOUtputStream os = ftpClient.put(filename);
File file_in = new File(localPath);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
- /**
- * FTP文件上传与下载
- * notice:
- * 之所以每次都要连接一次ftp是让它的目录重新返回到相对的根目录,
- * 如果复用上次的FtpClient则可能它当前在FTP的目录不是我们想要的
- * 目录,所以在FTP上传下载文件时,最好每次都重新登录一下FTP
- * @author lgh
- */
- public class FTPClient {
- private FtpClient ftpClient;
- private String ip;
- private int port;
- private String username;
- private String password;
- public FTPClient() {
- }
- public FTPClient(String ip, int port, String username, String password) {
- this.ip = ip;
- this.port = port;
- this.username = username;
- this.password = password;
- }
- /**
- * 需要备份的文件
- * @param list
- * @return
- */
- private List needBackFile(List list, String relativeName) {
- List fileNames = new ArrayList();
- for (int i = 0; i < list.size(); i++) {
- String temp = (String) list.get(i);
- if (temp.indexOf(relativeName) > 0) {
- fileNames.add(temp);
- }
- }
- return fileNames;
- }
- public static void main(String[] args) {
- FTPClient client = new FTPClient(".....", 21, "...", "....");
- try {
- // client.downloadFile("CRM/ccbcrm/", "D://", "CRMClientLog.log", "CRMClientLog.log");
- // client.uploadFile("", "D://", "CRMClientLog.log");
- List list = client.getList("csrtestftp/网络/", false);
- } catch (Exception ex) {
- Logger.getLogger(FTPClient.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- /**
- * 关闭FTP连接
- * @throws java.lang.Exception
- */
- public void closeServer() throws Exception {
- ftpClient.closeServer();
- }
- /**
- * 连接ftp服务器
- * @param ip
- * @param port
- * @param user
- * @param pwd
- * @return
- * @throws Exception
- */
- public boolean connectServer(String ip, int port, String user, String pwd)
- throws Exception {
- boolean isSuccess = false;
- try {
- ftpClient = new FtpClient();
- ftpClient.openServer(ip, port);
- ftpClient.login(user, pwd);
- isSuccess = true;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return isSuccess;
- }
- /**
- * 获得远程下的目录
- * @param remotePath 远程目录
- * @param fullPath 是否需要完整路径
- * @return
- */
- public List getList(String remotePath, boolean fullPath) {
- List list = new ArrayList();
- try {
- if (connectServer(ip, port, username, password)) {
- BufferedReader br = new BufferedReader(new InputStreamReader(ftpClient.nameList(remotePath)));
- String temp = ftpClient.getResponseString();
- System.out.println(temp);
- String readLine = null;
- int lastIndex;
- if ((lastIndex = remotePath.lastIndexOf("/")) > 0||(lastIndex = remotePath.lastIndexOf("//")) > 0
- ||(lastIndex = remotePath.lastIndexOf("\\")) > 0||(lastIndex = remotePath.lastIndexOf(File.separator)) > 0) {
- remotePath = remotePath.substring(0, lastIndex);
- } //去掉remotePath的后缀,可能是'/',也有可能是其他符号
- while ((readLine = br.readLine()) != null) {
- if (!fullPath) {
- list.add(readLine.substring(remotePath.length() + 1, readLine.length()));
- System.out.println(readLine.substring(remotePath.length() + 1, readLine.length()));
- } else {
- list.add(readLine);
- System.out.println(readLine);
- }
- }
- ftpClient.closeServer();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return list;
- }
- /**
- * 下载文件
- * @param remotePath
- * @param localPath
- * @param filename
- * @throws Exception
- */
- public void downloadFile(String remotePath, String localPath, String remoteFileName, String localFileName) throws Exception {
- try {
- if (connectServer(ip, port, username, password)) {
- if (remotePath.length() != 0) {
- ftpClient.cd(remotePath);
- }
- ftpClient.binary();
- TelnetInputStream is = ftpClient.get(remoteFileName);
- File fp = new File(localPath);
- if (!fp.exists()) {
- fp.mkdirs();
- }
- File file_out = new File(localPath + File.separator + localFileName);
- FileOutputStream os = new FileOutputStream(file_out);
- byte[] bytes = new byte[1024];
- int readBye;
- while ((readBye = is.read(bytes)) != -1) {
- os.write(bytes, 0, readBye);
- }
- is.close();
- os.close();
- ftpClient.closeServer();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- /**
- * 上传文件
- * @param remotePath
- * @param localPath
- * @param filename
- * @throws Exception
- */
- public void uploadFile(String remotePath, String localPath, String filename) throws Exception {
- try {
- if (connectServer(ip, port, username, password)) {
- if (remotePath.length() != 0) {
- ftpClient.cd(remotePath);
- }
- ftpClient.binary();
- TelnetOutputStream os = ftpClient.put(filename);
- File file_in = new File(localPath + File.separator + filename);
- FileInputStream is = new FileInputStream(file_in);
- byte[] bytes = new byte[1024];
- int readBye;
- while ((readBye = is.read(bytes)) != -1) {
- os.write(bytes, 0, readBye);
- }
- is.close();
- os.close();
- ftpClient.closeServer();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- /**
- * @return the ip
- */
- public String getIp() {
- return ip;
- }
- /**
- * @param ip the ip to set
- */
- public void setIp(String ip) {
- this.ip = ip;
- }
- /**
- * @return the port
- */
- public int getPort() {
- return port;
- }
- /**
- * @param port the port to set
- */
- public void setPort(int port) {
- this.port = port;
- }
- /**
- * @return the username
- */
- public String getUsername() {
- return username;
- }
- /**
- * @param username the username to set
- */
- public void setUsername(String username) {
- this.username = username;
- }
- /**
- * @return the password
- */
- public String getPassword() {
- return password;
- }
- /**
- * @param password the password to set
- */
- public void setPassword(String password) {
- this.password = password;
- }
- }
ftp uploadFileAction(重要)的更多相关文章
- 8.仿阿里云虚拟云服务器的FTP(包括FTP文件夹大小限制)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#iis 原文:http://dnt.dkill.net/Ar ...
- Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...
- 阿里云学生优惠Windows Server 2012 R2安装IIS,ftp等组件,绑定服务器域名,域名解析到服务器,域名备案,以及安装期间错误的解决方案
前言: 这几天终于还是按耐不住买了一个月阿里云的学生优惠.只要是学生,在学信网上注册过,并且支付宝实名认证,就可以用9块9的价格买阿里云的云服务ECS.确实是相当的优惠. 我买的是Windows S ...
- win7下利用ftp实现华为路由器的上传和下载
win7下利用ftp实现华为路由器的上传和下载 1. Win7下ftp的安装和配置 (1)开始->控制面板->程序->程序和功能->打开或关闭Windows功能 (2)在Wi ...
- Java实现FTP文件与文件夹的上传和下载
Java实现FTP文件与文件夹的上传和下载 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制 ...
- centos下开启ftp服务
如果要ftp访问linux需要安装ftp服务,vsftpd是Linux下比较好的的FTP服务器. 一.检查安装vsftp //检查是否安装vsftpd rpm -qa | grep vsftpd // ...
- 解决开启服务器防火墙导致ftp不能连接的问题
在防火墙设置的"高级"选项卡中的"网络连接设置"--"本地连接"--"设置"中添加了"FTP服务器" ...
- centos6.5 nginx-1.8.0和ftp搭建图片服务器
一.Nginx的安装步骤 1.Nginx安装环境: gcc: 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c+ ...
- Jenkins配置MSBuild实现自动部署(MSBuild+SVN/Subversion+FTP+BAT)
所要用到的主要插件: [MSBuild Plugin] 具体操作: 1.配置MSBuild的版本 [系统管理]->[Global Tool Configuration]->[MSBuild ...
随机推荐
- ubuntu16.04 登录密码破解方法
1:开机按Shift键,出现如下界面.(手速要快,Shift键要按时间久一点) 选择第二项 2:按回车键进入如下界面,然后选中有recovery mode的选项(第三项) 3:按e进入如下界面,并找到 ...
- Linux GPRS模块问题
这是一个硬件问题,不过被我这个学软件的给遇到了.很尴尬,纠结了很久. GPRS模块如果没有插上sim卡,开机之后一切正常.一旦插上卡之后开机大约过十秒钟之后会自动关机.并在串口上面打印一下信息: II ...
- HTC相关开发所需SDK等工具都在这里了。 【转】
OpenVR SDK https://github.com/ValveSoftware/openvr OpenVR SDK是由原本的SteamWorks SDK更新而来,新增对HTC VIVE开发者 ...
- Hadoop Trash回收站使用指南
转载:https://blog.csdn.net/sunnyyoona/article/details/78869778 我们在删除一个文件时,遇到如下问题,提示我们不能删除文件放回回收站: sudo ...
- SVN服务的部署及使用
环境说明 系统版本 CentOS 7.2 x86_64 SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是 ...
- ZOJ 3526 Weekend Party
Weekend Party Time Limit: 2 Seconds Memory Limit: 65536 KB As the only Oni (a kind of fabulous ...
- [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)
BestCoder Sequence Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, ...
- JUC同步器框架
The java.util.concurrent Synchronizer Framework 前提 AQS(java.util.concurrent.locks.AbstractQueuedSync ...
- POJO百度百科
POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称. 使用POJO名称是为了避免和EJB混淆起来, ...
- android Menory 小结
不建议在Activity中使用static 变量,考虑使用Application.当然,static final例外 但Application也不要cache某个Activity使用的View,如果c ...