`
liufei.fir
  • 浏览: 685026 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

apache mina开发文件传输服务器

阅读更多
服务器接收端
/**
 * 
 */
package org.liufei.net.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.dom4j.DocumentException;
import org.liufei.net.ServerConfig;
import org.liufei.util.FileDesHandler;
import org.liufei.util.entity.FileDes;

/**
 * @author 刘飞
 * 
 */
public class FileReceiveHandler extends IoHandlerAdapter {
	private static final Logger logger = Logger.getLogger(FileReceiveHandler.class) ;
	private ServerConfig config = ServerConfig.getInstance() ;

	private FileChannel fileChannel = null;

	/**
	 * 
	 */
	public FileReceiveHandler() {
	}
	
	private void initFileChannel() throws DocumentException, IOException {
		String time = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(GregorianCalendar.getInstance().getTime()) ;
		File file = new File(
				config.directory(), 
				FileDesHandler.query(FileDes.FILE_DES_ID).getFileName()) ;
		
		if(!file.exists()) {
			file.createNewFile() ;
		}
		else {
			file = new File(
					config.directory(), 
					time + "_" +
					FileDesHandler.query(FileDes.FILE_DES_ID).getFileName()) ;
		}
		
		fileChannel = new FileOutputStream(file).getChannel() ;
	}
	
	private void closeFileChannel() {
		if(fileChannel != null) {
			try {
				if(fileChannel.isOpen()) {
					FileLock fileLock = fileChannel.lock() ;
					if(fileLock != null) {
						fileLock.release() ;
					}
					
					fileChannel.close() ;
					fileChannel = null ;
				}
			} catch (IOException e) {
				logger.error("文件解锁异常。", e) ;
				throw new RuntimeException("文件解锁异常。", e) ;
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#exceptionCaught(org.apache
	 * .mina.core.session.IoSession, java.lang.Throwable)
	 */
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		closeFileChannel() ;
		session.close(true) ;
		logger.error("file upload error .", cause) ;
		FileDesHandler.close() ;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#messageReceived(org.apache
	 * .mina.core.session.IoSession, java.lang.Object)
	 */
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		if(fileChannel == null){
			try {
				initFileChannel() ;
			} catch (DocumentException e) {
				logger.error("reading xml config error.", e) ;
			} catch (IOException e) {
				logger.error("init FileChannel error.", e) ;
			}
		}
		
		logger.debug("Message From IP >>> " + session.getRemoteAddress().toString() + " <<<") ;
		IoBuffer ib = (IoBuffer) message;
		logger.debug("\n\t\tDATA >>> " + message.toString() + "\n\t\tDATA SIZE >>> " + ib.capacity() + "\n") ;
		fileChannel.write(ib.buf());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#messageSent(org.apache.
	 * mina.core.session.IoSession, java.lang.Object)
	 */
	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		super.messageSent(session, message);
		logger.debug("super.messageSent(IoSession session, Object message)\n\t\t >>> " + session.getRemoteAddress().toString() + " <<<") ;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#sessionClosed(org.apache
	 * .mina.core.session.IoSession)
	 */
	@Override
	public void sessionClosed(IoSession session) throws Exception {
		super.sessionClosed(session);
		logger.debug("super.sessionClosed(IoSession session)\n\t\t >>> " + session.getRemoteAddress().toString() + " <<<") ;
		FileDesHandler.close() ;
		closeFileChannel() ;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#sessionCreated(org.apache
	 * .mina.core.session.IoSession)
	 */
	@Override
	public void sessionCreated(IoSession session) throws Exception {
		super.sessionCreated(session);
		logger.debug("super.sessionCreated(IoSession session)\n\t\t >>> " + session.getRemoteAddress().toString() + " <<<") ;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#sessionIdle(org.apache.
	 * mina.core.session.IoSession, org.apache.mina.core.session.IdleStatus)
	 */
	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
		super.sessionIdle(session, status);
		logger.debug("super.sessionIdle(IoSession session, IdleStatus status)\n\t\t >>> " + session.getRemoteAddress().toString() + " <<<") ;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#sessionOpened(org.apache
	 * .mina.core.session.IoSession)
	 */
	@Override
	public void sessionOpened(IoSession session) throws Exception {
		super.sessionOpened(session);
		logger.debug("super.sessionOpened(IoSession session)\n\t\t >>> " + session.getRemoteAddress().toString() + " <<<") ;
	}
}


package org.liufei.net.file;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.log4j.Logger;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.liufei.net.ServerConfig;

public class FileServer {
	private static final Logger logger = Logger.getLogger(FileServer.class) ;
	private ServerConfig config = ServerConfig.getInstance() ;

	public SocketAcceptor server() {
		int port = config.port() ;
		try {
			SocketAcceptor socketAcceptor = new NioSocketAcceptor();

			socketAcceptor.getFilterChain();
			socketAcceptor.setHandler(new FileReceiveHandler());
			socketAcceptor.bind(new InetSocketAddress(port));
			logger.debug("start NET FILE SERVER success !\n\t\t HTTP PORT >>> " + port + "\n") ;
			return socketAcceptor ;
		} catch (IOException e) {
			logger.error("start NET FILE SERVER error !\n\t\t HTTP PORT >>> " + port + "\n", e) ;
			throw new RuntimeException("start NET FILE SERVER error !\n\t\t HTTP PORT >>> " + port + "\n", e) ;
		}
	}
}

客户端
package org.liufei.net.file;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

/**
 * 
 * @author 刘飞
 * 
 */
public class FileSenderHandler extends IoHandlerAdapter {

	/**
	 * 
	 */
	public FileSenderHandler() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#exceptionCaught(org.apache
	 * .mina.core.session.IoSession, java.lang.Throwable)
	 */
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		super.exceptionCaught(session, cause);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#messageReceived(org.apache
	 * .mina.core.session.IoSession, java.lang.Object)
	 */
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		super.messageReceived(session, message);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#messageSent(org.apache.
	 * mina.core.session.IoSession, java.lang.Object)
	 */
	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		super.messageSent(session, message);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#sessionClosed(org.apache
	 * .mina.core.session.IoSession)
	 */
	@Override
	public void sessionClosed(IoSession session) throws Exception {
		super.sessionClosed(session);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#sessionCreated(org.apache
	 * .mina.core.session.IoSession)
	 */
	@Override
	public void sessionCreated(IoSession session) throws Exception {
		super.sessionCreated(session);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#sessionIdle(org.apache.
	 * mina.core.session.IoSession, org.apache.mina.core.session.IdleStatus)
	 */
	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
		super.sessionIdle(session, status);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.mina.core.service.IoHandlerAdapter#sessionOpened(org.apache
	 * .mina.core.session.IoSession)
	 */
	@Override
	public void sessionOpened(IoSession session) throws Exception {
		super.sessionOpened(session);
	}

}
package org.liufei.net.file;

import java.io.File;
import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.apache.log4j.Logger;
import org.apache.mina.core.RuntimeIoException;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.liufei.net.ClientConfig;
import org.liufei.net.util.FileDesHandler;
import org.liufei.net.util.JavaZip;
import org.liufei.net.util.entity.FileDes;

public class FileClient {
	private static final int CAPACITY = 2048 * 1000;
	private static final Logger logger = Logger.getLogger(FileClient.class) ;
	private ClientConfig config = ClientConfig.getInstance() ;
	
	private String host = "172.16.0.11";
	private int port = 8888;
	
	public static void main(String[] args) {
		logger.debug("please run new FileClient().upload(String/File)") ;
		System.out.println("please run new FileClient().upload(String/File)");
	}
	
	/**
	 * 只能一次上传一个文件
	 * @param path
	 * @return
	 */
	public boolean upload(String path) {
		return upload(new File(path)) ;
	}
	
	/**
	 * 只能一次上传一个文件
	 * @param file
	 * @return
	 */
	public boolean upload(File file) {
		if(file.isFile() && !file.isDirectory()) {
			return netFileUpload(file, FileDes.UNZIP_NO) ;
		}
		else if(file.isDirectory()){
			File zipFile = new File(file.getAbsolutePath() + ".zip");
			try {
				JavaZip.zip(file, zipFile);
				return netFileUpload(zipFile, FileDes.UNZIP_YES) ;
			} catch (Exception e) {
				logger.error("传输目录文件时压缩传输异常.", e) ;
				throw new RuntimeException("传输目录文件时压缩传输异常.", e) ;
			}
		}
		else {
			return false ;
		}
	}
	
	/**
	 * File f = new File("D:\\MyBatis 3 User Guide Simplified Chinese.pdf");
	 * 
	 * @param file
	 * @return
	 */
	private boolean netFileUpload(final File file, final int unZip_mark) {
		boolean flag = false ;
		host = config.host();
		port = config.port();
		
		NioSocketConnector connector = null ;
		ConnectFuture connectFuture = null ;
		IoSession session = null;
		
		try {
			connector = new NioSocketConnector();
			connector.getFilterChain();
			connector.setHandler(new FileSenderHandler());
			connectFuture = connector.connect(new InetSocketAddress(host, port));
			logger.debug("connect to host >>> " + host + " <<< success.") ;
			for (;;) {
				try {
					ConnectFuture future = connector.connect(new InetSocketAddress(host, port));
					future.awaitUninterruptibly();
					session = future.getSession();

					logger.debug("connect to host >>> " + host + " <<< success.") ;
					
					break;
				} catch (RuntimeIoException e) {
					logger.error("connect to host >>> " + host + " <<< error.", e) ;
					close(
							connector, e, 
							connectFuture, e,
							session, e
					) ;
					Thread.sleep(5000);
				}
			}
			
			FileInputStream in = new FileInputStream(file);
			FileChannel fileChannel = in.getChannel();
			ByteBuffer byteBuffer = ByteBuffer.allocate(CAPACITY);
			logger.debug("begin upload file >>> " + file.getAbsolutePath() + " <<<") ;
			
			FileDes fileDes = FileDesHandler.query(FileDes.FILE_DES_ID) ;
			String filePath = file.getAbsolutePath().replace(File.separatorChar, '/') ;
			if(fileDes == null) {
				fileDes = new FileDes() ;
				fileDes.setSource(filePath) ;
				fileDes.setUnZip(unZip_mark) ;
				fileDes.setId(FileDes.FILE_DES_ID) ;
				fileDes.setFileName(file.getName()) ;
				fileDes.setFileLength(file.length()) ;
				fileDes.setMark(FileDes.MARK_START) ;
				fileDes.setStatus(FileDes.STATUS_ERROR) ;
				fileDes.setEnd(FileDes.END_NO) ;
				FileDesHandler.insert(fileDes) ;
			}
			else {
				fileDes.setSource(filePath) ;
				fileDes.setUnZip(unZip_mark) ;
				fileDes.setFileName(file.getName()) ;
				fileDes.setFileLength(file.length()) ;
				fileDes.setMark(FileDes.MARK_START) ;
				fileDes.setStatus(FileDes.STATUS_ERROR) ;
				fileDes.setEnd(FileDes.END_NO) ;
				FileDesHandler.update(fileDes) ;
			}
			
			
			while (true) {
				/**
				 * 不间断发送会导致buffer异常
				 */
				Thread.sleep(1000);
				byteBuffer.clear();
				int i = fileChannel.read(byteBuffer);
				if (i == -1) {
					flag = true ;
					logger.debug("upload file >>> " + file.getAbsolutePath() + " <<< success .") ;
					
					fileDes.setFileName(file.getName()) ;
					fileDes.setFileLength(file.length()) ;
					fileDes.setSource(filePath) ;
					fileDes.setUnZip(unZip_mark) ;
					fileDes.setMark(FileDes.MARK_START) ;
					fileDes.setStatus(FileDes.STATUS_SUCESS) ;
					fileDes.setEnd(FileDes.END_YES) ;
					FileDesHandler.update(fileDes) ;
					FileDesHandler.close() ;
					close(
							connector, null, 
							connectFuture, null,
							session, null
					) ;
					if(unZip_mark == FileDes.UNZIP_YES) {
						logger.debug("delete zip file : " + file.getAbsolutePath()) ;
						file.delete() ;
					}
					break;
				}
				IoBuffer io_buffer_apache = IoBuffer.wrap(byteBuffer);
				byteBuffer.flip();
				Thread.sleep(1000);
				session.write(io_buffer_apache);
				Thread.sleep(1000);
			}
			close(
					connector, null, 
					connectFuture, null,
					session, null
			) ;
			session.close(true);

		} catch (Throwable e) {
			close(
					connector, e, 
					connectFuture, e,
					session, e
			) ;
			FileDesHandler.close() ;
			throw new RuntimeException("NET FILE SERVER UPLOAD FILE [" + file + "] [host:" + host + ", port:" + port + "] error >>>", e) ;
		}
		
		return flag ;
	}
	
	private void close(
			
			NioSocketConnector connector, 
			Throwable e1, 
			ConnectFuture connectFuture, 
			Throwable e2, 
			IoSession session, 
			Throwable e3
			
	) {
		if(connector != null) {
			if(connector.isActive()) {
				connector.dispose(true) ;
			}
			if(!connector.isDisposed()) {
				connector.dispose(true) ;
			}
			if(e1 != null) 
				logger.error("NIO Socket Connector [host:" + host + ", port:" + port + "] Dispose >>>", e1) ;
			else 
				logger.debug("NIO Socket Connector [host:" + host + ", port:" + port + "] Dispose >>>") ;
			connector = null ;
		}
		if(connectFuture != null) {
			if(connectFuture.isConnected()) {
				connectFuture.cancel() ;
			}
			if(!connectFuture.isCanceled()) {
				connectFuture.cancel() ;
			}
			connectFuture = null ;
			if(e2 != null) 
				logger.error("Connect Future [host:" + host + ", port:" + port + "] Cancel >>>", e2) ;
			else 
				logger.debug("Connect Future [host:" + host + ", port:" + port + "] Cancel >>>") ;
		}
		if(session != null) {
			if(session.isConnected()) {
				session.close(true) ;
			}
			session = null;
			if(e3 != null) 
				logger.error("IoSession [host:" + host + ", port:" + port + "] closing >>>", e3) ;
			else 
				logger.debug("IoSession [host:" + host + ", port:" + port + "] closing >>>") ;
		}
	}
}

服务器
package org.liufei.net;

import org.apache.log4j.Logger;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.liufei.net.file.FileServer;
import org.liufei.net.ui.ServerManager;

public class Server {
	
	private static final Server server = new Server() ;
	private ServerConfig config = ServerConfig.getInstance() ;
	private SocketAcceptor socketAcceptor = null ;
	
	/**
	 * net file server
	 */
	private Server() {
	}
	
	public static Server getInstance() {
		return server ;
	}
	
	/**
	 * 启动一个服务并返回当前服务实例
	 * @return
	 */
	public void start(){
		try {
			socketAcceptor = new FileServer().server() ;
			if(socketAcceptor != null) {
				START_FLAG.set(START_SUCCESS) ;
				logger.debug("start NET FILE SERVER SUCCESS.") ;
			}
			else {
				START_FLAG.set(START_ERROR) ;
				logger.debug("start NET FILE SERVER ERROR.") ;
			}
		} catch (Exception e) {
			START_FLAG.set(START_ERROR) ;
			logger.error("start error message.", e) ;
			ServerManager manager = ServerManager.getInstance() ;
			manager.setEnabledTrue() ;
		}
	}
	
	public void shutdown() {
		if(config == null) {
			config = ServerConfig.getInstance() ;
		}
		if(socketAcceptor != null) {
			try {
				if(socketAcceptor.isActive()) {
					socketAcceptor.unbind() ;
				}
				if(!socketAcceptor.isDisposed()) {
					socketAcceptor.dispose(true);
				}
				
				socketAcceptor = null ;
				logger.debug("\r\nNET FILE SERVER shut down on host [ " + config.host() + " ], port [ " + config.port() + " ] .\r\n") ;
			} catch (Exception e) {
				logger.error("shut down error message.", e) ;
			}
		}
	}
	
	/**
	 * 是否已启动成功
	 * @return
	 */
	public boolean isStarted() {
		Integer start_flag = START_FLAG.get() ;
		if(start_flag == START_SUCCESS)
			return true ;
		else if(start_flag == START_ERROR)
			return false ;
		else 
			return false ;
	}

	private static final Logger logger = Logger.getLogger(Server.class) ;
	
	private static final ThreadLocal<Integer> START_FLAG = new ThreadLocal<Integer>() ;
	
	/**
	 * 启动成功
	 */
	public static final Integer START_SUCCESS = 1 ;
	
	/**
	 * 启动失败
	 */
	public static final Integer START_ERROR = 0 ;
}

package org.liufei.net;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.liufei.net.ui.ServerManager;
import org.liufei.net.util.AddJavaLibraryPath;
import org.liufei.net.util.FileCopyUtils;
import org.liufei.net.util.RegeditTool;

/**
 * 
 * @author 刘飞
 *
 */
public class ServerRunner {
	private static final Logger logger = Logger.getLogger(ServerRunner.class) ;
	public static void main(String[] args) {
		try {
			if(ServerConfig.getInstance().regeditAuto()) {
				setAutoStart() ;
				logger.debug("set auto start success.") ;
			}
		} catch (Exception e) {
			logger.debug("set auto start error.") ;
			logger.error("set auto atart error message.", e) ;
		}
		logger.debug("show net file server manager.") ;
		ServerManager.showNetFileServer(true) ;
		logger.debug("show net file server manager end.") ;
	}
	
	public static void setAutoStart() throws DocumentException, IOException {
		if(ServerConfig.getInstance().isCopyDLL())
			FileCopyUtils.copy(new File("lib/ICE_JNIRegistry.dll"), new File(ServerConfig.getInstance().copyDLLDest())) ;
		String JAVA_LIBRARY_PATH = System.getProperty("java.library.path") ;
		
		printLibrary("JAVA_LIBRARY_PATH", JAVA_LIBRARY_PATH) ;
		
		String ADD_DLL_PATH = new File("lib/ICE_JNIRegistry.dll").getAbsolutePath() + File.pathSeparator + JAVA_LIBRARY_PATH ;
		
		System.setProperty("java.library.path", ADD_DLL_PATH) ;

		AddJavaLibraryPath.addDir(new File("lib").getAbsolutePath()) ;
		
		printLibrary("After Add DLL LIB JAVA_LIBRARY_PATH", System.getProperty("java.library.path")) ;
		
		loadLibrary() ;
		
		RegeditTool.regeditNetFileServer() ;
	}
	
	private static void loadLibrary() {
		try {
				System.loadLibrary( "ICE_JNIRegistry" );
			}
		catch ( UnsatisfiedLinkError e )
			{
				System.err.println
				( "ERROR You have not installed the DLL named '"
					+ "ICE_JNIRegistry.DLL'.\n\t" + e.getMessage() );
			}
		catch ( SecurityException e )
			{
				System.err.println
				( "ERROR You do not have permission to load the DLL named '"
					+ "ICE_JNIRegistry.DLL'.\n\t" + e.getMessage() );
			}
	}
	
	public static String printLibrary(String message, String java_library_path) {
		String java_library_path_temp = java_library_path ;
		StringBuffer buffer = new StringBuffer() ;
		
		String[] libs = java_library_path_temp.split(File.pathSeparator) ;
		for (String lib : libs) {
			buffer.append(lib + "\n") ;
		}
		logger.debug("\n" + message + "(number = " + libs.length + ") : \n" + buffer.toString()) ;
		return buffer.toString() ;
	}
}

工具
package org.liufei.net.util;

import java.io.IOException;
import java.lang.reflect.Field;

/**
 * 动态加载增加java.library.path属性
 * 
 * @author 刘飞
 * 
 */
public class AddJavaLibraryPath {

	/**
	 * 通过反射实现动态增加java.library.path 本方法和jvm加联,用到了ClassLoader里的usr_paths。
	 * 
	 * @param s
	 * @throws IOException
	 */
	public static void addDir(String s) throws IOException {
		try {
			Field field = ClassLoader.class.getDeclaredField("usr_paths");
			field.setAccessible(true);
			String[] paths = (String[]) field.get(null);
			for (int i = 0; i < paths.length; i++) {
				if (s.equals(paths[i])) {
					return;
				}
			}
			String[] tmp = new String[paths.length + 1];
			System.arraycopy(paths, 0, tmp, 0, paths.length);
			tmp[paths.length] = s;
			field.set(null, tmp);
		} catch (IllegalAccessException e) {
			throw new IOException(
					"Failed to get permissions to set library path");
		} catch (NoSuchFieldException e) {
			throw new IOException(
					"Failed to get field handle to set library path");
		}
	}
}

package org.liufei.net.util;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.liufei.net.ServerConfig;

import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.NoSuchValueException;
import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;

/**
 * java 操作注册表
 * 
 * @author 刘飞
 */
public class RegeditTool {
	private static final Logger logger = Logger.getLogger(RegeditTool.class);

	/**
	 * 把信息存储到注册表HKEY_LOCAL_MACHINE下的某个节点的某一变量中,有则修改,无则创建
	 * 
	 * @param folder
	 * @param subKeyNode
	 * @param subKeyName
	 * @param subKeyValue
	 * @return
	 */
	public static boolean setValue(String folder, String subKeyNode,
			String subKeyName, String subKeyValue) {
		try {
			RegistryKey software = Registry.HKEY_LOCAL_MACHINE
					.openSubKey(folder);
			RegistryKey subKey = software.createSubKey(subKeyNode, "");
			subKey
					.setValue(new RegStringValue(subKey, subKeyName,
							subKeyValue));
			subKey.closeKey();
			return true;
		} catch (NoSuchKeyException e) {
			logger.error("No Such Key.", e);
		} catch (NoSuchValueException e) {
			logger.error("No Such Value.", e);
		} catch (RegistryException e) {
			logger.error("Registry Error.", e);
		}
		return false;
	}

	/**
	 * 删除注册表中某节点下的某个变量
	 * 
	 * @param folder
	 * @param subKeyNode
	 * @param subKeyName
	 * @return
	 */
	public static boolean deleteValue(String folder, String subKeyNode,
			String subKeyName) {

		try {
			RegistryKey software = Registry.HKEY_LOCAL_MACHINE
					.openSubKey(folder);
			RegistryKey subKey = software.createSubKey(subKeyNode, "");
			subKey.deleteValue(subKeyName);
			subKey.closeKey();
			return true;
		} catch (NoSuchKeyException e) {
			logger.error("No Such Key.", e);
		} catch (NoSuchValueException e) {
			logger.error("No Such Value.", e);
		} catch (RegistryException e) {
			logger.error("Registry Error.", e);
		}
		return false;
	}

	/**
	 * 删除注册表中某节点下的某节点
	 * 
	 * @param folder
	 * @param subKeyNode
	 * @return
	 */
	public static boolean deleteSubKey(String folder, String subKeyNode) {
		try {
			RegistryKey software = Registry.HKEY_LOCAL_MACHINE
					.openSubKey(folder);
			software.deleteSubKey(subKeyNode);
			software.closeKey();
			return true;
		} catch (NoSuchKeyException e) {
			logger.error("No Such Key.", e);
		} catch (NoSuchValueException e) {
			logger.error("No Such Value.", e);
		} catch (RegistryException e) {
			logger.error("Registry Error.", e);
		}
		return false;
	}

	/**
	 * 打开注册表项并读出相应的变量名的值
	 * 
	 * @param folder
	 * @param subKeyNode
	 * @param subKeyName
	 * @return
	 */
	public static String getValue(String folder, String subKeyNode,
			String subKeyName) {
		String value = "";
		try {
			RegistryKey software = Registry.HKEY_LOCAL_MACHINE
					.openSubKey(folder);
			RegistryKey subKey = software.openSubKey(subKeyNode);
			value = subKey.getStringValue(subKeyName);
			subKey.closeKey();
		} catch (NoSuchKeyException e) {
			logger.error("No Such Key.", e);
		} catch (NoSuchValueException e) {
			logger.error("No Such Value.", e);
		} catch (RegistryException e) {
			logger.error("Registry Error.", e);
		}
		return value;
	}

	public static void regeditNetFileServer() throws DocumentException {
		setValue(
				"SOFTWARE", 
				"Microsoft\\Windows\\CurrentVersion\\Run",
				ServerConfig.getInstance().regeditKey(), 
				ServerConfig.getInstance().regeditValue()
				);
	}
}
package org.liufei.util.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Logger;

/**
 * JDBC事物工具。
 * 
 * @author 刘飞
 * 
 */
public abstract class JdbcTransaction<T> {
	private Logger logger = Logger.getLogger(JdbcTransaction.class);

	private Connection conn;
	private boolean isCloseConnection = false;

	/**
	 * 自动控制的事物块。
	 * 
	 * @param conn
	 *            数据库连接
	 * @param isCloseConnection
	 *            完成事物以后是否关闭连接, 默认为false不关闭。
	 */
	public JdbcTransaction(Connection conn, boolean isCloseConnection) {
		super();
		this.conn = conn;
		this.isCloseConnection = isCloseConnection;
	}

	public final T doInTransaction() throws SQLException {
		T result = null;

		boolean autoCommit = false;

		if (this.conn != null) {
			try {

				/**
				 * 保存Connection原始属性
				 */
				autoCommit = this.conn.getAutoCommit();

				/**
				 * 在本代码快里设置Connection非自动提交
				 */
				if (this.conn.getAutoCommit()) {
					this.conn.setAutoCommit(false);
				}

				/**
				 * 执行事务代码块
				 */
				result = this.doInTransaction(this.conn);

				/**
				 * 提交事务
				 */
				this.conn.commit();

				/**
				 * 恢复Connection的原始属性, 以免对其他代码造成影响。
				 */
				this.conn.setAutoCommit(autoCommit);

			} catch (SQLException e) {
				logger.error("事物代码块异常 ", e);
				try {
					this.conn.rollback();
				} catch (SQLException e1) {
					logger.error("事物回滚时异常 ", e);
					throw new SQLException("事物回滚时异常 : "
							+ e1.getLocalizedMessage());
				}
				throw new SQLException("事物代码块异常 : " + e.getLocalizedMessage());
			} finally {
				try {
					this.conn.setAutoCommit(autoCommit);
				} catch (SQLException e) {
					logger.error("恢复Connection自动提交属性异常 ", e);
					throw new SQLException("恢复Connection自动提交属性异常 : \n"
							+ e.getLocalizedMessage());

				}
				if (this.isCloseConnection) {
					try {
						this.conn.close();
						this.conn = null;
					} catch (SQLException e) {
						logger.error("数据库连接关闭时 ", e);
						throw new SQLException("数据库连接关闭时 : "
								+ e.getLocalizedMessage());
					}
				}
			}
		}

		return result;
	}

	protected abstract T doInTransaction(Connection conn) throws SQLException;
}
package org.liufei.util.jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

/**
 * jdbc操作工具。
 * 
 * @author 刘飞
 * 
 */
public class JdbcUtil {
	private static final Logger logger = Logger.getLogger(JdbcUtil.class);

	/**
	 * 用于执行对数据库的查询。
	 * 
	 * @param conn
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public static ResultSet executeQuery(Connection conn, String sql)
			throws SQLException {
		if (conn != null && sql != null) {
			if(isSupportsResultSet_TYPE_SCROLL_INSENSITIVE_CONCUR_READ_ONLY(conn) && isSupportsResultSetType_TYPE_SCROLL_INSENSITIVE(conn)) {
				return conn.prepareStatement(sql,
						ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_READ_ONLY).executeQuery();
			}
			else {
				return conn.prepareStatement(sql).executeQuery();
			}
		} else {
			logger.error("数据库连接对象和SQL参数不能为空 : java.sql.Connection[conn : null], java.lang.String[sql : null].") ;
			throw new SQLException(
					"数据库连接对象和SQL参数不能为空 : java.sql.Connection[conn : null], java.lang.String[sql : null].");
		}
	}
	
	/**
	 * 
	 * @param <T>
	 * @param conn
	 * @param callback
	 * @param isCloseConnection
	 *            完成事物以后是否关闭连接。
	 * @return
	 * @throws SQLException 
	 */
	public static <T> T executeSQL(final Connection conn, final ConnectionCallback<T> callback, boolean isCloseConnection) throws SQLException {
		return (T) new JdbcTransaction<T>(conn, isCloseConnection){

			@Override
			public T doInTransaction(Connection conn) throws SQLException {
				return callback.doInSql(conn);
			}}
		.doInTransaction();
	}

	/**
	 * 用于执行对数据库的修改更新和删除。
	 * 
	 * @param conn
	 * @param sql
	 * @param isCloseConnection
	 *            完成事物以后是否关闭连接。
	 * @return
	 * @throws SQLException
	 */
	public static int executeSQLDML(Connection conn, final String sql,
			boolean isCloseConnection) throws SQLException {
		if (conn != null && sql != null) {
			
			JdbcTransaction<Integer> jdbcTransaction = new JdbcTransaction<Integer>(
					conn, isCloseConnection) {

				@Override
				public Integer doInTransaction(Connection conn)
						throws SQLException {
					int result = 0 ;
					if(isSupportsResultSet_TYPE_SCROLL_INSENSITIVE_CONCUR_UPDATABLE(conn) && isSupportsResultSetType_TYPE_SCROLL_INSENSITIVE(conn)) {
						/**
						 * 执行数据库更新
						 */
						result = conn.prepareStatement(sql,
								ResultSet.TYPE_SCROLL_INSENSITIVE,
								ResultSet.CONCUR_UPDATABLE).executeUpdate();
					}
					else {
						/**
						 * 执行数据库更新
						 */
						result = conn.prepareStatement(sql).executeUpdate();
					}
					return result;
				}
			};

			return jdbcTransaction.doInTransaction();
		} else {
			logger.error("数据库连接对象和SQL参数不能为空 : java.sql.Connection[conn : null], java.lang.String[sql : null].") ;
			throw new SQLException(
					"数据库连接对象和SQL参数不能为空 : java.sql.Connection[conn : null], java.lang.String[sql : null].");
		}
	}

	/**
	 * PreparedStatement 执行批处理。
	 * 
	 * @param conn
	 * @param batchSql
	 *            批量更新SQL
	 * @param parameters
	 *            二维参数列表, 用于注入SQL中
	 * @param times
	 *            每多少条执行一次更新
	 * @param isCloseConnection
	 *            完成事物以后是否关闭连接。
	 * @return 返回此批处理共影响的数据条数。
	 * @throws SQLException
	 */
	public static int executePreparedStatementSQLBatch(Connection conn,
			final String batchSql, final Object[][] parameters,
			final int times, final boolean isCloseConnection) throws SQLException {
		if (conn != null && batchSql != null) {

			if (batchSql.length() > 0) {
				if(isSupportsBatchUpdates(conn)) {
					JdbcTransaction<Integer> jdbcTransaction = new JdbcTransaction<Integer>(
							conn, isCloseConnection) {

						@Override
						public Integer doInTransaction(Connection conn)
								throws SQLException {
							int result = 0;
							/**
							 * 执行批量操作
							 */
							PreparedStatement pstmt = conn
									.prepareStatement(batchSql);

							int i = 1;
							for (Object[] params : parameters) {
								int j = 1;
								for (Object param : params) {
									pstmt.setObject(j, param);
									j++;
								}

								if (i % times == 0) {
									int[] rs = pstmt.executeBatch();
									for (int k : rs) {
										result += k;
									}
								}

								i++;
							}

							release(null, pstmt, null, false) ;
							
							return result;
						}
					};

					return jdbcTransaction.doInTransaction();
				}
				else {
					throw new RuntimeException("this database does not supports batch updates.") ;
				}
			} else {
				return 0;
			}
		} else {
			logger.error("数据库连接对象和SQL参数不能为空 : java.sql.Connection[conn : null], java.lang.String[sql : null].") ;
			throw new SQLException(
					"数据库连接对象和SQL参数不能为空 : java.sql.Connection[conn : null], java.lang.String[sql : null].");
		}
	}

	/**
	 * Statement执行批处理。
	 * 
	 * @param conn
	 * @param batchSql
	 * @param isCloseConnection
	 *            完成事物以后是否关闭连接。
	 * @return 返回此批处理共影响的数据条数。
	 * @throws SQLException
	 */
	public static int executeStatementSQLBatch(Connection conn,
			final String[] batchSql, final int times, final boolean isCloseConnection)
			throws SQLException {
		if (conn != null && batchSql != null) {

			if (batchSql.length > 0) {
				if(isSupportsBatchUpdates(conn)) {
					JdbcTransaction<Integer> jdbcTransaction = new JdbcTransaction<Integer>(
							conn, isCloseConnection) {

						@Override
						public Integer doInTransaction(Connection conn)
								throws SQLException {

							int rs = 0;
							/**
							 * 执行批量操作
							 */
							Statement stmt = conn.createStatement();
							int k = 0 ;
							for (String sql : batchSql) {
								if (sql.trim().length() > 0) {
									stmt.addBatch(sql);
								}
								
								if(k % times == 0) {
									int[] result = stmt.executeBatch();
									for (int i : result) {
										rs += i;
									}
								}
								
							}

							release(null, stmt, null, false) ;
							
							return rs;
						}
					};

					return jdbcTransaction.doInTransaction();
				}
				else {
					throw new RuntimeException("this database does not supports batch updates.") ;
				}
			} else {
				return 0;
			}
		} else {
			logger.error("数据库连接对象和SQL参数不能为空 : java.sql.Connection[conn : null], java.lang.String[sql : null].") ;
			throw new SQLException(
					"数据库连接对象和SQL参数不能为空 : java.sql.Connection[conn : null], java.lang.String[sql : null].");
		}
	}
	
	/**
	 * 设置只读模式
	 * @param connection
	 * @param readOnly
	 * @throws SQLException
	 */
	public static void setReadOnly(Connection connection, boolean readOnly) throws SQLException {
		connection.setReadOnly(readOnly) ;
	}

	/**
	 * 释放数据库资源。
	 * 
	 * @param rs
	 * @param stmt
	 * @param conn
	 * @param isCloseConnection
	 *            是否关闭数据库连接。
	 * @throws SQLException
	 */
	public static void release(ResultSet rs, Statement stmt, Connection conn,
			boolean isCloseConnection) throws SQLException {
		if (rs != null) {
			rs.close();
			rs = null;
		}
		if (stmt != null) {
			stmt.close();
			stmt = null;
		}
		if (conn != null) {
			if (!conn.isClosed() && !conn.getAutoCommit()) {
				conn.rollback();
			}
			if (isCloseConnection) {
				conn.close();
				conn = null;
			}
		}
	}
	
	/**
	 * 
	 * @param connection
	 * @param autoCommit
	 * @throws SQLException
	 */
	public static void setAutoCommit(Connection connection, boolean autoCommit) throws SQLException {
		connection.setAutoCommit(autoCommit);
	}
	
	/**
	 * 是否支持ResultSet.TYPE_SCROLL_INSENSITIVE
	 * @param connection
	 * @return
	 * @throws SQLException
	 */
	public static boolean isSupportsResultSetType_TYPE_SCROLL_INSENSITIVE(Connection connection) throws SQLException {
		return connection.getMetaData().supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE) ;
	}
	
	/**
	 * 是否支持ResultSet.CONCUR_READ_ONLY
	 * @param connection
	 * @return
	 * @throws SQLException
	 */
	public static boolean isSupportsResultSet_TYPE_SCROLL_INSENSITIVE_CONCUR_READ_ONLY(Connection connection) throws SQLException {
		return connection.getMetaData().supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY) ;
	}
	
	/**
	 * 是否支持ResultSet.CONCUR_UPDATABLE
	 * @param connection
	 * @return
	 * @throws SQLException
	 */
	public static boolean isSupportsResultSet_TYPE_SCROLL_INSENSITIVE_CONCUR_UPDATABLE(Connection connection) throws SQLException {
		return connection.getMetaData().supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE) ;
	}
	
	/**
	 * 是否支持 ANSI92 SQL
	 * @param connection
	 * @return
	 * @throws SQLException
	 */
	public static boolean isSupportsANSI92FullSQL(Connection connection) throws SQLException {
		return connection.getMetaData().supportsANSI92FullSQL() ;
	}
	
	/**
	 * 是否支持SelectForUpdate
	 * @param connection
	 * @return
	 * @throws SQLException
	 */
	public static boolean isSupportsSelectForUpdate(Connection connection) throws SQLException {
		return connection.getMetaData().supportsSelectForUpdate() ;
	}
	
	/**
	 * 是否支持批量更新。
	 * @param connection
	 * @return
	 * @throws SQLException
	 */
	public static boolean isSupportsBatchUpdates(Connection connection) throws SQLException {
		return connection.getMetaData().supportsBatchUpdates() ;
	}
	
	/**
	 * 是否支持事务保存点
	 * @param connection
	 * @return
	 * @throws SQLException
	 */
	public static boolean isSupportsSavepoints(Connection connection) throws SQLException {
		return connection.getMetaData().supportsSavepoints() ;
	}
	
	/**
	 * 设置事务保存点
	 * @param connection
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public static Savepoint setSavePoint(Connection connection, String name) throws SQLException {
		if (isSupportsSavepoints(connection)) {
			return connection.setSavepoint(name);
		} else {
			throw new RuntimeException("this database does not supports savepoints.") ;
		}
	}
	
	/**
	 * 回滚到事务点
	 * @param connection
	 * @param savepoint
	 * @throws SQLException
	 */
	public static void rollback(Connection connection, Savepoint savepoint) throws SQLException {
		if (savepoint == null) {
			connection.rollback();
		} else {
			if(isSupportsSavepoints(connection))
				connection.rollback(savepoint);
			else
				connection.rollback();
		} 
	}

	/**
	 * 回滚到事务点
	 * @param connection
	 * @throws SQLException
	 */
	public static void rollback(Connection connection) throws SQLException {
		connection.rollback();
	}
	
	/**
	 * 提交事务
	 * @param connection
	 * @param savepoint
	 */
	public static void commit(Connection connection, Savepoint savepoint) throws SQLException {
		if(savepoint == null) {
			if (!connection.getAutoCommit()) {
				connection.commit();
			}
		}
		else {
			if(isSupportsSavepoints(connection)) {
				if (!connection.getAutoCommit()) {
					connection.releaseSavepoint(savepoint);
				}
			}
			else {
				if (!connection.getAutoCommit()) {
					connection.commit();
				}
			}
		}
	}
	
	/**
	 * 提交事务
	 * @param connection
	 */
	public static void commit(Connection connection) throws SQLException {
		if (!connection.getAutoCommit()) {
  	        connection.commit();
  	     }
	}

	/**
	 * 获取数据库连接数据源。
	 * 
	 * @param jndiName
	 *            jndi名称
	 * @return
	 * @throws NamingException
	 */
	public static DataSource getDataSource(String jndiName)
			throws NamingException {
		Context ctx = new InitialContext();
		DataSource ds = (DataSource) ctx.lookup(jndiName);
		return ds;
	}
	
	public static Connection initConnection(String driver, String url, String user, String password) {
		try {
			Class.forName(driver).newInstance() ;
			return DriverManager.getConnection(url, user, password) ;
		} catch (InstantiationException e) {
			logger.error("Driver Class >>> " + driver + ", " + url + ", " + user + ", " + password + " <<< Instantiation Error.", e) ;
			throw new RuntimeException("Driver Class >>> " + driver + ", " + url + ", " + user + ", " + password + " <<< Instantiation Error.", e) ;
		} catch (IllegalAccessException e) {
			logger.error("Driver Class >>> " + driver + ", " + url + ", " + user + ", " + password + " <<< Illegal Access.", e) ;
			throw new RuntimeException("Driver Class >>> " + driver + ", " + url + ", " + user + ", " + password + " <<< Illegal Access.", e) ;
		} catch (ClassNotFoundException e) {
			logger.error("Driver Class >>> " + driver + ", " + url + ", " + user + ", " + password + " <<< Not Found.", e) ;
			throw new RuntimeException("Driver Class >>> " + driver + ", " + url + ", " + user + ", " + password + " <<< Not Found.", e) ;
		} catch (SQLException e) {
			logger.error("DriverManager Get Connection >>> " + driver + ", " + url + ", " + user + ", " + password + " <<< Error.", e) ;
			throw new RuntimeException("DriverManager Get Connection >>> " + driver + ", " + url + ", " + user + ", " + password + " <<< Error.", e) ;
		}
	}

	/**
	 * 测试数据库连接是否有效。
	 * 
	 * @param driver
	 * @param user
	 * @param password
	 * @param url
	 * @return
	 */
	public static boolean testConnection(String driver, String user,
			String password, String url) {
		try {
			Class.forName(driver).newInstance();
			Connection connection = DriverManager.getConnection(url, user,
					password);

			DatabaseMetaData metaData = connection.getMetaData();

			if (user.equals(metaData.getUserName())) {
				return true;
			}

		} catch (InstantiationException e) {
			logger.error("测试数据库连接异常", e) ;
		} catch (IllegalAccessException e) {
			logger.error("测试数据库连接异常", e) ;
		} catch (ClassNotFoundException e) {
			logger.error("测试数据库连接异常", e) ;
		} catch (SQLException e) {
			logger.error("测试数据库连接异常", e) ;
		}
		return false;
	}
}
package org.liufei.util.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 
 * @author 刘飞
 *
 */
public class JdbcUser {

	/**
	 * 查询
	 * 
	 * @param <T>
	 * @param conn
	 * @param sql
	 * @param callback
	 * @param isCloseConnection
	 * @return
	 * @throws SQLException
	 */
	public static <T> T executeConnectionQueryCallback(final Connection conn,
			final String sql, ResultSetCallback<T> callback, boolean isCloseConnection)
			throws SQLException {
		T t = new SqlCallback<T, ResultSet>() {
			public T doInSQLCallback(String sql, Callback<T, ResultSet> callback)
					throws SQLException {
				return callback.doInSql(JdbcUtil.executeQuery(conn, sql));
			}
		}.doInSQLCallback(sql, callback);
		
		if(isCloseConnection) {
			JdbcUtil.release(null, null, conn, true) ;
		}
		
		return t;
	}
	
	/**
	 * 执行更新
	 * @param <T>
	 * @param conn
	 * @param isCloseConnection
	 * @param callback
	 * @return
	 * @throws SQLException
	 */
	public static <T> T executeSQLDML(final Connection conn, boolean isCloseConnection, final ConnectionCallback<T> callback) throws SQLException {
		return new JdbcTransaction<T>(conn, isCloseConnection){

			@Override
			public T doInTransaction(Connection conn) throws SQLException {
				return callback.doInSql(conn);
			}}
		.doInTransaction();
	}
}
分享到:
评论
3 楼 xchsh12345 2013-12-05  
基本上无用,liufei这个jar包应该是自己封装的吧!
2 楼 jack_sun 2013-01-15  
能不能提供下载链接?
1 楼 gdjdxx 2012-05-14  
没有给出liufei相关的包,代码没用

相关推荐

    apache-mina-2.0.4.rar_apache mina_mina

    Apache Mina是一个高性能、异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端。这个"apache-mina-2.0.4.rar"压缩包包含的是Apache Mina 2.0.4版本的源代码,是深入理解和定制Mina的...

    mina的高级使用,mina文件图片传送,mina发送文件,mina报文处理,mina发送xml和json

    同时,Mina提供了进度反馈机制,可以实时监控文件传输进度,确保传输的可靠性。 3. **Mina发送文件** 发送文件的过程与图片类似,但可能需要考虑更多因素,如文件大小、文件类型等。Mina允许开发者自定义编码解码...

    apache mina实例免费下载

    在实际应用中,Apache MINA常用于构建服务器端应用,如聊天服务器、游戏服务器、文件传输服务等。通过MINA提供的API,开发者可以轻松创建出处理大量并发连接的服务。MINA实例通常包含以下部分: 1. **Acceptor**:...

    Apache Mina2服务器和客户端简单实例

    Apache Mina2是一个高度可扩展且高性能的网络通信框架,主要设计用于简化开发网络应用,如TCP/IP和UDP协议的服务端和客户端。它提供了一种事件驱动、非阻塞I/O模型,使得开发者能够更专注于业务逻辑,而不是底层的...

    apache Mina的完整学习资料

    Apache Mina的核心理念是提供一个与传输协议无关的抽象层,开发者可以通过它来开发基于TCP/IP、UDP或其他网络协议的应用。Mina简化了网络编程的复杂性,使得开发者可以专注于业务逻辑,而无需关注底层通信细节。 在...

    apache mina 学习笔记三(子项目FtpServer)

    Apache MINA FtpServer提供了一个可扩展且高效的FTP服务器实现,允许开发人员自定义认证机制、数据存储策略以及许多其他特性。 **FtpServer基本架构** Apache MINA FtpServer的核心组件包括服务器引擎、服务器端口...

    Apache Mina 入门Demo

    它是Java语言开发的,提供了一套简单而强大的API,使得开发者可以方便地创建网络服务器和客户端应用程序。Mina的目标是简化网络编程,使其与处理I/O操作一样简单。 在"Apache Mina 入门Demo"中,我们可以期待学习...

    Apache MINA基于JAVA的网络服务器框架 教程.zip

    2. **基于Flash Socket和AMF3开发web游戏引擎.txt**:AMF3(Action Message Format)是Adobe用于Flash与服务器之间高效传输数据的格式。这个文本可能探讨了如何结合Apache MINA和AMF3技术构建Web游戏服务器,支持...

    Apache Mina Server 2.0 抢鲜体验

    Apache Mina Server 2.0 是一款高性能、可扩展的网络通信框架,广泛应用于开发网络应用服务器,如TCP/IP和UDP服务。这个框架允许开发者用Java编写高效的网络应用程序,简化了网络编程的复杂性。Mina 2.0 版本在前一...

    Apache mina2学习笔记DEMO

    MINA 提供了一套高级的网络通信抽象层,简化了开发过程,特别是对于处理TCP/IP和UDP/IP协议栈的应用。MINA的核心概念是事件驱动和非阻塞I/O,这使得它在处理大量并发连接时表现出色。 在这个"Apache MINA2学习笔记...

    Apache MINA框架相关资料

    1. **中文参考手册**(Apache_Mina_Server_2.0中文参考手册V1.0.pdf):这是一份详尽的MINA框架使用手册,用中文详细介绍了MINA的核心概念、设计模式以及如何创建服务器和客户端。它包含了各种MINA组件的用法,如...

    Apache MINA 2.0 用户指南( 缺第一章节)

    Apache MINA 2.0 是一款高性能且易于使用的网络应用程序框架,它简化了开发人员在网络编程方面的负担,允许开发者专注于应用程序的核心功能,而不是底层的网络细节。MINA 2.0 支持多种网络协议,包括 TCP 和 UDP,...

    apache mina-spring 服务端程序

    Apache Mina是一个高度可扩展的网络通信框架,主要用于构建高性能、高并发的网络应用程序,如服务器端应用。它提供了一种简单而强大的编程模型,使得开发者可以专注于业务逻辑,而不是底层的网络通信细节。Spring...

    一个Apache MINA使用案例源代码ApacheMina

    Apache MINA是一个高性能、异步事件驱动的网络应用程序框架,主要设计用于简化开发服务器端的高性能网络应用。这个框架提供了一种抽象层,允许开发者使用相同的API处理多种不同的传输协议,如TCP/IP、UDP/IP以及SSL/...

    Apache-Mina-2.rar_apache mina_java mina_mina_mina-core-1.1.3

    描述中提到的"Mina中文参考手册"对于中国开发者来说是一份宝贵的资源,它详细介绍了如何使用Apache Mina进行开发。手册可能涵盖了Mina的基本概念、架构、API使用、以及如何构建实际的应用程序等内容。对于初学者来说...

    Apache mina源代码框架解析

    总的来说,Apache Mina通过其过滤器架构和处理器模型,为开发网络应用程序提供了强大的抽象。通过对`MinaTimeServer`的源代码分析,我们可以看到如何配置服务器,设置过滤器,处理网络事件,并与客户端进行有效通信...

    使用 Apache MINA 开发高性能网络应用程序

    总结来说,Apache MINA 是一个强大的工具,它降低了开发高性能网络应用的门槛,使得开发者能够专注于业务逻辑,而不是底层的网络通信细节。通过使用 MINA,开发者可以构建出可伸缩、可靠且易于维护的网络服务。

    apache-mina例子

    总的来说,Apache Mina提供的这套框架极大地简化了网络应用的开发工作,通过其高度可定制的过滤器机制和非阻塞I/O模型,使得开发者能够更专注于业务逻辑,而不是底层的网络通信细节。在`MinaTimeServer`和`Mina...

    Mina文件及字符串传输

    标题中的"Mina文件及字符串传输"指的是使用Apache Mina框架进行数据交互的过程,包括发送和接收字符串请求以及传输文件。这一过程通常涉及到网络编程中的TCP/IP协议,因为Mina支持基于TCP的传输层通信。 首先,让...

Global site tag (gtag.js) - Google Analytics