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

📄 tbtest.java

📁 java实现的ftp文件传输
💻 JAVA
字号:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException; 
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class TBtest 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 int bufsize = 4096;
	static TBdl dl1;
	static TBdl dl2;
	static TBupl upl1;
	static TBupl upl2;
	static WByte wbyte1;
	static WByte wbyte2;
	
	public static void main(String[] args) throws IOException, InterruptedException{
		System.out.println("tbtest~~~~~~~~~~~~~~~~~~~~~~");
		ftpdl = new FTPClient();
		ftpupl = new FTPClient();
		username1 = "anonymous";
		pwd1 = "123456";
		username2 = username1;
		pwd2 = pwd1;
		filename = "wulin.wma";
		src = "10.1.168.251";
		path = "娱乐/音乐/like/壮志在我胸_《江湖再见》主题曲.wma";
//		path = "统计.txt";
		dst = "10.1.168.252";
		wbyte1 = new WByte(bufsize);
		wbyte2 = new WByte(bufsize);
		OutputStream foutupl = null;
		BufferedOutputStream boutupl = null;
		InputStream findl = null;
		BufferedInputStream bindl = null;

		String server = "wulin.ForTest";
		System.out.println(server + " is testing.");
//		取时间
		long time1 = System.currentTimeMillis();
		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();
		}
		
//		获取时间
//		long time2 = System.currentTimeMillis();
//		System.out.println("1current time is : " + (time2-time1));
//		传输部分		
		try{
			foutupl = ftpupl.appendFileStream(filename);
			boutupl = new BufferedOutputStream(foutupl);
			findl = ftpdl.retrieveFileStream(path);
			bindl = new BufferedInputStream(findl);
		}catch(Exception e){
			System.out.println("error occur when open stream.");
		}
		
		dl1 = new TBdl(wbyte1,bindl,ftpdl);
		dl2 = new TBdl(wbyte2,bindl,ftpdl);
		upl1 = new TBupl(wbyte1,boutupl,ftpupl);
		upl2 = new TBupl(wbyte2,boutupl,ftpupl);
//		upl = new TBupl(wbyte2,ftpupl,filename);
		
		System.out.println("start....");
		
//		Thread tdl1 = new Thread(dl1);//下一个缓冲区
//		Thread tdl2 = new Thread(dl2);
//		Thread tupl1 = new Thread(upl1);
//		Thread tupl2 = new Thread(upl2);
//		Thread tupl = new Thread(upl);
		
		Thread tdl1;//下一个缓冲区
		Thread tdl2;
		Thread tupl1;
		Thread tupl2;
//		写缓冲区1
//		System.out.println("currenThread is : " + Thread.currentThread().getName());
//		Thread.currentThread().setPriority(3);
//		tdl1.setPriority(5);
		tdl1 = new Thread(dl1);
//		tdl2 = new Thread(dl2);
		
//		获取时间
//		long time3 = System.currentTimeMillis();
//		System.out.println("2current time2 is : " + (time3-time2));
		tdl1.start();
		tdl1.join();
		//开始双线程工作
		while(wbyte1.flag_over != 1 && wbyte2.flag_over != 1){
			if(wbyte2.flag_busy == 0 && wbyte2.flag_empty == 1){
//				tdl2.setPriority(4);
//				tupl1.setPriority(4);
				tdl2 = new Thread(dl2);
				upl1 = new TBupl(wbyte1,boutupl,ftpupl);
				tupl1 = new Thread(upl1);
				tdl2.start();//给出是否结束
				tupl1.start();//判断是否均空且均结束
				tdl2.join();
				tupl1.join();
			}
			else
				if(wbyte1.flag_busy == 0 && wbyte1.flag_empty == 1){
//					tdl1.setPriority(4);
//					tupl2.setPriority(4);
					tdl1 = new Thread(dl1);
					upl2 = new TBupl(wbyte2,boutupl,ftpupl);
					tupl2 = new Thread(upl2);
					tdl1.start();
					tupl2.start();
					tdl1.join();
					tupl2.join();
				}
		}
//		最后收尾
		if(wbyte1.flag_over == 1 && wbyte2.flag_empty == 0){
			tupl2 = new Thread(upl2);
			tupl2.start();
		}
		if(wbyte2.flag_over == 1 && wbyte2.flag_empty == 0){
			tupl1 = new Thread(upl1);
			tupl1.start();
		}
		long time4 = System.currentTimeMillis();
//		System.out.println("3current time is : " + (time4 - time3));
		System.out.println("total time is :" + (time4 - time1) + "ms.");
		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 + -