*设为首页
*加入收藏
热门关键字: JAVA
>>当前位置:Java大本营>Socket编程>文章内容
基于Java Socket的文件UpLoad代码
作者: 发布时间:2007-12-07 22:38:03
写了份文件对传的简单代码,可以把本地文件夹里的文件传递到Server端。

Server端代码:

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

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();
     InputStreamReader in = new InputStreamReader(connection
       .getInputStream());

     long time = System.currentTimeMillis();
     String t = "C:\\temp_" + time;
     File myFile = null;
     if ((new File(t).mkdir())) {
      myFile = new File(t + "\\temp.txt");
     } else {
      System.out.println("Create folder failed!");
     }

     FileOutputStream fos = new FileOutputStream(myFile);

     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.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class FileUpLoadProjClient extends Thread {

 private Socket socket;
 private PrintWriter out;
 final int port = 8110;
 String path = "C:\\src\\test";

 public FileUpLoadProjClient(InetAddress addr) {
  try {
   socket = new Socket(addr, port);
  } catch (IOException e) {

  }

  try {
   out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
     socket.getOutputStream())), true);
   start();
  } catch (IOException e) {
   try {
    socket.close();
   } catch (IOException e2) {
   }
  }
 }

 public void run() {
  try {
   File root = new File(path);
   String[] colum = root.list();
   System.out.println("The file's num is :" + colum.length);

   for (int i = 0; i < colum.length; i++) {
    System.out.println("The colum's content is :" + colum[i]);
    String filePath = path + "\\" + colum[i];
    System.out.println("The file's absolutePath is :" + filePath);
    FileInputStream fis = new FileInputStream(filePath);
    int ch = 0;

    while ((ch = fis.read()) != -1) {
     System.out.print((char) ch);
     out.write(ch);
    }
    fis.close();
   }
   out.close();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    socket.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);
 }

}


·充分理解Socket(02-25)
·Socket编程指南及程序示例 (01-27)
·java网络编程学习(12-06)
·secket 的几种构造方法(12-06)
·Java网络服务器编程 (12-05)
·Java网络编程 (12-05)
·Java开发环境简介(12-05)
·Java网络编程学习笔记(12-05)
·基于Socket的Java网络编程(12-05)
·Java网络编程-用SSL构建安全的Socket (12-05)
·Java聊天程序的实现(12-04)
·简单的双向消息发送的聊天程序(java) (12-04)
复制地址发给您的好友: [推荐文章] [返回顶部] [关闭窗口]
版权所有 Java大本营 1999-2007 转载请注明出处