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

📄 transferboth.java

📁 java实现的ftp文件传输
💻 JAVA
字号:
import java.io.IOException; 
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class TransferBoth extends Thread{
	static FTPClient ftpdl;
	static FTPClient ftpupl;
	static String username1;
	static String pwd1;
	static String username2;
	static String pwd2;
	static String filename;
	static String src;
	static String dst;
	static String path;
	static byte[] bufdl;
	static byte[] bufupl;
	static int bufsize = 1024;
	static TransferBothUpL upl;
	static TransferBothDL dl;
	static int read;
	static int len;
	static WByte wbyte1;
	static WByte wbyte2;
	static WByte wbyte3;
	static WByte wbyte4;
	public static void main(String[] args) throws IOException{
		ftpdl = new FTPClient();
		ftpupl = new FTPClient();
		username1 = "anonymous";
		pwd1 = "123456";
		username2 = username1;
		pwd2 = pwd1;
		filename = "wl.wma";
		src = "10.1.168.251";
		path = "娱乐/音乐/like/壮志在我胸_《江湖再见》主题曲.wma";
		dst = "10.1.168.252";
		wbyte1 = new WByte(bufsize);
		wbyte2 = new WByte(bufsize);
		wbyte3 = new WByte(bufsize);
		wbyte4 = new WByte(bufsize);

		String server = "wulin.ForTest";
		System.out.println(server + "is testing.");
		int reply1;
		int reply2;
		try {
			//中文识别
			ftpdl.setControlEncoding("gb2312");
			ftpupl.setControlEncoding("gb2312");
			ftpdl.connect(src);
			System.out.println("connect to " + src);
			reply1 = ftpdl.getReplyCode();
			ftpupl.connect(dst);
			System.out.println("connect to " + dst);
			reply2 = ftpupl.getReplyCode();
		if (!FTPReply.isPositiveCompletion(reply1)||!FTPReply.isPositiveCompletion(reply2)) {
			ftpdl.disconnect();
			ftpupl.disconnect();		
			System.err.println("FTP server refused connection.");
			System.exit(1);
			}
		}
		catch (IOException exp) {
			exp.printStackTrace();
		}
		try {
			ftpdl.login(username1,pwd1);
			ftpdl.setFileType(2);
			ftpdl.setFileTransferMode(0);
			reply1 = ftpdl.getReplyCode();
			
			ftpupl.login(username2,pwd2);
			ftpupl.setFileType(2);
			ftpupl.setFileTransferMode(0);
			reply2 = ftpupl.getReplyCode();
		}
		catch (IOException exp) {
			exp.printStackTrace();
		}
		
//		传输部分		
		dl = new TransferBothDL(wbyte1,wbyte2,wbyte3,wbyte4,ftpdl,path);
		upl = new TransferBothUpL(wbyte1,wbyte2,wbyte3,wbyte4,ftpupl,filename);
		
		System.out.println("start....");
		
		Thread tdl = new Thread(dl);
		Thread tupl = new Thread(upl);
		
		long times = System.currentTimeMillis();
		tdl.start();
		tupl.start();
		try{
			tdl.join();
			tupl.join();
		}catch(Exception e){
			System.out.println("wait has been interrupted.");
		}
		long timet = System.currentTimeMillis();
		System.out.println("total time consumes is : " + (timet - times));
		
		System.out.println("waitDl = " + dl.waitdl);
		System.out.println("waitUPL = " + upl.waitupl);
		System.out.println("process is over.");

		if(ftpdl.isConnected()||ftpupl.isConnected()){
			
			try {
				ftpdl.disconnect();
				ftpupl.disconnect();		
				System.out.println("disconnect form " + src + " success");
				System.out.println("disconnect form " + dst + " success");
			
			}catch(Exception e){
				System.out.println("error occur when disconnect from ftpserver.");
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

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