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

📄 filedownloadutil.java

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

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Observable;

import javax.swing.JOptionPane;

import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;

import com.topking.ftp.bean.FileBean;

public class FileDownLoadUtil extends Observable{

	private String flag;
	
	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
		this.setChanged();
		this.notifyObservers(flag);
	}

	public  void download(String remotePath,String localRootPath,FileBean rFile,String host,String user,String pass){
		String Rpath = remotePath;
		String Lpath = localRootPath;
		System.out.println("download() "+Rpath+";"+Lpath+";"+rFile.getFileName());
		FtpClient ftp = getFtp(host,user,pass);
		if(rFile.getType().equals("文件")){
			System.err.println("执行if");
//			readRemoteFile(Rpath+rFile.getFileName()+"/",Lpath+"/"+rFile.getFileName(),rFile.getFileName(),ftp);	
			new Thread(new ReadFileFromRemote(Rpath+rFile.getFileName()+"/",Lpath+"/"+rFile.getFileName(),rFile.getFileName(),getFtp(host,user,pass))).start();	
		}else{
			String dir = Lpath;
			System.out.println("DIR "+dir);
			createLocalDir(dir);
			System.err.println("执行else");
			String list = listRemoteFile(remotePath,ftp);
			String[] info = list.split("\n");
			for(int i=0;i<info.length;i++){			
				info[i] = info[i].replaceAll(" ", "/");	
				while(info[i].contains("//")){
					info[i] = info[i].replaceAll("//", "/");
				}				
				String[] s = info[i].split("/");		
				String type = s[0];				
				String fname = s[s.length-1];
				if(!".".equals(fname)&&!"..".equals(fname)){
					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("文件夹");		
						download(Rpath+fname+"/",Lpath+"/"+fname,fb,host,user,pass);
					}else{//文件
						fb.setType("文件");
						new Thread(new ReadFileFromRemote(Rpath+fname+"/",Lpath+"/"+fname,fb.getFileName(),getFtp(host,user,pass))).start();	
					}
				}//end if
			}//end for
		}		//end else
		this.setFlag("DOWNLOAD_FINISH");		
	}
	
	public  void createLocalDir(String dir){		
		if(!isExistDir(dir)){
			new File(dir).mkdir();
		}		
	}
	
	public  boolean isExistDir(String dir){
		boolean flag = false;
		if(new File(dir).exists()){
			flag = true;
		}
		return flag;
	}
		
	
	public  String listRemoteFile(String path,FtpClient ftp){
    	String list = null;
    	try {
			ftp.cd(path);			
			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);			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
    }
	
	public  FtpClient getFtp(String host,String userName,String passWord){		
		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;
	}
	
	public class ReadFileFromRemote implements Runnable{

		private String remotePath;
		private String localPath;
		private String fileName;
		private FtpClient ftp;
		
		public ReadFileFromRemote(String remotePath,String localPath,String fileName,FtpClient ftp){
			this.remotePath = remotePath;
			this.localPath = localPath;
			this.fileName = fileName;
			this.ftp = ftp;
		}
		public void readRemoteFile(){
			localPath = localPath.replace("//", "/");
			System.out.println("readRemoteFile "+remotePath);
			System.out.println("localPath: "+localPath);
			System.out.println("fileName: "+fileName);
			if(ftp.serverIsOpen()){
				File file = new File(localPath);
				try {		
//					String rfile = remotePath+"/"+fileName;
					String rfile = remotePath;
					System.out.println("********************************");
					System.out.println("RemoteFile : "+rfile);
					System.out.println("********************************");
					TelnetInputStream is = ftp.get(rfile);
//					ftp.binary();
//					System.out.println(ftp.readServerResponse());
					FileOutputStream os = new FileOutputStream(file);
					byte[] b = new byte[1];
					while(is.read(b)!=-1){
						os.write(b);
						os.flush();
					}					
					os.close();
					is.close();
					JOptionPane.showMessageDialog(null, "下载文件成功","提示",JOptionPane.DEFAULT_OPTION);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  			
			}else{
				JOptionPane.showMessageDialog(null, "服务器已经关闭,操作失败","错误",JOptionPane.ERROR_MESSAGE);
			}			
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			readRemoteFile();
		}
		
	}
}

⌨️ 快捷键说明

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