博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jakarta Commons NET(FTPClient)的简单示例
阅读量:2816 次
发布时间:2019-05-13

本文共 4142 字,大约阅读时间需要 13 分钟。

java中使用FTP传送文件或者取得文件,可以使用Jakarta Commons NET(FTPClient)的包来实现。

具体的示例如下:(例子是从网上拷贝的)

package test.ftp;            import java.io.FileInputStream;      import java.io.FileOutputStream;      import java.io.IOException;      import java.io.InputStream;      import java.io.OutputStream;            import org.apache.commons.net.ftp.FTP;      import org.apache.commons.net.ftp.FTPClient;      import org.apache.commons.net.ftp.FTPReply;                  public class FtpClientUtil {          private static final int FTP_PORT = 21;                    public static void main(String[] args) {              try {                  //读入文件                  FileInputStream fis = new FileInputStream("c:\testftp.txt");                  //传送文件到FTP服务器                FtpClientUtil.sendFile("localhost", FTP_PORT, "testuser", "testpassword",   "remoteFilename", fis);                                    //从FTP服务器取得文件                  FileOutputStream fos = new FileOutputStream("localfile");                   FtpClientUtil.retrieveFile("localhost", FTP_PORT, "testuser", "testpassword",   "remoteFilename", fos);                                } catch (Exception e) {                  e.printStackTrace();              }          }                //上传文件          public static void sendFile (String host,                   int port,                  String user,                  String password,                  String remoteFilename,                  InputStream is                  ) throws Exception {              FTPClient ftpclient = new FTPClient();                            try {                  //设置服务器名和端口                ftpclient.connect(host, port);                  int reply = ftpclient.getReplyCode();                  if (!FTPReply.isPositiveCompletion(reply)) {                      //连接错误的时候报错。                     Exception ee = new Exception("Can't Connect to :" + host);                      throw ee;                  }                                    //登录                  if (ftpclient.login(user, password) == false) {                      // invalid user/password                      Exception ee = new Exception("Invalid user/password");                      throw ee;                  }                        //设置传送文件模式                  ftpclient.setFileType(FTP.BINARY_FILE_TYPE);                                    //传送文件                  ftpclient.storeFile(remoteFilename, is);                                            } catch (IOException e) {                  throw e;              } finally {                  try {                      ftpclient.disconnect(); //解除连接                  } catch (IOException e) {                  }              }                        }                    //文件下载          public static void retrieveFile(String host,                   int port,                  String user,                  String password,                  String remoteFilename,                  OutputStream os) throws Exception {              FTPClient ftpclient = new FTPClient();                            try {                  //设置服务器名和端口                  ftpclient.connect(host, port);                  int reply = ftpclient.getReplyCode();                  if (!FTPReply.isPositiveCompletion(reply)) {                      //连接错误                      Exception ee = new Exception("Can't Connect to :" + host);                      throw ee;                  }                                    //登录                  if (ftpclient.login(user, password) == false) {                      // invalid user/password                      Exception ee = new Exception("Invalid user/password");                      throw ee;                  }                        //设置传送模式                 ftpclient.setFileType(FTP.BINARY_FILE_TYPE);                                    // 取得文件                  ftpclient.retrieveFile(remoteFilename, os);                    } catch (IOException e) {                  throw e;              } finally {                  try {                      ftpclient.disconnect(); //解除连接                 } catch (IOException e) {                  }              }          }      }

转载地址:http://zazhd.baihongyu.com/

你可能感兴趣的文章
webservice之实现一个基于JWS的webservice项目
查看>>
java 实现WebService 以及不同的调用方式
查看>>
JDK1.5+SSH+Jboss4.2.3应用部署、开发配置
查看>>
java this 关键字
查看>>
javascript 中的&& 与||
查看>>
python django安装报错The read operation timed out(pytz)
查看>>
搭建django cms时遇到的问题
查看>>
easyui combotree 实现勾选checkbox 同时选择所有子节点,取消父节点同时取消所有子节点
查看>>
jboss4 在eclipse 中一直处于starting状态
查看>>
使用virtualenv 常见基于不同版本python的独立环境
查看>>
git的一些命令
查看>>
git找回本地误删的文件
查看>>
eclipse html 格式化
查看>>
怎么判断jdk是32位的还是64位的
查看>>
squid 启用https反向代理代理
查看>>
jquery监听由脚本改变input值的事件
查看>>
ubuntu 16.04.2 中安装eclipse
查看>>
Learning Path : Your mentor to become a machine learning expert
查看>>
Comprehensive learning path – Data Science in Python
查看>>
python numpy学习
查看>>