基于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);
 }

}


你可能感兴趣的:(java,thread,socket,String,upload,File)