这是一个简单的包含发送端和接收端的例子。发送端向接收端发送文件名和文件内容 ,接收端将收到的文件保存在磁盘上。接收端可以同时接收多个发送端传来的文件,但没 有处理文件同名的情况。
这个例子中设计了一个简单的协议。发送的内容是这样的:
文件名长度(4字节)—文件名—文件内容长度(4字节)—文件内容 。
接收端也按照这个结构进行解析。建议先看 Client 类,再看 Server 类。
01.import java.io.*;02.import java.net.ServerSocket;03.import java.net.Socket;04.05./**06. * 简单的文件发送与接收示例07. */08.public class FileTrasmission {09.10. //程序入口11. public static void main(String[] args) throws Exception {12. int port = 7788;13. new Server(port, "c://save//").start();14. new Client().sendFile("127.0.0.1", port, "c://迷失在康熙末年.txt");15. }16.}17.18./**19. * 接收端。可同时接收多个发送端发来的文件。但如果发来的文件是同名的话那就乱了。20. */21.class Server {22.23. private int listenPort;24.25. private String savePath;26.27. /**28. * 构造方法29. *30. * @param listenPort 侦听端口31. * @param savePath 接收的文件要保存的路径32. *33. * @throws IOException 如果创建保存路径失败34. */35. Server(int listenPort, String savePath) throws IOException {36. this.listenPort = listenPort;37. this.savePath = savePath;38.39. File file = new File(savePath);40. if (!file.exists() && !file.mkdirs()) {41. throw new IOException("无法创建文件夹 " + savePath);42. }43. }44.45. // 开始侦听46. public void start() {47. new ListenThread().start();48. }49.50. // 网上抄来的,将字节转成 int。b 长度不得小于 4,且只会取前 4 位。51. public static int b2i(byte[] b) {52. int value = 0;53. for (int i = 0; i < 4; i++) {54. int shift = (4 - 1 - i) * 8;55. value += (b[i] & 0x000000FF) << shift;56. }57. return value;58. }59.60.61. /**62. * 侦听线程63. */64. private class ListenThread extends Thread {65.66. @Override67. public void run() {68. try {69. ServerSocket server = new ServerSocket(listenPort);70.71. // 开始循环72. while (true) {73. Socket socket = server.accept();74. new HandleThread(socket).start();75. }76. } catch (IOException e) {77. e.printStackTrace();78. }79. }80. }81.82. /**83. * 读取流并保存文件的线程84. */85. private class HandleThread extends Thread {86.87. private Socket socket;88.89. private HandleThread(Socket socket) {90. this.socket = socket;91. }92.93. @Override94. public void run() {95. try {96. InputStream is = socket.getInputStream();97. readAndSave(is);98. } catch (IOException e) {99. e.printStackTrace();100. } finally {101. try {102. socket.close();103. } catch (IOException e) {104. // nothing to do105. }106. }107. }108.109. // 从流中读取内容并保存110. private void readAndSave(InputStream is) throws IOException {111. String filename = getFileName(is);112. int file_len = readInteger(is);113. System.out.println("接收文件:" + filename + ",长度:" + file_len);114.115. readAndSave0(is, savePath + filename, file_len);116.117. System.out.println("文件保存成功(" + file_len + "字节)。");118. }119.120. private void readAndSave0(InputStream is, String path, int file_len) throws IOException {121. FileOutputStream s = getFileOS(path);122. readAndWrite(is, os, file_len);123. os.close();124. }125.126. // 边读边写,直到读取 size 个字节127. private void readAndWrite(InputStream is, FileOutputStream os, int size) throws IOException {128. byte[] buffer = new byte[4096];129. int count = 0;130. while (count < size) {131. int n = is.read(buffer);132. // 这里没有考虑 n = -1 的情况133. os.write(buffer, 0, n);134. count += n;135. }136. }137.138. // 读取文件名139. private String getFileName(InputStream is) throws IOException {140. int name_len = readInteger(is);141. byte[] result = new byte[name_len];142. is.read(result);143. return new String(result);144. }145.146. // 读取一个数字147. private int readInteger(InputStream is) throws IOException {148. byte[] bytes = new byte[4];149. is.read(bytes);150. return b2i(bytes);151. }152.153. // 创建文件并返回输出流154. private FileOutputStream getFileOS(String path) throws IOException {155. File file = new File(path);156. if (!file.exists()) {157. file.createNewFile();158. }159.160. return new FileOutputStream(file);161. }162. }163.}164.165./**166. * 发送端167. */168.class Client {169.170. // 网上抄来的,将 int 转成字节171. public static byte[] i2b(int i) {172. return new byte[]{173. (byte) ((i >> 24) & 0xFF),174. (byte) ((i >> 16) & 0xFF),175. (byte) ((i >> 8) & 0xFF),176. (byte) (i & 0xFF)177. };178. }179.180. /**181. * 发送文件。文件大小不能大于 {@link Integer#MAX_VALUE}182. *183. * @param hostname 接收端主机名或 IP 地址184. * @param port 接收端端口号185. * @param filepath 文件路径186. *187. * @throws IOException 如果读取文件或发送失败188. */189. public void sendFile(String hostname, int port, String filepath) throws IOException {190. File file = new File(filepath);191. FileInputStream is = new FileInputStream(filepath);192.193. Socket socket = new Socket(hostname, port);194. OutputStream s = socket.getOutputStream();195.196. try {197. int length = (int) file.length();198. System.out.println("发送文件:" + file.getName() + ",长度:" + length);199.200. // 发送文件名和文件内容201. writeFileName(file, os);202. writeFileContent(is, os, length);203. } finally {204. os.close();205. is.close();206. }207. }208.209. // 输出文件内容210. private void writeFileContent(InputStream is, OutputStream os, int length) throws IOException {211. // 输出文件长度212. os.write(i2b(length));213.214. // 输出文件内容215. byte[] buffer = new byte[4096];216. int size;217. while ((size = is.read(buffer)) != -1) {218. os.write(buffer, 0, size);219. }220. }221.222. // 输出文件名223. private void writeFileName(File file, OutputStream os) throws IOException {224. byte[] fn_bytes = file.getName().getBytes();225.226. os.write(i2b(fn_bytes.length)); // 输出文件名长度227. os.write(fn_bytes); // 输出文件名228. }229.}
有理想在的地方,地狱就是天堂