Java使用Socket通信傳輸文件的方法示例
本文實例講述了Java使用Socket通信傳輸文件的方法。分享給大家供大家參考,具體如下:
前面幾篇文章介紹了使用Java的Socket編程和NIO包在Socket中的應用,這篇文章說說怎樣利用Socket編程來實現(xiàn)簡單的文件傳輸。
這里由于前面一片文章介紹了NIO在Socket中的應用,所以這里在讀寫文件的時候也繼續(xù)使用NIO包,所以代碼看起來會比直接使用流的方式稍微復雜一點點。
下面的示例演示了客戶端向服務器端發(fā)送一個文件,服務器作為響應給客戶端回發(fā)一個文件。這里準備兩個文件E:/test/server_send.log和E:/test/client.send.log文件,在測試完畢后在客戶端和服務器相同目錄下會多出兩個文件E:/test/server_receive.log和E:/test/client.receive.log文件。
下面首先來看看Server類,主要關注其中的sendFile和receiveFile方法。
package com.googlecode.garbagecan.test.socket.nio; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.nio.channels.FileChannel; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.logging.Level; import java.util.logging.Logger; public class MyServer4 { private final static Logger logger = Logger.getLogger(MyServer4.class.getName()); public static void main(String[] args) { Selector selector = null; ServerSocketChannel serverSocketChannel = null; try { // Selector for incoming time requests selector = Selector.open(); // Create a new server socket and set to non blocking mode serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); // Bind the server socket to the local host and port serverSocketChannel.socket().setReuseAddress(true); serverSocketChannel.socket().bind(new InetSocketAddress(10000)); // Register accepts on the server socket with the selector. This // step tells the selector that the socket wants to be put on the // ready list when accept operations occur, so allowing multiplexed // non-blocking I/O to take place. serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); // Here's where everything happens. The select method will // return when any operations registered above have occurred, the // thread has been interrupted, etc. while (selector.select() > 0) { // Someone is ready for I/O, get the ready keys Iterator<SelectionKey> it = selector.selectedKeys().iterator(); // Walk through the ready keys collection and process date requests. while (it.hasNext()) { SelectionKey readyKey = it.next(); it.remove(); // The key indexes into the selector so you // can retrieve the socket that's ready for I/O doit((ServerSocketChannel) readyKey.channel()); } } } catch (ClosedChannelException ex) { logger.log(Level.SEVERE, null, ex); } catch (IOException ex) { logger.log(Level.SEVERE, null, ex); } finally { try { selector.close(); } catch(Exception ex) {} try { serverSocketChannel.close(); } catch(Exception ex) {} } } private static void doit(final ServerSocketChannel serverSocketChannel) throws IOException { SocketChannel socketChannel = null; try { socketChannel = serverSocketChannel.accept(); receiveFile(socketChannel, new File("E:/test/server_receive.log")); sendFile(socketChannel, new File("E:/test/server_send.log")); } finally { try { socketChannel.close(); } catch(Exception ex) {} } } private static void receiveFile(SocketChannel socketChannel, File file) throws IOException { FileOutputStream fos = null; FileChannel channel = null; try { fos = new FileOutputStream(file); channel = fos.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); int size = 0; while ((size = socketChannel.read(buffer)) != -1) { buffer.flip(); if (size > 0) { buffer.limit(size); channel.write(buffer); buffer.clear(); } } } finally { try { channel.close(); } catch(Exception ex) {} try { fos.close(); } catch(Exception ex) {} } } private static void sendFile(SocketChannel socketChannel, File file) throws IOException { FileInputStream fis = null; FileChannel channel = null; try { fis = new FileInputStream(file); channel = fis.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); int size = 0; while ((size = channel.read(buffer)) != -1) { buffer.rewind(); buffer.limit(size); socketChannel.write(buffer); buffer.clear(); } socketChannel.socket().shutdownOutput(); } finally { try { channel.close(); } catch(Exception ex) {} try { fis.close(); } catch(Exception ex) {} } } }
下面是Client程序代碼,也主要關注sendFile和receiveFile方法
package com.googlecode.garbagecan.test.socket.nio; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.SocketChannel; import java.util.logging.Level; import java.util.logging.Logger; public class MyClient4 { private final static Logger logger = Logger.getLogger(MyClient4.class.getName()); public static void main(String[] args) throws Exception { new Thread(new MyRunnable()).start(); } private static final class MyRunnable implements Runnable { public void run() { SocketChannel socketChannel = null; try { socketChannel = SocketChannel.open(); SocketAddress socketAddress = new InetSocketAddress("localhost", 10000); socketChannel.connect(socketAddress); sendFile(socketChannel, new File("E:/test/client_send.log")); receiveFile(socketChannel, new File("E:/test/client_receive.log")); } catch (Exception ex) { logger.log(Level.SEVERE, null, ex); } finally { try { socketChannel.close(); } catch(Exception ex) {} } } private void sendFile(SocketChannel socketChannel, File file) throws IOException { FileInputStream fis = null; FileChannel channel = null; try { fis = new FileInputStream(file); channel = fis.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); int size = 0; while ((size = channel.read(buffer)) != -1) { buffer.rewind(); buffer.limit(size); socketChannel.write(buffer); buffer.clear(); } socketChannel.socket().shutdownOutput(); } finally { try { channel.close(); } catch(Exception ex) {} try { fis.close(); } catch(Exception ex) {} } } private void receiveFile(SocketChannel socketChannel, File file) throws IOException { FileOutputStream fos = null; FileChannel channel = null; try { fos = new FileOutputStream(file); channel = fos.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); int size = 0; while ((size = socketChannel.read(buffer)) != -1) { buffer.flip(); if (size > 0) { buffer.limit(size); channel.write(buffer); buffer.clear(); } } } finally { try { channel.close(); } catch(Exception ex) {} try { fos.close(); } catch(Exception ex) {} } } } }
首先運行MyServer4類啟動監(jiān)聽,然后運行MyClient4類來向服務器發(fā)送文件以及接受服務器響應文件。運行完后,分別檢查服務器和客戶端接收到的文件。
更多關于java相關內容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
- Java Web項目中使用Socket通信多線程、長連接的方法
- Java實現(xiàn)的基于socket通信的實例代碼
- php與java通過socket通信的實現(xiàn)代碼
- JAVA實現(xiàn)基于Tcp協(xié)議的簡單Socket通信實例
- Python與Java間Socket通信實例代碼
- java中處理socket通信過程中粘包的情況
- Java Socket實現(xiàn)多線程通信功能示例
- Java--Socket通信(客戶端服務端雙向)
- JAVA中實現(xiàn)原生的 socket 通信機制原理
- Java Socket實現(xiàn)單線程通信的方法示例
- Java開發(fā)實現(xiàn)的Socket雙向通信功能示例
相關文章
SpringBoot3.x版本與Mybatis-Plus不兼容問題
當使用3.x版本的SpringBoot結合Mybatis-Plus時版本不兼容就會報錯,本文就來介紹一下這個問題的解決方法,感興趣的可以了解一下2024-03-03SpringBoot自定義starter啟動器的實現(xiàn)思路
這篇文章主要介紹了SpringBoot如何自定義starter啟動器,通過starter的自定義過程,能夠加深大家對SpringBoot自動配置原理的理解,需要的朋友可以參考下2022-10-10springboot整合mybatis實現(xiàn)簡單的一對多級聯(lián)查詢功能
這篇文章主要介紹了springboot整合mybatis實現(xiàn)簡單的一對多級聯(lián)查詢功能,分步驟通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08