Mina傳送檔案的客戶端實現
實現步驟:
1.建立一個無阻塞服務端socket 用nio 2.建立接收過濾器 也就是你要傳送物件的型別
3.設定 物件傳輸工廠 4.設定傳輸最大值 5.設定服務端訊息處理器, 將執行緒放入執行緒池 當連線很多時候可以通過執行緒池處理
NioSocketConnector connector = new NioSocketConnector(); DefaultIoFilterChainBuilder chain = connector.getFilterChain(); ObjectSerializationCodecFactory factory = new ObjectSerializationCodecFactory(); factory.setDecoderMaxObjectSize(Integer.MAX_VALUE); factory.setEncoderMaxObjectSize(Integer.MAX_VALUE); chain.addLast("logging", new LoggingFilter());//用於列印日誌可以不寫 connector.setHandler(new MinaFileClient(filePath)); ConnectFuture connectFuture = connector.connect(new InetSocketAddress(local,port)); connectFuture.awaitUninterruptibly();//寫上這句為了得到下面的session 意思是等待連線建立完成 讓建立連線由非同步變同步
package cn.org.handler; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetSocketAddress; import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; import org.apache.mina.core.future.ConnectFuture; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory; import org.apache.mina.filter.logging.LoggingFilter; import org.apache.mina.handler.stream.StreamIoHandler; import org.apache.mina.transport.socket.nio.NioSocketConnector; import cn.org.util.IoStreamThreadWork; /** * @author changyaobin * 檔案傳輸客戶端 */ public class MinaFileClient extends StreamIoHandler{ IoSession session; private String filePath; public void setSession(IoSession session) { this.session = session; } public IoSession getSession() { return session; } @Override protected void processStreamIo(IoSession session, InputStream in, OutputStream out) { //客戶端傳送檔案 File sendFile = new File(this.filePath); FileInputStream fis = null; try { fis = new FileInputStream(sendFile); } catch (FileNotFoundException e) { e.printStackTrace(); } //放入執行緒讓其執行 //客戶端一般都用一個執行緒實現即可 不用執行緒池 new IoStreamThreadWork(fis,out).start(); return; } public void createClienStream(){ int port = 8888; String local = "127.0.0.1"; NioSocketConnector connector = new NioSocketConnector(); DefaultIoFilterChainBuilder chain = connector.getFilterChain(); ObjectSerializationCodecFactory factory = new ObjectSerializationCodecFactory(); factory.setDecoderMaxObjectSize(Integer.MAX_VALUE); factory.setEncoderMaxObjectSize(Integer.MAX_VALUE); chain.addLast("logging", new LoggingFilter());//用於列印日誌可以不寫 connector.setHandler(new MinaFileClient(filePath)); ConnectFuture connectFuture = connector.connect(new InetSocketAddress(local,port)); connectFuture.awaitUninterruptibly();//寫上這句為了得到下面的session 意思是等待連線建立完成 讓建立連線由非同步變同步 } public MinaFileClient(String filePath) { super(); this.filePath = filePath; } public MinaFileClient() { super(); } }
接下來,實現IoStreamThreadWork多執行緒
package cn.org.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 用於mina 伺服器上傳下載 * 流處理執行緒公共類 * @author changyaobin * */ public class IoStreamThreadWork extends Thread{ public static final int BUFFER_SIZE = 1024*1000; private static final int fileHeadCheckSiteStart = 30;//此處代表位元組 private static final int fileHeadCheckSiteLength = 2;//此處代表位元組 private static String path = "resources"; private BufferedInputStream bis; private BufferedOutputStream bos; public BufferedInputStream getBis() { return bis; } public void setBis(BufferedInputStream bis) { this.bis = bis; } public BufferedOutputStream getBos() { return bos; } public void setBos(BufferedOutputStream bos) { this.bos = bos; } public IoStreamThreadWork(InputStream in, OutputStream os){ bis = new BufferedInputStream(in); bos = new BufferedOutputStream(os); } public synchronized void run() { byte[] bufferByte = new byte[BUFFER_SIZE]; int tempData = 0; StringBuffer hexs = new StringBuffer(); DecodeFile decodeFile = null; try { while((tempData = bis.read(bufferByte)) != -1 ){ bos.write(bufferByte, 0, tempData); //轉儲為16進位制字串陣列 //注:位元組轉換為16進位制字串,1個位元組變兩個16進位制字串 String[] hexTemps = Bytes2AllType.bytes2Hexs(bufferByte, 0, tempData); for (String str : hexTemps) { hexs.append(str); } } try { bos.flush(); } catch (IOException e) { e.printStackTrace(); } } catch (InstantiationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }finally{ try { bos.close(); bis.close(); } catch (IOException e) { e.printStackTrace(); } } } }