Java Socket文件传输详解编程语言

 
 
import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.ServerSocket; 
import java.net.Socket; 
 
public class FileServer { 
 
 private int port = 1128; 
 
 private ServerSocket servSocket; 
 
 private Socket socket; 
 
  
 private DataInputStream input; 
 
  
 private DataOutputStream outPut; 
 
 private String savePath = "E://up//"; 
 
 public void startServer() { 
 
  // 已经传输的文件大小 
  int ycSize = 0; 
 
  // 文件总大小 
  long sumSize = 0; 
 
  // 缓存大小 
  int hcSize = 8192; 
 
  // 缓存 
  byte[] hc = new byte[hcSize]; 
 
  try { 
 
   servSocket = new ServerSocket(port); 
 
   socket = servSocket.accept(); 
 
   input = new DataInputStream(new BufferedInputStream( 
     socket.getInputStream())); 
 
  } catch (IOException e) { 
 
   e.printStackTrace(); 
 
  } 
 
  try { 
   // 将文件名字读取进来 
   savePath += input.readUTF(); 
   // 文件的长度读取进来(实际只是为了显示进度) 
   sumSize = input.readLong(); 
 
  } catch (IOException e) { 
 
   e.printStackTrace(); 
 
  } 
 
  try { 
 
   outPut = new DataOutputStream(new BufferedOutputStream( 
     new FileOutputStream(savePath))); 
 
  } catch (FileNotFoundException e) { 
 
   e.printStackTrace(); 
 
  } 
 
  while (true) { 
 
   int read = 0; 
 
   if (input != null) { 
 
    try { 
 
     read = input.read(hc); 
 
    } catch (IOException e) { 
 
     e.printStackTrace(); 
    } 
   } 
 
   ycSize += read; 
 
   if (read == -1) { 
 
    break; 
 
   } 
 
   // 下面进度条本为图形界面的prograssBar做的,这里如果是打文件,可能会重复打印出一些相同的百分比 
   System.out.println("文件接收了" + (ycSize * 100 / sumSize) + "%/n"); 
 
   try { 
 
    outPut.write(hc, 0, read); 
 
   } catch (IOException e) { 
 
    e.printStackTrace(); 
 
   } 
 
  } 
 
  if (outPut != null) { 
   try { 
    outPut.close(); 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
   outPut = null; 
  } 
 
  if (input != null) { 
   try { 
    input.close(); 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
   input = null; 
  } 
 
  if (socket != null) { 
   try { 
    socket.close(); 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
   socket = null; 
  } 
 
  if (servSocket != null) { 
   try { 
    servSocket.close(); 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
   servSocket = null; 
  } 
 
  System.out.println("接收完成,文件存为" + savePath + "/n"); 
 } 
 
 public static void main(String[] args) { 
  FileServer fileServer = new FileServer(); 
  fileServer.startServer(); 
 } 
} 
 
  
 
  
 
import java.io.BufferedInputStream; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.net.Socket; 
import java.net.UnknownHostException; 
 
public class FileClient { 
 public static void main(String[] args) { 
 
  String filePath = "E://TDDOWNLOAD//DEEPBBS.COM_GhostXPsp3_2011.07CJ.iso"; 
 
  File file = new File(filePath); 
 
  DataInputStream input = null; 
  DataOutputStream output = null; 
  Socket socket = null; 
 
  try { 
 
   String ip = "192.168.1.104"; 
   int port = 1128; 
 
   socket = new Socket(ip, port); 
 
   input = new DataInputStream(new BufferedInputStream( 
     new FileInputStream(filePath))); 
 
   output = new DataOutputStream(socket.getOutputStream()); 
 
  } catch (UnknownHostException e) { 
 
   e.printStackTrace(); 
 
  } catch (IOException e) { 
 
   e.printStackTrace(); 
 
  } 
 
  try { 
   output.writeUTF(file.getName()); 
 
   output.flush(); 
 
   output.writeLong((long) file.length()); 
 
   output.flush(); 
 
  } catch (IOException e) { 
 
   e.printStackTrace(); 
 
  } 
 
  int bufferSize = 8192; 
  byte[] buf = new byte[bufferSize]; 
 
  while (true) { 
 
   int read = 0; 
 
   if (input != null) { 
 
    try { 
 
     read = input.read(buf); 
 
    } catch (IOException e) { 
 
     e.printStackTrace(); 
 
    } 
 
   } 
 
   if (read == -1) { 
 
    break; 
   } 
 
   try { 
 
    output.write(buf, 0, read); 
 
    output.flush(); 
 
   } catch (IOException e) { 
 
    e.printStackTrace(); 
 
   } 
 
  } 
 
  try { 
   input.close(); 
 
   output.close(); 
 
   socket.close(); 
 
  } catch (IOException e) { 
 
   e.printStackTrace(); 
 
  } 
 } 
} 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/10546.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论