怎样用java开发ftp客户端
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样用java开发ftp客户端相关的知识,希望对你有一定的参考价值。
要做毕业设计,希望能提供详细的参考资料,源码,有教程最好.
Java基本编程懂,但是网络相关的没怎么做过,所以要找入门教程,程序范例.网上搜的太杂,没一个好使的.
毕业设计催得又紧,只能求助各位大虾了.
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.DefaultListModel;
import javax.swing.JTree;
import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpLoginException;
public class Ttt
extends JFrame
implements ActionListener
FtpClient ftp = null;
private List list = new List();
private JPanel FtpClientFrame = new JPanel(new BorderLayout());
private JPanel FtpClientFrameOne = new JPanel(new FlowLayout(FlowLayout.
LEFT));
private JPanel FtpClientFrameTwo = new JPanel(new GridLayout(1, 8));
private JPanel FtpClientFrameThree = new JPanel(new GridLayout(2, 1));
private JPanel FtpClientFrameFour = new JPanel(new GridLayout(1, 2));
//连接、断开按钮
private JButton linkButton = new JButton("Link");
private JButton breakButton = new JButton("Break");
//连接状态
private JLabel statusLabel = new JLabel();
//用户登录
private JLabel urlLabel = new JLabel("Ftp URL:");
private JLabel usernameLabel = new JLabel("username:");
private JLabel passwordLabel = new JLabel("password:");
private JLabel portLabel = new JLabel("port:");
private JTextField urlTextField = new JTextField(10);
private JTextField usernameTextField = new JTextField(10);
private JTextField passwordTextField = new JTextField(10);
private JTextField portTextField = new JTextField(10);
//本地、远程窗口
DefaultListModel modelList = new DefaultListModel();
private JList localList = new JList();
private JList distanceList = new JList();
JScrollPane localScrollPane = new JScrollPane(localList);
JScrollPane distanceScrollPane = new JScrollPane(distanceList);
//本地、远程目录树
private JTree localTree;
private JTree ServerTree;
public Ttt()
FtpClientFrameOne.add(linkButton);
FtpClientFrameOne.add(breakButton);
FtpClientFrameOne.add(statusLabel);
FtpClientFrameTwo.add(urlLabel);
FtpClientFrameTwo.add(urlTextField);
FtpClientFrameTwo.add(usernameLabel);
FtpClientFrameTwo.add(usernameTextField);
FtpClientFrameTwo.add(passwordLabel);
FtpClientFrameTwo.add(passwordTextField);
FtpClientFrameTwo.add(portLabel);
FtpClientFrameTwo.add(portTextField);
FtpClientFrameThree.add(FtpClientFrameOne);
FtpClientFrameThree.add(FtpClientFrameTwo);
FtpClientFrameFour.add(localScrollPane);
FtpClientFrameFour.add(list);
FtpClientFrame.add(FtpClientFrameThree, "North");
FtpClientFrame.add(FtpClientFrameFour, "Center");
setContentPane(FtpClientFrame);
setTitle("Ftp客户端");
setSize(600, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
linkButton.addActionListener(this);
breakButton.addActionListener(this);
public String getDir(String path)
String dirName;
int ch;
int begin = 55;
dirName = path.substring(begin).trim();
return dirName;
public void loadList()
StringBuffer buf = new StringBuffer();
int ch;
list.removeAll();
try
TelnetInputStream t = ftp.list();
t.setStickyCRLF(true);
while ( (ch = t.read()) >= 0)
if (ch == '\n')
list.add(getDir(buf.toString()));
buf.setLength(0);
else
buf.append( (char) ch);
catch (IOException e)
e.printStackTrace();
list.validate();
public void actionPerformed(ActionEvent evt)
Object source = evt.getSource();
if (source == linkButton)
//连接Ftp服务器
try
if (ftp != null)
ftp.closeServer();
statusLabel.setText("连接中,请等待.....");
ftp = new FtpClient(urlTextField.getText());
ftp.login(usernameTextField.getText(),
passwordTextField.getText());
ftp.binary();
catch (FtpLoginException e)
JOptionPane.showMessageDialog(null, "Login Failure!!!");
e.printStackTrace();
catch (IOException e)
JOptionPane.showMessageDialog(null,
urlTextField.getText() + "Connection Failure!!!");
e.printStackTrace();
catch (SecurityException e)
JOptionPane.showMessageDialog(null, "No Purview!!!");
e.printStackTrace();
if (urlTextField.getText().equals(""))
JOptionPane.showMessageDialog(null, "Ftp服务器地址不能空!!!");
else if (usernameTextField.getText().equals(""))
JOptionPane.showMessageDialog(null, "用户名不能为空!!!");
else if (passwordTextField.getText().equals(""))
JOptionPane.showMessageDialog(null, "密码不能为空!!!");
else
statusLabel.setText("已连接到Ftp:" + urlTextField.getText());
loadList();
if (source == breakButton)
System.exit(0);
public static void main(String[] args)
// TODO Auto-generated method stub
Ttt ftpClientFrame = new Ttt();
收集的一些代码,忘记从拿来的了` 你可以看看 参考技术A package zn.ccfccb.util;
import hkrt.b2b.view.util.Log;
import hkrt.b2b.view.util.ViewUtil;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import zn.ccfccb.util.CCFCCBUtil;
import zn.ccfccb.util.ZipUtilAll;
public class CCFCCBFTP
/**
* 上传文件
*
* @param fileName
* @param plainFilePath 明文文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1)
bos.write(buffer, 0, count);
bos.flush();
Log.info("加密上传文件开始");
Log.info("连接远程上传服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
// Log.info("连接远程上传服务器"+"192.168.54.106:"+2021);
// ftpClient.connect("192.168.54.106", 2021);
// ftpClient.login("hkrt-CCFCCBHK", "3OLJheziiKnkVcu7Sigz");
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs)
if (ff.getName().equals(filepath))
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
Log.info("检查文件路径是否存在:/"+filepath);
if("false".equals(bl))
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath);
return bl;
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/");
return bl;
catch (Exception e)
throw e;
finally
if (fis != null)
try
fis.close();
catch (Exception e)
Log.info(e.getLocalizedMessage(), e);
if (bos != null)
try
bos.close();
catch (Exception e)
Log.info(e.getLocalizedMessage(), e);
/**
*下载并解压文件
*
* @param localFilePath
* @param fileName
* @param routeFilepath
* @return
* @throws Exception
*/
public static String fileDownloadByFtp(String localFilePath, String fileName,String routeFilepath) throws Exception
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
FTPClient ftpClient = new FTPClient();
String SFP = System.getProperty("file.separator");
String bl = "false";
try
Log.info("下载并解密文件开始");
Log.info("连接远程下载服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
// ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021);
// ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD);
FTPFile[] fs;
ftpClient.makeDirectory(routeFilepath);
ftpClient.changeWorkingDirectory(routeFilepath);
bl = "false";
fs = ftpClient.listFiles();
for (FTPFile ff : fs)
if (ff.getName().equals(fileName))
bl = "true";
Log.info("下载文件开始。");
ftpClient.setBufferSize(1024);
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = ftpClient.retrieveFileStream(fileName);
bos = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer)) != -1)
bos.write(buffer, 0, count);
bos.flush();
fos = new FileOutputStream(localFilePath+SFP+fileName);
fos.write(bos.toByteArray());
Log.info("下载文件结束:"+localFilePath);
Log.info("检查文件是否存:"+fileName+" "+bl);
if("false".equals(bl))
ViewUtil.dataSEErrorPerformedCommon("查询无结果,请稍后再查询。");
return bl;
return bl;
catch (Exception e)
throw e;
finally
if (fis != null)
try
fis.close();
catch (Exception e)
Log.info(e.getLocalizedMessage(), e);
if (bos != null)
try
bos.close();
catch (Exception e)
Log.info(e.getLocalizedMessage(), e);
if (fos != null)
try
fos.close();
catch (Exception e)
Log.info(e.getLocalizedMessage(), e);
// 调用样例:
public static void main(String[] args)
try
// 密钥/res/20150228
// ZipUtilAll.unZip(new File(("D:/123/123.zip")), "D:/123/");
// ZipDemo1232.unZip(new File(("D:/123/123.zip")), "D:/123/");
// 明文文件路径
String plainFilePath = "D:/req_20150204_0011.txt";
// 密文文件路径
String secretFilePath = "req_20150204_00134.txt";
// 加密
// encodeAESFile(key, plainFilePath, secretFilePath);
fileDownloadByFtp("D://123.zip","123.zip","req/20150228");
ZipUtilAll.unZip("D://123.zip", "D:/123/李筱/");
// 解密
plainFilePath = "D:/123.sql";
// secretFilePath = "D:/test11111.sql";
// decodeAESFile(key, plainFilePath, secretFilePath);
catch (Exception e)
e.printStackTrace();
参考技术B 买本java web开发详解
看了一下656页有上传下载例子
以上是关于怎样用java开发ftp客户端的主要内容,如果未能解决你的问题,请参考以下文章