欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
使用 java Socket 從客戶(hù)端向服務(wù)器端傳文件 - cwyang的專(zhuān)欄 - CSD...
使用 java Socket 從客戶(hù)端向服務(wù)器端傳文件收藏

 服務(wù)器端類(lèi) SocketServer

 

public class SocketServer {
 protected int listenPort = 3000;

 public static void main(String[] args) {
  SocketServer server = new SocketServer();
  server.acceptConnections();
 }
 /**
  * 建立鏈接
  *
  */
 public void acceptConnections() {
  try {
   ServerSocket server = new ServerSocket(listenPort);
   Socket socket = null;
   while (true) {
    socket = server.accept();
    //handleConnection(socket);
    new ServerThread(socket).start();
   }
  } catch (BindException e) {
   System.out.println("Unable to bind to port " + listenPort);
  } catch (IOException e) {
   System.out.println("Unable to instantiate a ServerSocket on port: "
     + listenPort);
  }

 }
}

服務(wù)器端接收線(xiàn)程類(lèi)

public class ServerThread extends Thread {
 private Socket socket;

 public ServerThread(Socket socket)
 {
  this.socket = socket;
 }


 public void run(){

  try {

   DataInputStream inputStream = null;
   try {
    inputStream = new DataInputStream(new BufferedInputStream(
      socket.getInputStream()));
   } catch (Exception e) {
    System.out.print("接收消息緩存錯誤\n");
    return;
   }
   try {
    // 本地保存路徑,文件名會(huì )自動(dòng)從服務(wù)器端繼承而來(lái)最好是web工程里的一個(gè)路徑。
    String savePath = "E:\\";
    int bufferSize = 8192;
    byte[] buf = new byte[bufferSize];
    long len = 0;
    savePath += inputStream.readUTF();
    DataOutputStream fileOut = new DataOutputStream(
      new BufferedOutputStream(new BufferedOutputStream(
        new FileOutputStream(savePath))));
    len = inputStream.readLong();
    System.out.println("文件的長(cháng)度為:" + len + "\n");
    System.out.println("開(kāi)始接收文件!" + "\n");
    while (true) {
     int read = 0;
     if (inputStream != null) {
      read = inputStream.read(buf);
     }
     if (read == -1) {
      break;
     }
     //System.out.println(buf.toString());
     fileOut.write(buf, 0, read);
    }
    System.out.println("接收完成,文件存為" + savePath + "\n");
    fileOut.flush();
    fileOut.close();
    inputStream.close();
   } catch (Exception e) {
    System.out.println("接收消息錯誤" + "\n");
    return;
   }
  } catch (Exception e) {
   System.out.println("Error handling a client: " + e);
  }
  
 }

}

 

客戶(hù)端發(fā)送文件類(lèi)

public class SocketClient extends Thread{
 protected String hostIp ="127.0.0.1";
 protected int hostPort = 3000;
 InputStream fis;
 DataOutputStream ps;
 File fi;
 DataInputStream dis;
 String path;
 Socket client;
 
 public SocketClient() {
 }
 public SocketClient(String path) {
  this.path = path;
 }
 public SocketClient(String path,String aHostIp, int aHostPort) {
  this.path = path;
  this.hostIp = aHostIp;
  this.hostPort = aHostPort;
 }

 public static void main(String[] args) {
  SocketClient client = new SocketClient();
  String path = "f:\\35.xml";  //測試文件
  client.sendMessage();
 }
 public void run(){
  sendMessage();
 }
 
 /**
  * 將連接到遠程服務(wù)器
  */
 public void setUpConnection() {
  try {
   client = new Socket(hostIp, hostPort);
   fis = new FileInputStream(path);
   ps = new DataOutputStream(client.getOutputStream());
   fi = new File(path);
  } catch (UnknownHostException e) {
   System.out
     .println("Error setting up socket connection: unknown host at "
       + hostIp + ":" + hostPort);
  } catch (IOException e) {
   System.out.println("Error setting up socket connection: " + e);
  }

 }

 /**
  * 將向遠程服務(wù)器請求 path 的內容
  *
  * @param path
  * @return
  */
 public void sendMessage() {
  setUpConnection();
  if(client == null)
   return ;
  try {
   // 將文件名及長(cháng)度傳給客戶(hù)端。
   ps.writeUTF(fi.getName());
   ps.flush();

   ps.writeLong((long) fi.length());
   ps.flush();
   int bufferSize = 8192;
   byte[] buf = new byte[bufferSize];
   while (true) {
    int read = 0;
    if (fis != null) {
     read = fis.read(buf);
    }
    if (read == -1) {
     break;
    }
    ps.write(buf, 0, read);
   }
   ps.flush();

   System.out.println("文件傳輸完成");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {

   // 注意關(guān)閉socket鏈接哦,不然客戶(hù)端會(huì )等待server的數據過(guò)來(lái),
   // 直到socket超時(shí),導致數據不完整。
   try {
    ps.close();
    fis.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

  }
 }

}

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Java基于socket文件傳輸示例
【android】Socket簡(jiǎn)單用法
Java網(wǎng)絡(luò )篇
JavaSocket短連接實(shí)現分別接收字符串和16進(jìn)制數據
JavaEE-網(wǎng)絡(luò )編程-TCP流套接字編程
通用各類(lèi)文檔讀寫(xiě)的設計與實(shí)現
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久