⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpmainframe.java

📁 ftp 客服端和服务器端
💻 JAVA
字号:
package com.topking.ftp.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.UnsupportedLookAndFeelException;

import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

/*
 * FtpMainFrame.java
 *
 * Created on 2008年11月29日, 下午4:22
 */



/**
 *
 * @author  lzkj
 */
public class FtpMainFrame extends javax.swing.JFrame {

    /** Creates new form FtpMainFrame */
	 // Variables declaration - do not modify
    private javax.swing.JLabel L_icon;
    private javax.swing.JLabel L_txt;
    private javax.swing.JPasswordField P_passWord;
    private javax.swing.JTextField T_fileName;
    private javax.swing.JTextField T_host;
    private javax.swing.JTextField T_userName;
    private javax.swing.JButton bt_chooice;
    private javax.swing.JProgressBar pbar_progress;
    private String currentDirectoryPath;
    private ImageIcon ticon;
    private String ttext;
    
    // End of variables declaration
    public FtpMainFrame() {
    	setTitle("FTP文件上传工具(JAVA版) @author: jacky");
    	setLocation(300,200);    	   	
    	this.setIconImage(this.getToolkit().createImage(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/logo.gif")));   	   	
    	currentDirectoryPath = "";
    	initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        T_host = new javax.swing.JTextField();
        T_userName = new javax.swing.JTextField();
        P_passWord = new javax.swing.JPasswordField();
        T_fileName = new javax.swing.JTextField();
        bt_chooice = new javax.swing.JButton();
//        L_icon = new javax.swing.JLabel(new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/spinner.gif")));
        L_icon = new javax.swing.JLabel();
        L_txt = new javax.swing.JLabel();
        pbar_progress = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        T_host.setText("211.152.32.40");

        T_userName.setText("ks966.com");

        P_passWord.setText("9jQr2eYwNX");

        bt_chooice.setText("浏览");

        L_txt.setText("");
        
//        L_icon.setVisible(false);
//        L_txt.setVisible(false);
        
        T_fileName.setEditable(false);

        pbar_progress.setStringPainted(true);
        pbar_progress.setVisible(false);
        
        bt_chooice.addActionListener(new ActionListener(){
        	String path = null;
        	String fname = null;
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(bt_chooice.getText().equals("浏览")){
					JFileChooser jfc = new JFileChooser(currentDirectoryPath);
					jfc.showDialog(FtpMainFrame.this, "选择上传文件");
					path = jfc.getSelectedFile().getAbsolutePath();
					fname = jfc.getSelectedFile().getName();
					System.out.println(fname);
					if(path!=null){
						currentDirectoryPath = jfc.getSelectedFile().getPath();//保存该路径作为浏览窗口的新路径
						T_fileName.setText(path);
						bt_chooice.setText("上传");
					}					
				}else{
					System.out.println("上传文件");
					ticon = new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/spinner.gif"));
					ttext = "正在验证登录";
					new Thread(new changeLabelUI()).start();
					if(UploadFileToServer(T_host.getText(),T_userName.getText(),new String(P_passWord.getPassword()),path,fname)){
						pbar_progress.setVisible(true);
					}else{
						pbar_progress.setVisible(false);
					}
				}
			}
        	
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(T_host, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(T_userName, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(P_passWord, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(bt_chooice)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(T_fileName, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(L_icon, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(L_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(223, Short.MAX_VALUE))
            .addComponent(pbar_progress, javax.swing.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(T_host, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(T_userName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(P_passWord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_chooice)
                    .addComponent(T_fileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(L_icon, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(L_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pbar_progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    
    public boolean UploadFileToServer(String hostName,String name,String pass,String filePath,String fName){
    	boolean flag = false;
    	TelnetOutputStream os = null;  
    	FileInputStream is = null;  
    	FtpClient ftp = new FtpClient();
    	try {
			ftp.openServer(hostName);
			ftp.login(name, pass);
			ticon = new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/add.gif"));
			ttext = "验证登录成功";
			L_icon.setIcon(ticon);
			L_txt.setText(ttext);			
			L_icon.setVisible(true);
			L_txt.setVisible(true);
			ftp.cd("/META-INF/");
			ftp.binary();
			os = ftp.put(fName);
			is = new FileInputStream(new File(filePath));
			int b = -1;
			while((b = is.available())!=-1){
				byte[] bytes = new byte[is.available()];
				os.write(bytes);
				
			}
			pbar_progress.setVisible(true);
			flag = true;
			ticon = new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/delete.gif"));
			ttext = "上传文件成功";
			L_icon.setIcon(ticon);
			L_txt.setText(ttext);
			L_icon.setVisible(true);
			L_txt.setVisible(true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ticon = new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/delete.gif"));
			ttext = "验证登录失败";
			L_icon.setIcon(ticon);
			L_txt.setText(ttext);
			L_icon.setVisible(true);
			L_txt.setVisible(true);
		} finally { 
			try{
				 if (is != null) {  
				        is.close();  
				 }  
				 if (os != null) {  
				        os.close();  
				 }
			}catch (IOException e) {
				
			}		     
		}  
    	return flag;
    }
    
    public class changeLabelUI implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			L_icon.setIcon(ticon);
			L_txt.setText(ttext);
			L_icon.setVisible(true);
			L_txt.setVisible(true);
		}    	
    }
    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {       
        try {
			javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
			com.birosoft.liquid.LiquidLookAndFeel.setLiquidDecorations(true);
			java.awt.EventQueue.invokeLater(new Runnable() {
	            public void run() {
	            	new FtpMainFrame().setVisible(true);
	            }
	        });     			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -