-
- import java.io.IOException;
- import java.io.OutputStream;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.IOUtils;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
- /**
- * 使用commons的net包进行ftp链接.
- * 相关包:commons-net-1.4.1.jar ; commons-io-1.2.jar;jakarta-oro-2.0.8.jar测试通过.可以列出ftp上的文件
- * 通过把ftp服务器上的文件流连接到outSteam及可以把文件下载到本机的目录..限制如果目录为中文则需要处理.最好使用英文文件名
- * @author xzgf
- * email:
- *
- * @create 2007-2-11
- *
- */
- public class ListFtpFile {
- private FTPClient ftpClient = new FTPClient();
- private OutputStream outSteam = null;
- /**
- * ftp服务器地址
- */
- private String hostName = "127.0.0.1";
- /**
- * 登录名
- */
- private String userName = "1";
- /**
- * 登录密码
- */
- private String password = "1";
- /**
- * 需要访问的远程目录
- */
- private String remoteDir = "/ClientTest";
- /**
- * 登录方法
- *
- */
- private void login() {
- try {
- //链接到ftp服务器
- ftpClient.connect(hostName);
- System.out.println("连接到ftp服务器:" + hostName + " 成功..开始登录");
- //登录.用户名 密码
- ftpClient.login(userName, password);
- System.out.println("登录成功.");
- FTPFile[] remoteFiles = ftpClient.listFiles(remoteDir);
- System.out.println("目录" + remoteDir + "下的文件:");
- if(remoteFiles != null) {
- for(int i=0;i
- String name = remoteFiles[i].getName();
- long length = remoteFiles[i].getSize();
- String readableLength = FileUtils.byteCountToDisplaySize(length);
- System.out.println(name + ":/t/t" + readableLength);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- //使用IO包关闭流
- IOUtils.closeQuietly(outSteam);
- try {
- ftpClient.disconnect();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- ListFtpFile listFtpfiles = new ListFtpFile();
- listFtpfiles.login();
- }
- }
- private JButton getJButton() {
if (jButton == null) {
try {
jButton = new JButton();
jButton.setBounds(new Rectangle(420, 6, 58, 25));
jButton.setText("浏览");
jButton.addActionListener(new java.awt.event.ActionListener() {
//@SuppressWarnings("deprecation")
@SuppressWarnings("deprecation")
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
if(e.getSource()==jButton){
JFrame jf1 = new JFrame();
FileDialog fd1 = new FileDialog(jf1,"选择要发送的文件",FileDialog.LOAD);
fd1.show();
String st = fd1.getDirectory() + fd1.getFile();
if(st!=null){
try {
jTextField.setText(st);
} catch (Exception e1) {
up
最新推荐文章于 2024-04-23 22:51:23 发布