您的位置:首页 > 编程语言 > Java开发

基于Java Socket的文件UpLoad代码(完美版)-用递归解决java的目录树遍历

2008-06-08 05:46 721 查看
        上次用J2SE写了一个文件夹传递工具,把所有文件都以字节流的形式写入到一个*.txt文件里。结果回到家后,光分目录筛选文件就浪费了我整整一个晚上。痛定思痛,决定还是从程序上来解决问题。
       那么所有的磁盘文件目录都是树的结构,而遍历树最好的方法非"深度优先遍历"莫属,其最有效的方法便是使用"递归"进行"深度优先遍历"。
      于是经过3个多小时的痛苦挣扎,最终写出了如下的程序,可以完整的读取文件夹里的所有内容,并不改变目录结构的放到Server所指定的路径下面。勘称"完美",嘿嘿。
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);
 }
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐