基于Java Socket的文件UpLoad代码(完美版)

Server端程序:

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.File;

public class FileUpLoadProjServer extends Thread {

 public FileUpLoadProjServer(Socket s, String c) throws IOException {
 }

 public void run() {
 }

 public static void main(String[] args) {
  try {
   ServerSocket server = new ServerSocket(8110);
   Socket connection = null;
   while (true) {
    try {
     connection = server.accept();
     DataInputStream in = new DataInputStream(connection
       .getInputStream());
  
     String myFile = in.readUTF();
     String tempFile = "D"+myFile.substring(myFile.indexOf(":"));
     String strDiretory = "";
     int tempIndex = 0;
     while((tempIndex = tempFile.indexOf("\\")) != -1){
      strDiretory += tempFile.substring(0,tempIndex+1);
      tempFile = tempFile.substring(tempIndex+1);
     }
     System.out.println(strDiretory+" ,tempFile is :"+tempFile);
     File d = new File(strDiretory);
     d.mkdirs();
     
     File f = new File(strDiretory+tempFile);
     f.createNewFile();
     
     FileOutputStream fos = new FileOutputStream(f);
     int ch = 0;

     while ((ch = in.read()) != -1) {
      System.out.print((char) ch);
      fos.write(ch);
     }
     fos.close();
     connection.close();
    } catch (IOException ioe) {
     System.err.println(ioe);
    } finally {
     try {
      if (connection != null)
       connection.close();
     } catch (IOException e) {
     }
    }
   }
  } catch (IOException ee) {
   System.err.println(ee);
  }
 }
}

 

Client端程序:

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

public class FileUpLoadProjClient extends Thread {

 private Socket socket;
 private DataOutputStream out;
 final int port = 8110;
 String path = "C:\\src";
 String[] filePathArray = new String[1000];
 int fileNum;
 InetAddress m_addr;
 public FileUpLoadProjClient(InetAddress addr) {
  madetree(new File(path));
  m_addr = addr;
  start();
 }

 void madetree(File dothis) {
  File[] farray = dothis.listFiles();
  for (int i = 0; i < farray.length; i++) {
   if (farray[i].isFile()){
    filePathArray[fileNum++] = farray[i].getAbsolutePath();
   }else if(farray[i].isDirectory())
    madetree(farray[i]);
  }
 }

 public void run() {
  try {
   for(int k = 0;k < filePathArray.length&&filePathArray[k]!=null;k++){
     System.out.println("The file''s absolutePath is :" + filePathArray[k]);
     try {
      socket = new Socket(m_addr, port);
      out = new DataOutputStream(socket.getOutputStream());
     } catch (IOException e) {
      try {
       socket.close();
      } catch (IOException e2) {
      }
     }
     FileInputStream fis = new FileInputStream(filePathArray[k]);
     int ch = 0;
 
     out.writeUTF(filePathArray[k]);
     
     while ((ch = fis.read()) != -1) {
      out.write(ch);
     }
     fis.close();
     socket.close();
   }
   out.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public static void main(String[] args) throws IOException,
   InterruptedException {
  InetAddress addr = InetAddress.getByName("127.0.0.1");
  new FileUpLoadProjClient(addr);
 }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java Socket文件传输代码示例: // 服务器端代码 import java.net.*; import java.io.*; public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(4444); Socket clientSocket = serverSocket.accept(); InputStream inputStream = clientSocket.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream("received_file.txt"); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, bytesRead); } fileOutputStream.close(); inputStream.close(); clientSocket.close(); serverSocket.close(); } } // 客户端代码 import java.net.*; import java.io.*; public class Client { public static void main(String[] args) throws IOException { Socket socket = new Socket("localhost", 4444); File file = new File("file_to_send.txt"); FileInputStream fileInputStream = new FileInputStream(file); OutputStream outputStream = socket.getOutputStream(); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fileInputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } fileInputStream.close(); outputStream.close(); socket.close(); } } 这个示例中,服务器监听端口4444,并等待客户端连接。客户端连接到服务器并打开一个文件,将其内容通过Socket发送给服务器。服务器接收数据并将其写入一个名为“received_file.txt”的文件。请注意,这个示例没有错误处理和验证代码,仅作为演示用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值