FTP客户端遇到的问题,求助!!
程序代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpLoginException;
public class FTPClient extends JFrame {
private static final String setText = null;
/**
* 客户端框架
* FlowLayout 布局管理器
*/
JPanel pn = new JPanel();
JPanel pn1 = new JPanel(new FlowLayout(0));
JPanel pn2 = new JPanel(new FlowLayout(0));
JPanel pn3 = new JPanel(new FlowLayout(0));
JPanel pn4 = new JPanel(new FlowLayout(0));
JPanel pn5 = new JPanel(new GridLayout(1,2));
JPanel pc = new JPanel();
JPanel ps = new JPanel();
JLabel labelHost = new JLabel();
JTextField textFieldHost = new JTextField(18);
JLabel labelPort = new JLabel();
JTextField textFieldPort = new JTextField(4);
JButton buttonLink = new JButton(); //连接按钮
JLabel labelUser = new JLabel();
JTextField textFieldUser = new JTextField(8); //用户名
JLabel labelPassword = new JLabel();
TextField textFieldPassword = new TextField(8); //密码
JButton buttonDisconnect = new JButton(); //断开连接
JLabel labelFileShow = new JLabel();
JLabel labelclient = new JLabel();
JLabel labelserver = new JLabel();
JLabel labelstatus = new JLabel();
List listclient = new List(5); // 创建一个用指定可视行数初始化的新滚动列表
List listServer = new List(5);
PopupMenu pmc1 = new PopupMenu(); //组件的指定位置上动态弹出的菜单
MenuItem upload = new MenuItem("上传");
MenuItem view = new MenuItem("浏览");
PopupMenu pms = new PopupMenu();
MenuItem download = new MenuItem("下载");
MenuItem reflash = new MenuItem("刷新");
int port = 21;
String dir ="";
TelnetInputStream is = null;
TelnetOutputStream os = null;
FtpClient ftp = new FtpClient();
/**
* 构造方法
*/
public FTPClient() {
setSize(new Dimension(480,500));
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e){
e.printStackTrace();
}
}
private void jbInit()throws Exception {
add(pn,BorderLayout.NORTH);
add(pc,BorderLayout.CENTER);
add(ps,BorderLayout.SOUTH);
pn.setLayout(new GridLayout(5,1));
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
pn.add(pn5);
pc.setLayout(new GridLayout(1,2));
pc.add(listclient);
listclient.add("c");
listclient.add("c");
listclient.add("c");
listclient.add("c");
listclient.add("c");
pc.add(listServer);
listServer.add("s");
listServer.add("s");
listServer.add("s");
listServer.add("s");
listServer.add("s");
pn1.add(labelHost);
pn1.add(textFieldHost);
pn1.add(buttonLink);
pn3.add(labelPort);
pn3.add(textFieldPort);
pn2.add(labelUser);
pn2.add(textFieldUser);
pn2.add(labelPassword);
pn2.add(textFieldPassword);
pn2.add(buttonDisconnect);
pn4.add(labelFileShow);
pn5.add(labelclient);
pn5.add(labelserver);
ps.add(labelstatus);
labelHost.setText("主机名");
labelPort.setText("端口");
labelUser.setText("用户名");
labelPassword.setText("密码");
textFieldPassword.setEchoChar('*');
labelclient.setText("客户端");
labelserver.setText("服务器端");
pmc1.add(upload);
pmc1.add(view);
pms.add(download);
pms.add(reflash);
listclient.add(pmc1);
listclient.addMouseListener( new MouseAdapter() {
public void mouseClicked(MouseEvent me) { // 鼠标按键在组件上单击时调用。
if(SwingUtilities.isRightMouseButton(me)) {
pmc1.show(listclient,me.getX(),me.getY());
}
}
});
listServer.add(pms);
listServer.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me){
if(SwingUtilities.isRightMouseButton(me)) { //如果鼠标事件指定右边鼠标按键,则返回 true。
pms.show(listServer, me.getX(), me.getY());
}
}
});
buttonLink.setLabel("连接");
buttonLink.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonLink_ActionPerform(e);
}
});
buttonLink.setEnabled(true);
buttonDisconnect.setLabel("断开");
buttonDisconnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonDisconnect_ActionPerFormed(e);
}
});
buttonDisconnect.setEnabled(false);
labelFileShow.setText("目录列表");
upload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
upload_actionPerformed(e); //监听器,调用处理程序
}
});
view.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
view_actionPerformed(e);
}
});
download.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
download_actionPerformed(e);
}
});
reflash.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
reflash_actionPerformed(e);
}
});
}
/**
*
* 响应连接按钮事件,连接到服务器
*/
void buttonLink_ActionPerform(ActionEvent e) {
System.out.println("连接中。。。。。!");
String hostname = textFieldHost.getText();
labelstatus.setText("连接中,请等待。。。");
try {
ftp.openServer(hostname,port);
ftp.login(textFieldUser.getText(), textFieldPassword.getText());
ftp.binary();
ftp.cd(dir);
showFileContents();
labelstatus.setText("连接主机:"+textFieldHost.getText()+"成功");
buttonDisconnect.setEnabled(true);
}
catch(FtpLoginException e0) {
labelstatus.setText("用户名、密码错误!");
e0.printStackTrace();
}
catch(IOException e1) {
labelstatus.setText(hostname+"连接失败!!!");
e1.printStackTrace();
}
catch(SecurityException e2) {
labelstatus.setText("没有权限!!");
e2.printStackTrace();
}
if(hostname.equals(""))
labelstatus.setText("FTP服务器不能为空!!!");
else if(textFieldUser.getText().equals(""))
labelstatus.setText("用户名不能为空!!!");
else if(textFieldPassword.getText().equals(""))
labelstatus.setText("密码不能为空!!!") ;
}
void buttonDisconnect_ActionPerFormed(ActionEvent e) {
try {
ftp.closeServer(); //关闭客户端与服务器的连接
labelstatus.setText("与服务器已断开....");
} catch (IOException e1) {
System.out.println("Error" + e1);
e1.printStackTrace();
}
buttonLink.setEnabled(true);
buttonDisconnect.setEnabled(false);
}
protected void reflash_actionPerformed(ActionEvent e) {
System.out.println("view!");
}
void download_actionPerformed(ActionEvent e) {
System.out.println("down!");
upload.setEnabled(false);
TelnetInputStream fget = null;
RandomAccessFile getfile = null ;
try {
String fileName = "test.jpg";
String fileUrl = "E:\test.jpg";
char ch;
if(!new File("E:/FTP共享/").isDirectory()) {
new File("E:/FTP共享/").mkdir();
}
String keepLocate = "E:/FTP共享/"+fileName;
fget = ftp.get("fileUrl");
DataInputStream puts = new DataInputStream(fget);
File fi = new File(keepLocate);
RandomAccessFile getFile = new RandomAccessFile(fi,"rw");
getFile.seek(0); //将指针指向最前端
while((ch = (char) puts.read()) >= 0) {
getFile.write(ch);
}
System.out.print("------4" + "finish and success" );
}
catch (Exception e1) {
e1.printStackTrace(System.out);
// throws new Exception(e.getMessage());
}finally {
try {
fget.close();
}catch(Exception e2) {
System.out.print("-------1" + "" +e);
}
try {
getfile.close();
}catch(Exception e3) {
System.out.println("-----2" + e);
}
try {
ftp.closeServer();
}catch(Exception e4) {
System.out.println("------3" + "" +e);
}
System.out.println("--------5" + "finish and success");
}
/* String outfile = null;String file = null;
try {
RandomAccessFile rFile = new RandomAccessFile(outfile,"rw"); //对随机访问文件的读写和写入
rFile.seek(0); // 设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。
TelnetInputStream tInput = ftp.get(file); //.get(file);
DataInputStream dInput = new DataInputStream(tInput);
while((ch = dInput.read())>=0) {
rFile.write(ch);
}
dInput.close();
rFile.close();
}
catch(IOException e1) {
System.out.println("Error" + e1);
e1.printStackTrace();
}*/
}
protected void view_actionPerformed(ActionEvent e) {
System.out.println("view");
}
void showFileContents() {
FtpClient ftp = new FtpClient();
int ch;
StringBuffer buf = new StringBuffer();
try {
FilterInputStream inStream = ftp.list(); //获取服务器当前目录下所有的文件盒目录的输入
while((ch=inStream.read())>=0) { //读取数据
buf.append((char)ch); //保存数据到缓冲区
}
// listServer.append(buf.toString());
buf.append(buf.toString());
inStream.close();
}
catch(Exception e) {
System.out.println("Error" + e);
}
// }
}
private void upload_actionPerformed(ActionEvent e) {
String infile = null;
int i = infile.lastIndexOf("//");
String outfile = infile.substring(i+1,infile.length());
labelstatus.setText("infile[" +infile + "]");
labelstatus.setText("outfile[" + outfile + "]lastindex[" + i+ "]");
try {
TelnetOutputStream os =ftp.put(outfile);
File file_in = new File(infile);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while((c=is.read(bytes))!=-1){
os.write(bytes,0,c);
labelstatus.setText("bytes[" + bytes + "]");
}
is.close();
os.close();
}
catch(IOException e1){
e1.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("Starting FTPClient!");
FTPClient mainFrame = new FTPClient();
mainFrame.setVisible(true);
}
做了个简单的FTP客户端,List列表框里不知道怎么显示文件的名字,菜鸟求助!还有就是,下载和上传的代码........import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpLoginException;
public class FTPClient extends JFrame {
private static final String setText = null;
/**
* 客户端框架
* FlowLayout 布局管理器
*/
JPanel pn = new JPanel();
JPanel pn1 = new JPanel(new FlowLayout(0));
JPanel pn2 = new JPanel(new FlowLayout(0));
JPanel pn3 = new JPanel(new FlowLayout(0));
JPanel pn4 = new JPanel(new FlowLayout(0));
JPanel pn5 = new JPanel(new GridLayout(1,2));
JPanel pc = new JPanel();
JPanel ps = new JPanel();
JLabel labelHost = new JLabel();
JTextField textFieldHost = new JTextField(18);
JLabel labelPort = new JLabel();
JTextField textFieldPort = new JTextField(4);
JButton buttonLink = new JButton(); //连接按钮
JLabel labelUser = new JLabel();
JTextField textFieldUser = new JTextField(8); //用户名
JLabel labelPassword = new JLabel();
TextField textFieldPassword = new TextField(8); //密码
JButton buttonDisconnect = new JButton(); //断开连接
JLabel labelFileShow = new JLabel();
JLabel labelclient = new JLabel();
JLabel labelserver = new JLabel();
JLabel labelstatus = new JLabel();
List listclient = new List(5); // 创建一个用指定可视行数初始化的新滚动列表
List listServer = new List(5);
PopupMenu pmc1 = new PopupMenu(); //组件的指定位置上动态弹出的菜单
MenuItem upload = new MenuItem("上传");
MenuItem view = new MenuItem("浏览");
PopupMenu pms = new PopupMenu();
MenuItem download = new MenuItem("下载");
MenuItem reflash = new MenuItem("刷新");
int port = 21;
String dir ="";
TelnetInputStream is = null;
TelnetOutputStream os = null;
FtpClient ftp = new FtpClient();
/**
* 构造方法
*/
public FTPClient() {
setSize(new Dimension(480,500));
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e){
e.printStackTrace();
}
}
private void jbInit()throws Exception {
add(pn,BorderLayout.NORTH);
add(pc,BorderLayout.CENTER);
add(ps,BorderLayout.SOUTH);
pn.setLayout(new GridLayout(5,1));
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
pn.add(pn5);
pc.setLayout(new GridLayout(1,2));
pc.add(listclient);
listclient.add("c");
listclient.add("c");
listclient.add("c");
listclient.add("c");
listclient.add("c");
pc.add(listServer);
listServer.add("s");
listServer.add("s");
listServer.add("s");
listServer.add("s");
listServer.add("s");
pn1.add(labelHost);
pn1.add(textFieldHost);
pn1.add(buttonLink);
pn3.add(labelPort);
pn3.add(textFieldPort);
pn2.add(labelUser);
pn2.add(textFieldUser);
pn2.add(labelPassword);
pn2.add(textFieldPassword);
pn2.add(buttonDisconnect);
pn4.add(labelFileShow);
pn5.add(labelclient);
pn5.add(labelserver);
ps.add(labelstatus);
labelHost.setText("主机名");
labelPort.setText("端口");
labelUser.setText("用户名");
labelPassword.setText("密码");
textFieldPassword.setEchoChar('*');
labelclient.setText("客户端");
labelserver.setText("服务器端");
pmc1.add(upload);
pmc1.add(view);
pms.add(download);
pms.add(reflash);
listclient.add(pmc1);
listclient.addMouseListener( new MouseAdapter() {
public void mouseClicked(MouseEvent me) { // 鼠标按键在组件上单击时调用。
if(SwingUtilities.isRightMouseButton(me)) {
pmc1.show(listclient,me.getX(),me.getY());
}
}
});
listServer.add(pms);
listServer.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me){
if(SwingUtilities.isRightMouseButton(me)) { //如果鼠标事件指定右边鼠标按键,则返回 true。
pms.show(listServer, me.getX(), me.getY());
}
}
});
buttonLink.setLabel("连接");
buttonLink.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonLink_ActionPerform(e);
}
});
buttonLink.setEnabled(true);
buttonDisconnect.setLabel("断开");
buttonDisconnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonDisconnect_ActionPerFormed(e);
}
});
buttonDisconnect.setEnabled(false);
labelFileShow.setText("目录列表");
upload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
upload_actionPerformed(e); //监听器,调用处理程序
}
});
view.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
view_actionPerformed(e);
}
});
download.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
download_actionPerformed(e);
}
});
reflash.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
reflash_actionPerformed(e);
}
});
}
/**
*
* 响应连接按钮事件,连接到服务器
*/
void buttonLink_ActionPerform(ActionEvent e) {
System.out.println("连接中。。。。。!");
String hostname = textFieldHost.getText();
labelstatus.setText("连接中,请等待。。。");
try {
ftp.openServer(hostname,port);
ftp.login(textFieldUser.getText(), textFieldPassword.getText());
ftp.binary();
ftp.cd(dir);
showFileContents();
labelstatus.setText("连接主机:"+textFieldHost.getText()+"成功");
buttonDisconnect.setEnabled(true);
}
catch(FtpLoginException e0) {
labelstatus.setText("用户名、密码错误!");
e0.printStackTrace();
}
catch(IOException e1) {
labelstatus.setText(hostname+"连接失败!!!");
e1.printStackTrace();
}
catch(SecurityException e2) {
labelstatus.setText("没有权限!!");
e2.printStackTrace();
}
if(hostname.equals(""))
labelstatus.setText("FTP服务器不能为空!!!");
else if(textFieldUser.getText().equals(""))
labelstatus.setText("用户名不能为空!!!");
else if(textFieldPassword.getText().equals(""))
labelstatus.setText("密码不能为空!!!") ;
}
void buttonDisconnect_ActionPerFormed(ActionEvent e) {
try {
ftp.closeServer(); //关闭客户端与服务器的连接
labelstatus.setText("与服务器已断开....");
} catch (IOException e1) {
System.out.println("Error" + e1);
e1.printStackTrace();
}
buttonLink.setEnabled(true);
buttonDisconnect.setEnabled(false);
}
protected void reflash_actionPerformed(ActionEvent e) {
System.out.println("view!");
}
void download_actionPerformed(ActionEvent e) {
System.out.println("down!");
upload.setEnabled(false);
TelnetInputStream fget = null;
RandomAccessFile getfile = null ;
try {
String fileName = "test.jpg";
String fileUrl = "E:\test.jpg";
char ch;
if(!new File("E:/FTP共享/").isDirectory()) {
new File("E:/FTP共享/").mkdir();
}
String keepLocate = "E:/FTP共享/"+fileName;
fget = ftp.get("fileUrl");
DataInputStream puts = new DataInputStream(fget);
File fi = new File(keepLocate);
RandomAccessFile getFile = new RandomAccessFile(fi,"rw");
getFile.seek(0); //将指针指向最前端
while((ch = (char) puts.read()) >= 0) {
getFile.write(ch);
}
System.out.print("------4" + "finish and success" );
}
catch (Exception e1) {
e1.printStackTrace(System.out);
// throws new Exception(e.getMessage());
}finally {
try {
fget.close();
}catch(Exception e2) {
System.out.print("-------1" + "" +e);
}
try {
getfile.close();
}catch(Exception e3) {
System.out.println("-----2" + e);
}
try {
ftp.closeServer();
}catch(Exception e4) {
System.out.println("------3" + "" +e);
}
System.out.println("--------5" + "finish and success");
}
/* String outfile = null;String file = null;
try {
RandomAccessFile rFile = new RandomAccessFile(outfile,"rw"); //对随机访问文件的读写和写入
rFile.seek(0); // 设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。
TelnetInputStream tInput = ftp.get(file); //.get(file);
DataInputStream dInput = new DataInputStream(tInput);
while((ch = dInput.read())>=0) {
rFile.write(ch);
}
dInput.close();
rFile.close();
}
catch(IOException e1) {
System.out.println("Error" + e1);
e1.printStackTrace();
}*/
}
protected void view_actionPerformed(ActionEvent e) {
System.out.println("view");
}
void showFileContents() {
FtpClient ftp = new FtpClient();
int ch;
StringBuffer buf = new StringBuffer();
try {
FilterInputStream inStream = ftp.list(); //获取服务器当前目录下所有的文件盒目录的输入
while((ch=inStream.read())>=0) { //读取数据
buf.append((char)ch); //保存数据到缓冲区
}
// listServer.append(buf.toString());
buf.append(buf.toString());
inStream.close();
}
catch(Exception e) {
System.out.println("Error" + e);
}
// }
}
private void upload_actionPerformed(ActionEvent e) {
String infile = null;
int i = infile.lastIndexOf("//");
String outfile = infile.substring(i+1,infile.length());
labelstatus.setText("infile[" +infile + "]");
labelstatus.setText("outfile[" + outfile + "]lastindex[" + i+ "]");
try {
TelnetOutputStream os =ftp.put(outfile);
File file_in = new File(infile);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while((c=is.read(bytes))!=-1){
os.write(bytes,0,c);
labelstatus.setText("bytes[" + bytes + "]");
}
is.close();
os.close();
}
catch(IOException e1){
e1.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("Starting FTPClient!");
FTPClient mainFrame = new FTPClient();
mainFrame.setVisible(true);
}
搜索更多相关的解决方案:
客户端
----------------解决方案--------------------------------------------------------
文件类不是有个getname方法吗?直接getname就可以获得文件的名字String类型的 然后在加到list里就是了
至于下载和上载 就不知道了
----------------解决方案--------------------------------------------------------
回复 2楼 w527705090
想请问下怎么连接那个当做服务器的文件夹?? ----------------解决方案--------------------------------------------------------
回复 3楼 蓝xuan
你肯定不能直接连接服务器的文件夹 只能通过服务器 将你发送的文件写到服务器的文件夹下
----------------解决方案--------------------------------------------------------
以下是引用w527705090在2013-3-31 23:54:43的发言:
你肯定不能直接连接服务器的文件夹
只能通过服务器 将你发送的文件写到服务器的文件夹下
你后面的那句话不是很懂,求解释!!! 你肯定不能直接连接服务器的文件夹
只能通过服务器 将你发送的文件写到服务器的文件夹下
----------------解决方案--------------------------------------------------------
回复 5楼 蓝xuan
就是 服务器接收客户端的资料 也就是用流处理 不知道你懂了没...
----------------解决方案--------------------------------------------------------
还是没懂啊啊啊啊啊啊,有点抽象呀呀呀,求详细点。。。。。!!!!
----------------解决方案--------------------------------------------------------
用流传文件你知道吗? 就是IO流
懂了吗 ?
----------------解决方案--------------------------------------------------------