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

📄 mainframe.java

📁 ftp 客服端和服务器端
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
							System.out.println(cmd);
							ftp.sendServer(cmd);
							ftp.binary();							
							int reply = ftp.readServerResponse();//等待返回结果
							if(reply==200){
								loadRemoteFile();
							}
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							JOptionPane.showMessageDialog(this, "删除文件失败 异常信息("+e1.getMessage()+")","错误",JOptionPane.ERROR_MESSAGE);
							e1.printStackTrace();
						}					
					}
				}				
			}
		}
		if(e.getSource()==Rproperty){
			InstallData d = (InstallData)list_remote.getSelectedValue();
			if(d!=null){
				FileBean fb = (FileBean)d.getValue();	
				new PropertyWindow(fb,Rx,Ry).setVisible(true);
			}
		}
		if(e.getSource()==Rload){
			loadRemoteFile();
		}
		if(e.getSource()==Rdownload){			
			InstallData d = (InstallData)list_remote.getSelectedValue();
			if(d!=null){
				FileBean fb = (FileBean)d.getValue();	
				FileDownLoadUtil down = new FileDownLoadUtil();
				down.addObserver(this);
				if(fb.getType().equals("文件夹")){
					down.download(remotePath+fb.getFileName()+"/", localPath+"/"+fb.getFileName(), fb, host, userName, passWord);
				}else{
					down.download(remotePath, localPath, fb, host, userName, passWord);
				}
			}			
		}
	}	
	
	
	
	    public class removeTB implements Runnable{

	    	boolean canBeClear = true;
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Label:while(ftp.serverIsOpen()){					
					if(upList!=null&&upList.size()>0){//所有线程已经执行完毕						
						for(int i=0;i<tb_progress.getRowCount();i++){
							if(!"上传完毕".equals(tb_progress.getModel().getValueAt(i, 4))){
								continue Label;
							}//end if
						}//end for
						clearInfo();
					}				//end if	
				}				//end while
			}    	//end run
	    }
	
	/***
	 * 文件上传处理类
	 * @author lzkj
	 *
	 */
		public class upLoadThread implements Runnable{
		private UpLoadBean ub;
		private FtpClient ftpClient;	
		
		public upLoadThread(UpLoadBean ub){
			this.ub = ub;			
			this.ftpClient = new FtpClient();
			ftpClient = getFtp();						
		}
		
		public void upLoadFile(FileBean fb,String path){
			TelnetOutputStream os = null;  
			FileInputStream is = null ; 			
			try {
				ftpClient.cd(path);
				ftpClient.binary();
				os = ftpClient.put(fb.getFileName());  
				is = new FileInputStream(new File(fb.getFilePath()));  
				byte[] bytes = new byte[1024];
				int tbt = 0;
				int size = (int)fb.getSize();				
			    int c;  
			    long s = System.currentTimeMillis();
			    long e;					
				try {
					Thread.currentThread().sleep(1000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			    while ((c = is.read(bytes)) != -1) {					   
			    	os.write(bytes, 0, c);
			    	e = System.currentTimeMillis()+10;
			    	tbt+=c;
			    	tb_progress.getModel().setValueAt(size<1?"100%":((tbt*100)/size)+"%", ub.getRowIndex(), 2);
			    	tb_progress.getModel().setValueAt(e-s<1?"0 (kb/s)":(1024*tbt)/((e-s)*1000)+" (kb/s)", ub.getRowIndex(), 3);
			    	tb_progress.updateUI();
			    }  
			    tb_progress.getModel().setValueAt("上传完毕", ub.getRowIndex(), 4);					    
			    tb_progress.updateUI();
//			    repaint();
				os.close();
				is.close();											
			} catch (IOException e1) {
				tb_progress.getModel().setValueAt("0%", ub.getRowIndex(), 2);
		    	tb_progress.getModel().setValueAt("0 (kb/s)", ub.getRowIndex(), 3);
		    	tb_progress.getModel().setValueAt("上传失败("+e1.getMessage()+")", ub.getRowIndex(), 4);					    
			    tb_progress.updateUI();
				e1.printStackTrace();
			}finally{
				try {
					if(os!=null&&is!=null){
						os.close();
						is.close();	
//						ftpClient.closeServer();
					}					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			if(ftpClient.serverIsOpen()){
				if(ub.getFb().getType().equals("文件")){					
					upLoadFile(ub.getFb(),ub.getRemotePath());
				}
			}//end if
			else{//服务器已经关闭
				JOptionPane.showMessageDialog(MainFrame.this, "服务器已经关闭,操作失败","错误",JOptionPane.ERROR_MESSAGE);
			}
			try {
				Thread.currentThread().sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}//end run			
	}


	
	/**
	 * 文件下载处理类
	 * @author lzkj
	 *
	 */
	public class downLoadThread implements Runnable{

		private FileBean fb;
		
		public downLoadThread(FileBean fb){
			this.fb = fb;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			
		}
		
	}

	
	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		if(arg instanceof String){
			String temp = (String)arg;
			System.out.println(arg);
			if(temp.equals("LOCAL_RENAME_OK")){
				loadLocalFile();
			}
			if(temp.equals("REMOTE_RENAME_OK")){
				loadRemoteFile();
			}
			if(temp.equals("DOWNLOAD_FINISH")){
				loadLocalFile();
//				JOptionPane.showMessageDialog(null, "下载文件成功","提示",JOptionPane.DEFAULT_OPTION);
			}
		}
	} 
	
	
	public  boolean upLoad(String Rpath,String fileName,FtpClient ftp){
		System.out.println("Rpath:"+Rpath);
		System.out.println("fileName:"+fileName);
		String localRootPath = PathUtil.rePlace(fileName);
		System.out.println("localRootPath:"+localRootPath);
		String remoteRootPath = Rpath;
		File rootFile = new File(fileName);
		if(rootFile.isDirectory()){	
			if(!createDir(remoteRootPath,PathUtil.getFolderName(fileName),ftp)){
				System.out.println("创建远程根文件夹"+remoteRootPath+fileName+"/失败");
				return false;
			}
			remoteRootPath = remoteRootPath+PathUtil.getFolderName(fileName)+"/";
			String[] fs = rootFile.list();		
			for(int i=0;i<fs.length;i++){
				File subFile = new File(localRootPath+"/"+fs[i]);
				if(subFile.isDirectory()){
					upLoad(remoteRootPath,localRootPath+"/"+fs[i],ftp);//递归		
				}//end if
				else{
					//文件上传
					FileBean fb = new FileBean();
					fb.setFileName(subFile.getName());
					fb.setFilePath(subFile.getAbsolutePath());
					fb.setSize(subFile.length());
					fb.setTime(new Long(subFile.lastModified()).toString());
					fb.setType("文件");
//					new Thread(new upLoadThread(fb,remoteRootPath)).start();
					UpLoadBean ub = new UpLoadBean();
					ub.setFb(fb);
					ub.setRemotePath(remoteRootPath);
					upList.add(ub);
					int size = (int)fb.getSize();
					int bk = (size/1024)<1?1:(size/1024);
					String bsize = bk+"K";
					if(bk>1024){
						bk = bk/1024;
						bsize = bk+"M";
					}
					Object[] obj = new Object[]{fb.getFileName(),bsize,"0%","0 (kb/s)","准备上传"};
					((javax.swing.table.DefaultTableModel)(tb_progress.getModel())).addRow(obj);
					tb_progress.updateUI();
					ub.setRowIndex(RowCount++);
				}
			}//end for
		}else{
			//文件上传
			FileBean fb = new FileBean();
			fb.setFileName(rootFile.getName());
			fb.setFilePath(rootFile.getAbsolutePath());
			fb.setSize(rootFile.length());
			fb.setTime(new Long(rootFile.lastModified()).toString());
			fb.setType("文件");
//			new Thread(new upLoadThread(fb,remoteRootPath)).start();
			UpLoadBean ub = new UpLoadBean();
			ub.setFb(fb);
			ub.setRemotePath(remoteRootPath);
			upList.add(ub);
			int size = (int)fb.getSize();
			int bk = (size/1024)<1?1:(size/1024);
			String bsize = bk+"K";
			if(bk>1024){
				bk = bk/1024;
				bsize = bk+"M";
			}
			Object[] obj = new Object[]{fb.getFileName(),bsize,"0%","0 (kb/s)","准备上传"};
			((javax.swing.table.DefaultTableModel)(tb_progress.getModel())).addRow(obj);	
			tb_progress.updateUI();
			ub.setRowIndex(RowCount++);
		}	
		System.err.println(RowCount);
		return true;
	}
	
	public  boolean createDir(String Rpath,String fileName,FtpClient ftp){
		boolean flag = false;
		if(!isExsitDir(Rpath+fileName,ftp)){
			try {
				String cmd = "MKD "+Rpath+fileName+"/"+"\r\n";				
				System.err.println("CMD : "+cmd);
				ftp.sendServer(cmd);
				ftp.binary();
				int reply = ftp.readServerResponse();
				System.err.println("Reply : "+reply);
				flag = true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}
	
	public  boolean isExsitDir(String dir,FtpClient ftpClient){
		try {
			ftpClient.cd(dir);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("文件夹不存在.......................");
//			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public FtpClient getFtp(){		
		try {
			FtpClient fc =  new FtpClient();
			fc.openServer(host);
			fc.login(userName, passWord);
			return fc;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return null;
	}
}

⌨️ 快捷键说明

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