³õÀ´Õ§µ½¡£¸÷λ´óÉñÃÇ°ï°ïæ¡£²»Ê¤¸Ð¼¤£¡
ÏÖÓÐÒ»ÏîÄ¿£¬ÐèÒªÔÚsocketͨÐÅʱ£¬serverÿСʱ·¢ËÍÒ»¸öÎļþ£¬clientÿСʱ½ÓÊÕÒ»¸öÎļþ£¬¸ÃÔõô×ö£¬Çó½â¡£ÄÜдµãα´úÂë¸üºÃÁË¡£
------½â¾ö·½°¸--------------------
»ù±¾Ë¼ÏëÊÇ£¬Ã¿Ò»¸öСʱ£¬¿Í»§¶ËÈ¥Á¬½Ó·þÎñÆ÷Ò»´Î£¬·þÎñÆ÷Ö»Òª½ÓÊÕµ½Á¬½Ó£¬¾ÍÍù¿Í»§¶Ë·¢ÎļþÊý¾Ý£¬¿Í»§¶Ë½ÓÊÕÍê³Éºó£¬¶Ï¿ªÁ¬½Ó£¬µÈÒ»¸öСʱºóÔÙÈ¥Á¬½Ó£¬Èç¹û·´¸´¡£¡£¡£ µ«ÔÚÕâÖÖÇé¿öÏ£¬Èç¹ûÒª±£Ö¤Ò»Ð¡Ê±£¨Æä´ÎûÓоø¶ÔµÄ±£Ö¤£©´«ÊäÒ»´ÎÎļþ£¬ÐèÒªÔÚ¿Í»§¶Ë¿ØÖƺÃ
------½â¾ö·½°¸--------------------
¹À¼ÆÂ¥ÉϽ²µÄ¸úû½²Ò»Ñù£¬Ëû¿ÉÒÔÐèÒªSocketͨÐÅÔ´Â룬ºÙ£¬SocketÈç¹û²»Ì«»á£¬¾ÍÓÃmina¿ò¼Ü°É£¬±ðÂñÌ«¶àµÄµØÀס£
------½â¾ö·½°¸--------------------
Ò²²»Çå³þÄãΪʲôҪÓÃsocket£¬Èç¹ûÊÇΪÁË»ñÈ¡Îļþ¿ÉÒÔÓÃFtpÏÂÔØ£¬¶¨Ê±Æ÷¿ÉÒÔÓãºtimer.schedule£¬
µ±È»¸ù¾Ý¾ßÌåÇé¿ö»¹¿ÉÒÔÓÃspringµÄquartz£¬Ö»ÐèÅäÖþÍÐС£²¿·Ösocket´úÂ룺
public class FtpClient {
private ClientView clientView;
/**
* Á½¸öSocket·Ö±ðÓÃÀ´´«ÊäÎļþºÍ¿ØÖÆÐÅÏ¢£¬ÀàËÆÓÚFtpµÄTcp 20¡¢21¡£ 20ÓÃÀ´´«ÊäÎļþ£¬21ÓÃÀ´´«Êä¿ØÖÆÐÅÏ¢¡£
* */
private Socket fileSocket, commandSocket;
// ÀûÓÃdis»ñÈ¡µ±Ç°Á¬½ÓµÄÊäÈëÁ÷£¬²¢ÀûÓô¦ÀíÁ÷½øÐзâ×°¡£
private DataInputStream dis;
// ÀûÓÃdos»ñÈ¡µ±Ç°Á¬½ÓµÄÊä³öÁ÷£¬²¢ÀûÓô¦ÀíÁ÷½øÐзâ×°¡£
private DataOutputStream dos;
// ѡȡĬÈÏ·¾¶±£´æÎļþ
private String fileDir = "c:/";
private boolean flag = true;
public FtpClient() {
super();
}
public FtpClient(ClientView clientView) {
super();
this.clientView = clientView;
}
// ½ÓÊÕÎļþ
public void receiveFile(Socket s, String str) throws Exception {
fileSocket = s;
dis = new DataInputStream(fileSocket.getInputStream());
String existFile = dis.readUTF();
clientView.getJta().append(existFile);
// Èç¹ûÎļþ²»´æÔÚ£¬Ôò²»ÓýÓÊÜÎļþ
if (existFile.startsWith("404 File does not exist"))
;
// Èç¹ûÎļþ´æÔÚ£¬Ôò½ÓÊÜÎļþ
else {
clientView.getJta().append("·¢ËÍÎļþÃû³É¹¦\n");
// ¹ØÁª±£´æÎļþ¼Ð
File f = new File(fileDir);// µ±Ç°¸ùĿ¼
File file = new File(f, "Downloads" + File.separator + str); // ±£´æµ½µ±Ç°Ä¿Â¼µÃDownloadsÎļþÀïÃæ
// ´«ÊäÎļþÇ°ÖØй¹ÔìSocket
fileSocket = new Socket(clientView.getIPtf().getText(), 5432);
// ½«Îļþ·â×°µ½Êä³öÁ÷ÖÐ
BufferedOutputStream buff = new BufferedOutputStream(
new FileOutputStream(file));
// »ñÈ¡µ±Ç°Á¬½ÓµÄÊäÈëÁ÷£¬²¢ÀûÓô¦ÀíÁ÷½øÐзâ×°¡£
dis = new DataInputStream(fileSocket.getInputStream());
byte[] line = new byte[102400];// ÿ´Î·¢Ë͵Ŀé´ò´óС
clientView.getJta().append("¿ªÊ¼½ÓÊÕÎļþ\n");
clientView.getJta().append("Îļþ´«ËÍÖÐ....\n");
int mount;
while ((mount = dis.read(line)) != -1) {
buff.write(line, 0, mount);
buff.flush();// Ë¢ÐÂ
Thread.sleep(50);// ÉèÖÃÒ»¸öµÈ´ýʱ¼ä£¬ÓÐÀûÓÚ½ÓÊÕÎļþ
}
clientView.getJta().append("finished\n");
buff.close();
}
fileSocket.close();
}
// ½¨Á¢Á¬½Ó
public void TestStart() {
clientView.getJta().append("Çë·¢ËÍÃüÁî: \n");
}
public void getCommand(String str) {
try {
// ÉèÖÃflag=falseÏ൱ÓÚ³ÌÐòÔÝÍ£
while (flag) {
commandSocket = new Socket(clientView.getIPtf().getText(),
Integer.parseInt(clientView.getJtf().getText()));
// ÀûÓÃdos»ñÈ¡µ±Ç°Á¬½ÓµÄÊä³öÁ÷£¬²¢ÀûÓô¦ÀíÁ÷½øÐзâ×°¡£
dos = new DataOutputStream(commandSocket.getOutputStream());
dos.writeUTF(str);
// ÀûÓÃdis»ñÈ¡µ±Ç°Á¬½ÓµÄÊäÈëÁ÷£¬²¢ÀûÓô¦ÀíÁ÷½øÐзâ×°¡£