基於TCP的檔案傳輸

2021-07-31 17:08:37 字數 1340 閱讀 3143

伺服器端處理客戶請求:

public class serverthread extends thread 

@override

public void

run()

file file=new file(directory.getabsolutepath()+file.separatorchar+

filename);

fos=new fileoutputstream(file);

//開始接受檔案

byte bytes=new byte[1024];

int length=0

;while((length=dis.read(bytes,

0,bytes.length))!=-1)

system.out.println("檔案接受成功!檔案長度為"+filelength);

//4、獲取輸出流,響應客戶端的請求

os = socket.getoutputstream();

oos=new printwriter(os);

oos.write("伺服器已經接受到檔案!");

oos.flush();

} catch (ioexception e) finally

if(os!=null)

os.close();

socket.close();

} catch (ioexception e) }}

}

伺服器端;

public class server 

} catch (ioexception e)

}}

客戶端:

public class client 

system.out.println();

system.out.println("***檔案傳輸成功***");

//3.獲取輸入流,並讀取伺服器端的響應資訊

inputstream is = socket.getinputstream();

bufferedreader ois=new bufferedreader(

new inputstreamreader(is)

);string s=null;

while((s=ois.readline())!=null)

//4/關閉資源

fis.close();

dos.close();

is.close();

os.close();

socket.close();

} }catch (ioexception e)

}}

tcp檔案傳輸

include include include include include include include include define buffer size 100 1024 1024 define file name max size 512 intmain int argc,char a...

TCP檔案傳輸(一)

最近在做網路抓包和檔案傳輸,今天遇到乙個奇怪的現象,自己查了半天也沒搞清楚,導致花了乙個下午時間就只實現了檔名和檔案長度的傳送,首先給出檔案傳輸的客戶端和伺服器端 一 客戶端 1 winsock com元件初始化和釋放 include stdafx.h ifndef winsock startup ...

TCP實現檔案傳輸

一直想著給之前的clouddisk專案加上乙個c s架構的檔案傳輸模組,因為之前是nginx fastcgi架構的b s架構,自己又不會前段 沒有辦法繼續增加新的功能塊。最近終於抽出時間開始寫專案了,已經選用tcp完成linux下的cs架構檔案上傳功能模組,這裡展示tcp檔案傳輸模組。socket類...