Socket:java与C之间的文件传送<2>(JAVA语言之间的通信)

其实绝大多数编程语言之间的通信原理是一样的这边文章来看一下java是如何进行文件传输的

建议先看 Client 类,再看 Server 类。

  1. import java.io.*;   
  2. import java.net.ServerSocket;   
  3. import java.net.Socket;   
  4.   
  5. /**   
  6. * 简单的文件发送与接收示例   
  7. */   
  8. public class FileTrasmission {   
  9.   
  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. @Override   
  67. 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. @Override   
  94. 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 do   
  105. }   
  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 os = 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. }   
import java.io.*; 
import java.net.ServerSocket; 
import java.net.Socket; 

/** 
* 简单的文件发送与接收示例 
*/ 
public class FileTrasmission { 

//程序入口 
public static void main(String[] args) throws Exception { 
int port = 7788; 
new Server(port, "c://save//").start(); 
new Client().sendFile("127.0.0.1", port, "c://迷失在康熙末年.txt"); 
} 
} 

/** 
* 接收端。可同时接收多个发送端发来的文件。但如果发来的文件是同名的话那就乱了。 
*/ 
class Server { 

private int listenPort; 

private String savePath; 

/** 
* 构造方法 
* 
* @param listenPort 侦听端口 
* @param savePath 接收的文件要保存的路径 
* 
* @throws IOException 如果创建保存路径失败 
*/ 
Server(int listenPort, String savePath) throws IOException { 
this.listenPort = listenPort; 
this.savePath = savePath; 

File file = new File(savePath); 
if (!file.exists() && !file.mkdirs()) { 
throw new IOException("无法创建文件夹 " + savePath); 
} 
} 

// 开始侦听 
public void start() { 
new ListenThread().start(); 
} 

// 网上抄来的,将字节转成 int。b 长度不得小于 4,且只会取前 4 位。 
public static int b2i(byte[] b) { 
int value = 0; 
for (int i = 0; i < 4; i++) { 
int shift = (4 - 1 - i) * 8; 
value += (b[i] & 0x000000FF) << shift; 
} 
return value; 
} 


/** 
* 侦听线程 
*/ 
private class ListenThread extends Thread { 

@Override 
public void run() { 
try { 
ServerSocket server = new ServerSocket(listenPort); 

// 开始循环 
while (true) { 
Socket socket = server.accept(); 
new HandleThread(socket).start(); 
} 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
} 

/** 
* 读取流并保存文件的线程 
*/ 
private class HandleThread extends Thread { 

private Socket socket; 

private HandleThread(Socket socket) { 
this.socket = socket; 
} 

@Override 
public void run() { 
try { 
InputStream is = socket.getInputStream(); 
readAndSave(is); 
} catch (IOException e) { 
e.printStackTrace(); 
} finally { 
try { 
socket.close(); 
} catch (IOException e) { 
// nothing to do 
} 
} 
} 

// 从流中读取内容并保存 
private void readAndSave(InputStream is) throws IOException { 
String filename = getFileName(is); 
int file_len = readInteger(is); 
System.out.println("接收文件:" + filename + ",长度:" + file_len); 

readAndSave0(is, savePath + filename, file_len); 

System.out.println("文件保存成功(" + file_len + "字节)。"); 
} 

private void readAndSave0(InputStream is, String path, int file_len) throws IOException { 
FileOutputStream os = getFileOS(path); 
readAndWrite(is, os, file_len); 
os.close(); 
} 

// 边读边写,直到读取 size 个字节 
private void readAndWrite(InputStream is, FileOutputStream os, int size) throws IOException { 
byte[] buffer = new byte[4096]; 
int count = 0; 
while (count < size) { 
int n = is.read(buffer); 
// 这里没有考虑 n = -1 的情况 
os.write(buffer, 0, n); 
count += n; 
} 
} 

// 读取文件名 
private String getFileName(InputStream is) throws IOException { 
int name_len = readInteger(is); 
byte[] result = new byte[name_len]; 
is.read(result); 
return new String(result); 
} 

// 读取一个数字 
private int readInteger(InputStream is) throws IOException { 
byte[] bytes = new byte[4]; 
is.read(bytes); 
return b2i(bytes); 
} 

// 创建文件并返回输出流 
private FileOutputStream getFileOS(String path) throws IOException { 
File file = new File(path); 
if (!file.exists()) { 
file.createNewFile(); 
} 

return new FileOutputStream(file); 
} 
} 
} 
  1. /**   
  2. * 发送端   
  3. */   
  4. class Client {   
  5.   
  6. // 网上抄来的,将 int 转成字节   
  7. public static byte[] i2b(int i) {   
  8. return new byte[]{   
  9. (byte) ((i >> 24) & 0xFF),   
  10. (byte) ((i >> 16) & 0xFF),   
  11. (byte) ((i >> 8) & 0xFF),   
  12. (byte) (i & 0xFF)   
  13. };   
  14. }   
  15.   
  16. /**   
  17. * 发送文件。文件大小不能大于 {@link Integer#MAX_VALUE}   
  18. *   
  19. * @param hostname 接收端主机名或 IP 地址   
  20. * @param port 接收端端口号   
  21. * @param filepath 文件路径   
  22. *   
  23. * @throws IOException 如果读取文件或发送失败   
  24. */   
  25. public void sendFile(String hostname, int port, String filepath) throws IOException {   
  26. File file = new File(filepath);   
  27. FileInputStream is = new FileInputStream(filepath);   
  28.   
  29. Socket socket = new Socket(hostname, port);   
  30. OutputStream os = socket.getOutputStream();   
  31.   
  32. try {   
  33. int length = (int) file.length();   
  34. System.out.println("发送文件:" + file.getName() + ",长度:" + length);   
  35.   
  36. // 发送文件名和文件内容   
  37. writeFileName(file, os);   
  38. writeFileContent(is, os, length);   
  39. } finally {   
  40. os.close();   
  41. is.close();   
  42. }   
  43. }   
  44.   
  45. // 输出文件内容   
  46. private void writeFileContent(InputStream is, OutputStream os, int length) throws IOException {   
  47. // 输出文件长度   
  48. os.write(i2b(length));   
  49.   
  50. // 输出文件内容   
  51. byte[] buffer = new byte[4096];   
  52. int size;   
  53. while ((size = is.read(buffer)) != -1) {   
  54. os.write(buffer, 0, size);   
  55. }   
  56. }   
  57.   
  58. // 输出文件名   
  59. private void writeFileName(File file, OutputStream os) throws IOException {   
  60. byte[] fn_bytes = file.getName().getBytes();   
  61.   
  62. os.write(i2b(fn_bytes.length)); // 输出文件名长度   
  63. os.write(fn_bytes); // 输出文件名   
  64. }   
  65. }  
/** 
* 发送端 
*/ 
class Client { 

// 网上抄来的,将 int 转成字节 
public static byte[] i2b(int i) { 
return new byte[]{ 
(byte) ((i >> 24) & 0xFF), 
(byte) ((i >> 16) & 0xFF), 
(byte) ((i >> 8) & 0xFF), 
(byte) (i & 0xFF) 
}; 
} 

/** 
* 发送文件。文件大小不能大于 {@link Integer#MAX_VALUE} 
* 
* @param hostname 接收端主机名或 IP 地址 
* @param port 接收端端口号 
* @param filepath 文件路径 
* 
* @throws IOException 如果读取文件或发送失败 
*/ 
public void sendFile(String hostname, int port, String filepath) throws IOException { 
File file = new File(filepath); 
FileInputStream is = new FileInputStream(filepath); 

Socket socket = new Socket(hostname, port); 
OutputStream os = socket.getOutputStream(); 

try { 
int length = (int) file.length(); 
System.out.println("发送文件:" + file.getName() + ",长度:" + length); 

// 发送文件名和文件内容 
writeFileName(file, os); 
writeFileContent(is, os, length); 
} finally { 
os.close(); 
is.close(); 
} 
} 

// 输出文件内容 
private void writeFileContent(InputStream is, OutputStream os, int length) throws IOException { 
// 输出文件长度 
os.write(i2b(length)); 

// 输出文件内容 
byte[] buffer = new byte[4096]; 
int size; 
while ((size = is.read(buffer)) != -1) { 
os.write(buffer, 0, size); 
} 
} 

// 输出文件名 
private void writeFileName(File file, OutputStream os) throws IOException { 
byte[] fn_bytes = file.getName().getBytes(); 

os.write(i2b(fn_bytes.length)); // 输出文件名长度 
os.write(fn_bytes); // 输出文件名 
} 
}

看起来很复杂的样子,对于一个刚刚接触的人来说却是不怎么容易理解,那么这是一个怎么样的逻辑的,我们分析一下就会变得比较简单了;

首先来讲这个例子是说一个完整的文件传输包括文件大小,文件名称,文件内容。显然对于我们来说要一个个得来。对于文件大小文件名称,我们完全可以用一个固定的常量来代替,这样就大大简化了代码的复杂度,最终也就变成了如何传送文件内容!

1,读取文件内容,

2,发送内容,

说起来好简单的样子但是要怎么做呢?

  1. FileInputStream is = new FileInputStream(this.FilePath);  
  2.             byte Buffer[] = new byte[1024 * 8];  
  3.         while ((size = is.read(buffer)) != -1) {  
  4.             //System.out.println(size + "  ");  
  5.             outputstream.write(buffer, 0, size);  
  6.         }  
FileInputStream is = new FileInputStream(this.FilePath);
	        byte Buffer[] = new byte[1024 * 8];
		while ((size = is.read(buffer)) != -1) {
			//System.out.println(size + "  ");
			outputstream.write(buffer, 0, size);
		}

客户端怎么接收呢?

  1. InputStream inputStream = socket.getInputStream();  
InputStream inputStream = socket.getInputStream();
  1. int Size;  
int Size;
  1. byte Buffer[] = new byte[1024 * 8];  
  2. while ((Size = inputStream.read(Buffer)) != -1) {  
  3.          System.out.print(Size + " ");  
byte Buffer[] = new byte[1024 * 8];
while ((Size = inputStream.read(Buffer)) != -1) {
	     System.out.print(Size + " ");
  1. system.out.print(new String11(b<SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">uffer</SPAN><SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">));//此处文档显示有问题所以加了个11,源码需要去掉!</SPAN>  
             system.out.print(new String11(b<span style="FONT-FAMILY: Arial, Helvetica, sans-serif">uffer</span><span style="FONT-FAMILY: Arial, Helvetica, sans-serif">));//此处文档显示有问题所以加了个11,源码需要去掉!</span>
  1. }  
		}
这样看起来是不是就没那么复杂了呢 ,其实很多东西动手自己做,你会发现是如此的简单!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值