基於Java Socket的檔案UpLoad代碼

來源:互聯網
上載者:User

寫了份檔案對傳的簡單代碼,可以把本地檔案夾裡的檔案傳遞到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     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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.