大隐于市

用艺术的眼光看待编程!

孙东风ID:dongfengsun
126666次访问,排名614(-1)好友5人,关注者28
03年从西安交通大学毕业后一直从事移动平台的开发工作。崇尚一切自由的东西,热爱交流。目前专注于手机二维码技术研究并负责技术管理工作,希望能和同行认识、交流、共享。
dongfengsun的文章
原创 73 篇
翻译 2 篇
转载 32 篇
评论 139 篇
孙东风的公告
原创文章转载请注明出处
QQ :372590353
MSN:sundongfeng1227@hotmail.com
  我的EMAIL



最近评论
ddd999ddd:楼主能简单举个实际应用的例子来说明认识您所谓的有用的默认构造函数的重要性吗?
wistaria2002:我的毕业论文就是手机QR码,楼主研究心得值得学习,现在的手机QR码好像quickmark做的不错,现在nokia n95手机已经自带了Qr码解码程序
lixun:呵呵。支持java。共享我一份研究一下?lixun_4321@sina.com
liuguoying2008:你好,我正学习这方面的东西,需要一些例子,对我非常重要,希望您能在百忙之中给我发一份,谢谢!我的邮箱是liuguoying20062007@163.com
aspnetwuxueyou:有时间欢迎看看我对AO的理解
文章分类
收藏
    相册
    存档
    订阅我的博客
    XML聚合  FeedSky

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

    新一篇: 无线开发,你凭什么吸引VC? | 旧一篇: 基于Java Socket的文件UpLoad代码

            上次用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);
     }

    }

    发表于 @ 2007年12月12日 00:46:00|评论(loading...)|编辑

    新一篇: 无线开发,你凭什么吸引VC? | 旧一篇: 基于Java Socket的文件UpLoad代码

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © 孙东风