TCPプロトコルにより、クライアントからサーバ側へのファイルアップロードを完了
9269 ワード
様々なタイプのファイルをアップロードするように要求すると、包装フローを使用して包装することはできません.読み書きしながら、データバッファを作成する必要があります.コピーファイルはSystemを採用する.CurrentTimeMillis()メソッドで名前を付けると、重複しません.
:
import java.io.*;
import java.net.Socket;
public class TCPClient {
public static void main(String[] args) throws IOException {
//7、 , TCP , 。
Socket sk = new Socket("192.168.10.103",6666);
OutputStream out = sk.getOutputStream();
// ;
FileInputStream in = new FileInputStream("Msg.txt");
int len=0;
byte[] bytes = new byte[1024];
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
sk.close();
}
}
:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(6666);
System.out.println(" !");
// , ;
Socket sk = ss.accept();
InputStream in = sk.getInputStream();
FileOutputStream out = new FileOutputStream(System.currentTimeMillis() + " Msg.txt");
int len=0;
byte[] bytes = new byte[1024];
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
ss.close();
}
}