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

📄 inceptthread.java

📁 JAVA实现的网络服务器文件同步
💻 JAVA
字号:
package com.msd;

import java.net.*;
import java.io.*;
import sun.misc.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

public class InceptThread implements Runnable {

	private String userName = "MSDWAP";
	private String password = "GTimeFileSend";
	private String SALT = "3GHficCedsVbR+956-ONjWNoKis4sBt9";
	private long valTime = 0L;
	private static final String[] hexDigits = {
      "0", "1", "2", "3", "4", "5", "6", "7",
      "8", "9", "a", "b", "c", "d", "e", "f"};
	private int soTimeout = 30000;
	
	private String idKey = null;
	
	public InceptThread(String _idKey,int _soTimeout) {
		this.idKey = _idKey;
		this.soTimeout = _soTimeout;
	}

   	public void run() {
       	try {
			Socket socket = null;
			DataInputStream in = null;
			DataOutputStream out = null;

			while((ServerThreadFactory.getInstance()).isRunning(idKey)) {
				//===========获得SOCKET================//
				socket = (SocketList.getInstance()).getSocket(); 
				//===========建立IO对象================//
				try {
					socket.setSoTimeout(soTimeout);
					in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
					out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
				} catch(Exception createIOException) {
					Log.logger.error("INCEPTThread CreateIOException ERROR :" + createIOException);
					if(in != null) {
						try {
							in.close();
							in = null;
						} catch(Exception e1) { }
					}
					if(out != null) {
						try {
							out.close();
							out = null;
						} catch(Exception e2) { }
					}
					if(socket != null) {
						try {
							socket.close();
							socket = null;
						} catch(Exception e3) { };
					}
					continue;
				} 
				//=============END====================//
				
              	try {
					int timeoutNum = 0;
                 	while(true) {
                 		//==============command : 0x1:bind,0x2:unbind,0x3:sendFile==============//
                    	int command = 0;
				    	byte[] inByte = null;

						try {
							int len = in.readInt();
							command = in.readInt();
														
							inByte = new byte[len - 8];
							in.readFully(inByte);							
						} catch(SocketTimeoutException e1) {
							if(timeoutNum < 3) {
								timeoutNum++;
								continue;
							} else {
								break;
							}
						} catch(ConnectException e2) {
							break;
						} catch(Exception e3) {
							break;
						}

						if(command == 0x1) {
							try {
								String psdTmp = new String(decryptMode(getFromBASE64(SALT),inByte));
								int s1 = psdTmp.indexOf("$");	
								int s2 = psdTmp.lastIndexOf("$");
								if(s1 > 0 && s2 > s1 && s2 < (psdTmp.length() - 1)) {
									String usTmp = psdTmp.substring(0,s1);
									long valTmp = Long.parseLong(psdTmp.substring(s1 + 1,s2));
									String pdTmp = psdTmp.substring(s2 + 1);
																		
									if(usTmp.equals(userName) && pdTmp.equals(password) && valTmp > valTime) {
										
										byte[] bindTmp = new byte[9];
										appendInt(9,bindTmp,0);
                               			appendInt(0x11,bindTmp,4);
                               			bindTmp[8] = (byte)0;
                               	                              			
                               			out.write(bindTmp,0,9);
							   			out.flush();
							   			bindTmp = null;
							   			inByte = null;
							   			continue;

									} else {
																				
										byte[] unBindTmp = new byte[9];
										appendInt(9,unBindTmp,0);
                               			appendInt(0x11,unBindTmp,4);
                               			unBindTmp[8] = (byte)1;
                               			
                               			out.write(unBindTmp,0,9);
							   			out.flush();
							   			unBindTmp = null;
							   			inByte = null;
										break;		
											
									}								
								} else {
									inByte = null;
									break;									
								}
							} catch(Exception e) {
								Log.logger.error("InceptThread bind Error:" + e.toString());
								inByte = null;
								break;	
							}	
						} else if(command == 0x2) {
							inByte = null;
							break;
						} else if(command == 0x3) {
							DataInputStream dis = null;
							try {								
								dis = new DataInputStream(new ByteArrayInputStream(inByte));
								String md5Tmp = readString(32,dis);
								String fileName = readString(20,dis);
								byte flag = dis.readByte();
								byte[] fileTmp = readBytes(inByte.length - 53,dis);
																
								String fileMd5 = MD5Encode(fileTmp);							
								if(fileMd5.equals(md5Tmp)) {
									(FileList.getInstance()).addFile(flag,fileName,fileTmp);
									
									byte[] sendSUC = new byte[9];
									appendInt(9,sendSUC,0);
                           			appendInt(0x13,sendSUC,4);
                           			sendSUC[8] = (byte)0;
                           			
                           			out.write(sendSUC,0,9);
						   			out.flush();
						   			sendSUC = null;
						   			inByte = null;						   			
						   			continue;
									
								} else {
									
									byte[] sendFla = new byte[9];
									appendInt(9,sendFla,0);
                           			appendInt(0x13,sendFla,4);
                           			sendFla[8] = (byte)1;
                           			
                           			out.write(sendFla,0,9);
						   			out.flush();
						   			sendFla = null;
						   			inByte = null;						   			
									continue;		
								}							
								
							} catch(Exception e) {
								Log.logger.error("InceptThread avlFile Error :" + e.toString());
								inByte = null;
								break;								
							} 
						} else {
							inByte = null;
							break;	
						}	
					}			
				} catch(Exception e) {
					Log.logger.error("InceptThread do Error: " + e.toString());	
				}		

				try {
					in.close();
					in = null;
				} catch(Exception e1) { }
				
				try {
					out.close();
					out = null;
				} catch(Exception e2) { }
				
				try {
					socket.close();
					socket = null;				 
				} catch(Exception e3) { }
			}	   
		} catch(Exception e) {
			Log.logger.error("InceptThread " + idKey + " run Exception:" + e);
			(ServerThreadFactory.getInstance()).ExceptionOut(idKey);
		}
	}

	private String readString(int len,DataInputStream dist) throws IOException {
		byte[] tmp = new byte[len];
		dist.readFully(tmp);
		String tmpStr = new String(tmp,0,len);
		return tmpStr.trim();
	} 
   
	private byte[] readBytes(int len,DataInputStream dis) throws Exception{
		byte[] tmp = new byte[len];
		dis.readFully(tmp);
		return tmp;
	}
 
	private void appendInt(int v,byte[] b,int i) {
		b[i + 0] = (byte)((v >>> 24) & 0xFF);
		b[i + 1] = (byte)((v >>> 16) & 0xFF);
		b[i + 2] = (byte)((v >>> 8 ) & 0xFF);
		b[i + 3] = (byte)((v >>> 0 ) & 0xFF);
	}
   
   //===================验证加密,解密===================//
	private byte[] getFromBASE64(String rn) {
		if(rn == null) 
			return null;
	 	BASE64Decoder decoder = new BASE64Decoder();
	  	try {
	    	return decoder.decodeBuffer(rn);
	  	} catch (Exception e) {
	    	return null;
	  	}
	}
	
    private byte[] decryptMode(byte[] keybyte, byte[] src) {      
		try {
		    SecretKey deskey = new SecretKeySpec(keybyte,"DESede");
		    Cipher ci = Cipher.getInstance("DESede");
		    ci.init(Cipher.DECRYPT_MODE, deskey);
		    return ci.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
		    Log.logger.error("InceptThread decryptMode Error1: " + e1.toString());
		} catch (javax.crypto.NoSuchPaddingException e2) {
		    Log.logger.error("InceptThread decryptMode Error2: " + e2.toString());
		} catch (java.lang.Exception e3) {
		    Log.logger.error("InceptThread decryptMode Error3: " + e3.toString());
		}
		return null;
    }
    
	private String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}
	
	private String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n = 256 + n;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}
	
	private String MD5Encode(byte[] source) {
		String resultString = "";
		try {
		  MessageDigest md = MessageDigest.getInstance("MD5");
		  resultString = byteArrayToHexString(md.digest(source));
		} catch (Exception ex) {  }
		return resultString;
	}
	//======================END======================//
}

⌨️ 快捷键说明

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