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

基于Java Socket的文件UpLoad代码

2008-01-03 09:26 417 查看
写了份文件对传的简单代码,可以把本地文件夹里的文件传递到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);
 }
}
瞎写的,凑合着用吧。

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1921438
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: