java ftp swing源码_简单的FTP上传下载(java实现 swing界面)

1 importjava.awt.EventQueue;2

3 importjavax.swing.JFrame;4 importjava.awt.BorderLayout;5 importjavax.swing.JTable;6 importjavax.swing.border.BevelBorder;7 importjavax.swing.JFileChooser;8 importjavax.swing.JScrollPane;9 importjavax.swing.JTextField;10 importjavax.swing.JButton;11 importjavax.swing.JRadioButton;12 importjavax.swing.JTextArea;13 importjavax.swing.JLabel;14 importjava.awt.event.ActionListener;15 importjava.awt.event.ActionEvent;16 importjava.awt.Color;17 importjava.awt.Font;18 importjavax.swing.SwingConstants;19 importjavax.swing.UIManager;20 importjava.awt.Toolkit;21 importjavax.swing.table.DefaultTableModel;22 importjavax.swing.border.CompoundBorder;23 importjavax.swing.border.LineBorder;24 importjavax.swing.filechooser.FileSystemView;25 importjavax.swing.JScrollBar;26

27 importorg.apache.commons.net.ftp.FTPFile;28

29 importjava.awt.ScrollPane;30 importjava.awt.Label;31 importjava.io.File;32 importjava.io.IOException;33 importjava.util.ArrayList;34 importjava.util.Date;35 importjava.util.Vector;36 importjava.awt.Scrollbar;37

38 public class Frame_Main implementsActionListener{39

40

41 //初始化参数--------------------------------

42 staticFTPFile[] file;43 static String FTP="192.168.1.86";

44 static String username="huanglizhe";

45 static String password="123456";46 //初始化参数--------------------------------

47

48

49 privateJFrame frame;50 privateJTable table;51 staticFtp_by_apache ftp;52 public staticFtp_by_apache getFtp() {53 returnftp;54 }55

56 /**

57 * Launch the application.58 */

59 public static voidmain(String[] args) {60

61 ftp=newFtp_by_apache(FTP,username,password);62 file=ftp.getAllFile();63

64

65

66 EventQueue.invokeLater(newRunnable() {67 public voidrun() {68 try{69 Frame_Main window = newFrame_Main();70 window.frame.setVisible(true);71 } catch(Exception e) {72 e.printStackTrace();73 }74 }75 });76

77 }78

79 /**

80 * Create the application.81 */

82 publicFrame_Main() {83 initialize();84 }85

86 /**

87 * Initialize the contents of the frame.88 */

89 private voidinitialize() {90 frame = newJFrame();91 frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Frame_Main.class.getResource("/com/sun/java/swing/plaf/windows/icons/UpFolder.gif")));92 frame.setTitle("FTP");93 frame.setBounds(100, 100, 470, 534);94 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);95 frame.getContentPane().setLayout(null);96

97 //上传按钮--------------------------------------------------

98 JButton upload = new JButton("\u4E0A\u4F20");99 upload.setFont(new Font("宋体", Font.PLAIN, 12));100 upload.setBackground(UIManager.getColor("Button.highlight"));101 upload.addActionListener(newActionListener() {102 public voidactionPerformed(ActionEvent arg0) {103 //上传点击按钮触发------------------------------------

104 System.out.println("上传!!!!!");105 int result = 0;106 File file = null;107 String path = null;108 JFileChooser fileChooser = newJFileChooser();109 FileSystemView fsv =FileSystemView.getFileSystemView();110 System.out.println(fsv.getHomeDirectory()); //得到桌面路径

111 fileChooser.setCurrentDirectory(fsv.getHomeDirectory());112 fileChooser.setDialogTitle("请选择要上传的文件...");113 fileChooser.setApproveButtonText("确定");114 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);115 result = fileChooser.showOpenDialog(null);116 if (JFileChooser.APPROVE_OPTION ==result) {117 path=fileChooser.getSelectedFile().getPath();118 System.out.println("path: "+path);119 try{120 //下载

121 ftp.upload(path);122 } catch(IOException e1) {123 //TODO Auto-generated catch block

124 e1.printStackTrace();125 }126 finally{127

128 ftp.close_connection();129 }130 }131 //上传点击按钮触发------------------------------------

132 }133 });134 upload.setBounds(195, 15, 82, 23);135 frame.getContentPane().add(upload);136 //上传按钮--------------------------------------------------137

138

139

140 //刷新按钮--------------------------------------------------

141 JButton refresh = new JButton("\u5237\u65B0");142 refresh.addActionListener(newActionListener() {143 public voidactionPerformed(ActionEvent arg0) {144 }145 });146 refresh.setFont(new Font("宋体", Font.PLAIN, 12));147 refresh.setBackground(UIManager.getColor("Button.highlight"));148 refresh.setBounds(312, 15, 82, 23);149 frame.getContentPane().add(refresh);150 //刷新按钮--------------------------------------------------151

152

153

154 //显示基本信息(FTP username)-----------------------------------------------

155 JLabel lblNewLabel = new JLabel("FTP\u5730\u5740");156 lblNewLabel.setBounds(32, 10, 54, 15);157 frame.getContentPane().add(lblNewLabel);158

159 JLabel lblNewLabel_1 = new JLabel("\u7528\u6237\u540D");160 lblNewLabel_1.setBounds(32, 35, 54, 15);161 frame.getContentPane().add(lblNewLabel_1);162

163 JLabel address = newJLabel(FTP);164 address.setBounds(110, 10, 75, 15);165 frame.getContentPane().add(address);166

167 JLabel name = newJLabel(username);168 name.setBounds(110, 35, 82, 15);169 frame.getContentPane().add(name);170 //显示基本信息-----------------------------------------------171

172

173 //table数据初始化 从FTP读取所有文件

174 String[][] data1=new String[file.length][4];175 for(int row=0;row

178 data1[row][0]=file[row].getName();179 if(file[row].isDirectory())180 {181 data1[row][1]="文件夹";182 }183 else if(file[row].isFile()){184 String[] geshi=file[row].getName().split("\\.");185 data1[row][1]=geshi[1];186 }187 data1[row][2]=file[row].getSize()+"";188 data1[row][3]="下载";189 }190

191

192

193 //table列名-----------------------------------------------------

194 String[] columnNames = {"文件", "文件类型", "文件大小(字节)", ""};195 DefaultTableModel model = newDefaultTableModel();196 model.setDataVector(data1, columnNames);197

198 //加滚动条--------------------------------------------------------

199 JScrollPane scrollPane = newJScrollPane();200 scrollPane.setBounds(32, 73, 362, 384);201 frame.getContentPane().add(scrollPane);202 //加滚动条-----------------------------------------------------203

204 //table功能------------------------------------------------------

205 table = newJTable(model);206 scrollPane.setViewportView(table);207 table.setColumnSelectionAllowed(true);208 table.setCellSelectionEnabled(true);209 table.setFont(new Font("微软雅黑", Font.PLAIN, 12));210 table.setBorder(new LineBorder(new Color(0, 0, 0)));211 table.setToolTipText("\u53EF\u4EE5\u70B9\u51FB\u4E0B\u8F7D");212

213 //table button初始化(最后一列的按键)--------------------

214 ButtonColumn buttonsColumn = new ButtonColumn(table, 3);215

216 }217

218 @Override219 public voidactionPerformed(ActionEvent arg0) {220 //TODO Auto-generated method stub

221

222 }223 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值