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

📄 mainframe.java

📁 ftp 客服端和服务器端
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                        .addComponent(T_local, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_localreturn))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bt_remotereturn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(T_remote, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)))
            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 922, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_browser)
                    .addComponent(T_remote, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_remotereturn)
                    .addComponent(bt_localreturn)
                    .addComponent(T_local, 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.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        clearInfo();
        pack();
    }// </editor-fold>

    public void clearInfo(){
    	int RowCount = tb_progress.getRowCount();
    	for(int i=0;i<RowCount;i++){
			((javax.swing.table.DefaultTableModel)(tb_progress.getModel())).removeRow(0);
			tb_progress.updateUI();
		}
    	for(int i = 0;i<upList.size();i++){
    		upList.remove(i);    		
    	}
    	RowCount = 0;
    	loadRemoteFile();	
    }
    
    public void openFile(String file){
    	String[] cmd = new String[3];
    	cmd[0] = "cmd";
    	cmd[1] = "/C";
    	cmd[2] = file;
    	try {
			Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public boolean isFile(String fname){
    	File file = new File(fname);
    	return file.isDirectory()?false:true;
    }
    
    public void loadLocalFile(){
    	if(localPath!=null&&!"".equals(localPath)){
    		Vector v = new Vector();
    		String[] files = new File(localPath).list();
    		for(int i=0;i<files.length;i++){
//    			//System.out.println(files[i]);
    			InstallData data;
    			FileBean fb = new FileBean();
    			File file = new File(localPath+"/"+files[i]);
    			fb.setFileName(file.getName());
				fb.setFilePath(file.getAbsolutePath());
				fb.setSize(file.length());
				fb.setTime(df.format(new Date(file.lastModified())));
    			if(isFile(localPath+"/"+files[i])){//文件    				
    				fb.setType("文件");
    				data = new InstallData(files[i],new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/file.gif")),fb);
    				
    			}else{//文件夹  
    				fb.setType("文件夹");
    				data = new InstallData(files[i],new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/folder.gif")),fb);    				
    			}  
    			v.add(data);
    			list_local.setModel(new DefaultComboBoxModel(v));
    			list_local.setCellRenderer(new MyListCellRenderer());
    			refreshLocalPath();
    		}
    	}
    }
    
    public String listRemoteFile(){
    	String list = null;
    	try {
			ftp.cd(remotePath);			
			TelnetInputStream is = ftp.list();
			int c;
			StringBuffer sb = new StringBuffer();
			while ((c=is.read())!=-1) {	
				sb.append((char) c);
				/*if(c==10||c==13){
					sb.append('\n');
				}else{
					sb.append((char) c);
				}*/			
			}
			list = new String(sb);
			is.close();
			OutputStream os = new FileOutputStream(new File("RemoteFileList.txt"));
			os.write(list.getBytes());
			os.flush();
			os.close();			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(this, "刷新远程目录失败 异常信息("+e.getMessage()+")","错误",JOptionPane.ERROR_MESSAGE);
			
			e.printStackTrace();
		}
		return list;
    }
    
    public void loadRemoteFile(){
    			
    		String list = listRemoteFile();
    		System.out.println();
    		System.out.println(list);
    		System.out.println();
//			BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
			String[] info = list.split("\n");
			Vector v = new Vector();
//			while((line = reader.readLine())!=null){	
			for(int i=0;i<info.length;i++){				
				InstallData data;
				System.err.println("info["+i+"] "+info[i]);
				if(info[i].length()>8){
					info[i] = info[i].replaceAll(" ", "/");	
					while(info[i].contains("//")){
						info[i] = info[i].replaceAll("//", "/");
					}		
					System.out.println("info["+i+"] "+info[i]);
					System.out.println("-----------------------------------");
					String[] s = info[i].split("/");			
					String type = s[0];				
					String fname = s[s.length-1];
					if(!fname.startsWith(".")&&!fname.startsWith("..")){
						FileBean fb = new FileBean();					
						String time = s[5]+" "+s[6]+" "+s[7];					
						fb.setFileName(fname);
						fb.setFilePath(remotePath+fname+"/");					
						fb.setTime(time);
						fb.setSize(Long.parseLong(s[4]));
						
						if("drw-rw-rw-".equals(type)){//文件夹
							fb.setType("文件夹");						
							data = new InstallData(fname,new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/folder.gif")),fb);	    				
						}else{//文件
							fb.setType("文件");							
							data = new InstallData(fname,new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/file.gif")),fb);	    				
						}
						v.add(data);
						list_remote.setModel(new DefaultComboBoxModel(v));
						list_remote.setCellRenderer(new MyListCellRenderer());
					}
				}				
			}		
    }
    
    public void refreshLocalPath(){
    	T_local.setText(localPath);
    }
    
    public String getRemotePath(){
    	return T_remote.getText().trim();
    }
    
    public void refreshRemotePath(){
    	T_remote.setText(remotePath);
    }
    
    public String getLocalPath(){
    	return T_local.getText().trim();
    }
        
   
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==bt_browser){
			JFileChooser jfc = new JFileChooser();
			jfc.showOpenDialog(this);
//			localPath = jfc.getSelectedFile().getPath().replaceAll("\\", "/");
			String path = jfc.getSelectedFile().getPath();
			path = path.replace('\\', '/');
			StringBuffer temp = new StringBuffer(path);
			temp.reverse();
			System.out.println(temp);
			int off = temp.indexOf("/");
			temp = new StringBuffer(temp.substring(off, temp.length())).reverse();
			System.out.println(temp);
			localPath = new String(temp);
			refreshLocalPath();
			loadLocalFile();
		}
		if(e.getSource()==bt_localreturn){
			String dir = localPath.split("/")[0];
			String[] paths = localPath.split("//")[1].split("/");
			StringBuffer newPath = new StringBuffer();
			newPath.append(dir+"/");
			for(int i=0;i<paths.length-1;i++){
				newPath.append("/"+paths[i]);
			}
			localPath = new String(newPath);
			refreshLocalPath();
			loadLocalFile();			
		}
		if(e.getSource()==bt_remotereturn){	
			if("/".equals(T_remote.getText())){
				T_remote.setText("/");
				remotePath = "/";
			}else{
				String[] paths = remotePath.split("/");
				StringBuffer newPath = new StringBuffer();			
				for(int i=0;i<paths.length-1;i++){
					newPath.append(paths[i]+"/");
				}
				remotePath = new String(newPath);	
			}					
			refreshRemotePath();
			loadRemoteFile();			
		}		
		if(e.getSource()==T_local){
			if(T_local.getText().trim()!=null&&!"".equals(T_local.getText().trim())){
				localPath = T_local.getText().trim().replace('\\', '/'); 
				refreshLocalPath();
				loadLocalFile();	
			}
		}
		if(e.getSource()==T_remote){
			if(T_remote.getText().trim()!=null&&!"".equals(T_remote.getText().trim())){
				remotePath = T_remote.getText().trim(); 
				refreshRemotePath();
				loadRemoteFile();	
			}
		}
		if(e.getSource()==Lrename){
			InstallData d = (InstallData)list_local.getSelectedValue();
			if(d!=null){
				FileBean fb = (FileBean)d.getValue();	
				ReNameFrame ref = new ReNameFrame(fb,Lx,Ly,"LOCAL",null);
				ref.addObserver(this);
			}
		}
		if(e.getSource()==Ldelete){
			InstallData d = (InstallData)list_local.getSelectedValue();
			if(d!=null){
				FileBean fb = (FileBean)d.getValue();
				int result = JOptionPane.showOptionDialog(this, fb.getType().equals("文件夹")?"是否确定删除文件夹:"+fb.getFileName():"是否确定删除文件:"+fb.getFileName(), "删除文件", JOptionPane.YES_NO_OPTION, JOptionPane.DEFAULT_OPTION, null, new Object[]{"是","否"}, JOptionPane.YES_OPTION);
				if(result==0){
					new File(fb.getFilePath()).delete();
					loadLocalFile();
				}
			}
		}
		if(e.getSource()==Lproperty){
			InstallData d = (InstallData)list_local.getSelectedValue();
			if(d!=null){
				FileBean fb = (FileBean)d.getValue();
				new PropertyWindow(fb,Lx,Ly).setVisible(true);
			}			
		}
		if(e.getSource()==Lupload){
			InstallData d = (InstallData)list_local.getSelectedValue();
			if(d!=null){
				FileBean fb = (FileBean)d.getValue();
				/**
				 * clear the list;
				 * */
				for(int i=0;i<upList.size();i++){
					upList.remove(i);
				}
				
				System.out.println("RemotePath : "+remotePath);
				if(upLoad(remotePath, fb.getFilePath(), ftp)){
					for(int i=0;i<upList.size();i++){
						UpLoadBean ub = upList.get(i);
						new Thread(new upLoadThread(ub)).start();
					}
				}				
			}			
		}
		
		if(e.getSource()==Rrename){
			InstallData d = (InstallData)list_remote.getSelectedValue();
			if(d!=null){
				FileBean fb = (FileBean)d.getValue();	
				ReNameFrame rf = new ReNameFrame(fb,Rx,Ry,"REMOTE",ftp);
				rf.addObserver(this);
			}
		}
		if(e.getSource()==Rdelete){
			InstallData d = (InstallData)list_remote.getSelectedValue();
			if(d!=null){
				FileBean fb = (FileBean)d.getValue();
				if(fb.getType().equals("文件夹")){
					JOptionPane.showMessageDialog(this, "抱歉,系统暂不支持文件夹的删除","提示",JOptionPane.ERROR_MESSAGE);
					return;
				}
				int result = JOptionPane.showOptionDialog(this, fb.getType().equals("文件夹")?"是否确定删除文件夹:"+fb.getFileName():"是否确定删除文件:"+fb.getFileName(), "删除文件", JOptionPane.YES_NO_OPTION, JOptionPane.DEFAULT_OPTION, null, new Object[]{"是","否"}, JOptionPane.YES_OPTION);
				if(result==0){
					if(ftp.serverIsOpen()){					
						try {
							String cmd = "DELE "+fb.getFilePath()+"\r\n";

⌨️ 快捷键说明

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