/*
* MyClient.java
*
*創建於2009年8月16日下午1:37
*
*要更改此模板,請選擇工具|模板管理器
*並在編輯器中打開模板。
*/
導入Java . io . *;
導入Java . net . *;
公共類MyClient
{
套接字客戶端;
布爾標誌=真;
FileInputStream fis//這個輸入流負責讀取本機要傳輸的文件。
DataOutputStream dos//這個輸出流負責將數據傳輸到另壹臺計算機(服務器端)。
DataInputStream dis//這個輸入流負責從另壹臺計算機讀取響應信息。
公共靜態void main(String[] args)
{
新的MyClient()。client start();
}
public void ClientStart()
{
嘗試
{
client = new Socket(" 192.168 . 2 . 101 ",30000);//服務器端IP,(這個只在局域網內)我的是這個,妳的看現實。
//client=new Socket("localhost ",30000);
system . out . println(" connected ");
dos = new data output stream(client . get output stream());
dis = new data inputstream(client . getinputstream());
傳輸(新文件(" e:\ \ 1 \ \ ");
string s = "/]00 ";//表示傳輸完成的標記
byte b[]= s . getbytes();
dos.write(b,0,s . length());
dos . flush();
}
捕捉(IOException e)
{
system . out . println(" Error ");
}
}
public void transmit(file f)throwsioexception//這是傳輸的核心,會遞歸。
{
字節b[];
字符串ts;
int ti
for(文件f1:f.listFiles())
{//首先通過if語句判斷f1是文件還是文件夾。
如果(f1.isDirectory()) //fi是文件夾,則向服務器發送消息。
{
ts="/]0f"+(f1.getPath()。replace("e:\\1\\ "," ");///]0f "用於表示此消息的內容是文件夾名稱。
b = ts . getbytes();
dos . write(b);
dos . flush();
dis . read();
傳輸(f 1);//因為f1是文件夾(也就是目錄),裏面很可能有文件或者文件夾,所以進行遞歸。
}
其他
{
fis = new file inputstream(f 1);
ts="/]0c"+(f1.getPath()。replace("e:\\1\\ "," ");//同上,表示這是壹個文件的名稱。
b = ts . getbytes();
dos . write(b);
dos . flush();
dis . read();
dos . write int(fis . available());//傳輸壹個指示要傳輸的文件大小的整數值。
dos . flush();
dis . read();
b =新字節[10000];
while(fis . available()>;0)//開始文件傳輸
{
ti = fis . read(b);
dos.write(b,0,ti);
dos . flush();
}
dos . flush();
fis . close();
dis . read();
}
}
}
}
/*
* MyServer.java
*
*創建於2009年8月16日上午11:44。
*
*要更改此模板,請選擇工具|模板管理器
*並在編輯器中打開模板。
*/
/**
*
* @作者管理員
*/
導入Java . io . *;
導入Java . net . *;
公共類MyServer
{
ServerSocket server = null
Socket client = null
布爾標誌=真;
DataInputStream dis
DataOutputStream dos
文件輸出流fos
公共靜態void main(String[] args)
{
新建MyServer()。server start();
}
public void ServerStart()
{
嘗試
{
server = new server socket(30000);
System.out.println("端口號:"+server . getlocalport());
client = server . accept();
System.out.println("連接完成");
dis = new data inputstream(client . getinputstream());
dos = new data output stream(client . get output stream());
String answer = " g
byte ans[]= answer . getbytes();
byte b[]=新字節[1000];
int ti
新文件(“e:\\2”)。mkdirs();
while(標誌)
{
ti = dis . read(b);
dos . write(ans);
String select=new String(b,0,ti);
if(select.contains("/]0f "))
{
File f = new File(" e:\ \ 2 \ \ "+(select . replace("/]0f "," ")));
system . out . println(" creat directory ");
f . mkdirs();
}
else if(select.contains("/]0c "))
{
fos = new file output stream(" e:\ \ 2 \ \ "+(select . replace("/]0c "," ")));
字符串cs;
布爾型cflag = true
int tip = dis . readint();
dos . write(ans);
while(提示& gt0)
{
ti=dis.read(b,0,(tip & gt1000?1000:tip));
tip = tip-ti;
cs =新字符串(b,0,4);
fos.write(b,0,ti);
}
fos . flush();
fos . close();
dos . write(ans);
}
else if(select.contains("/]00 "))
{
flag = false
}
}
dis . close();
client . close();
server . close();
}
捕捉(IOException e)
{
system . out . println(" Error ");
}
}
}