Javaのネットワークプログラミングは、ファイルをアップロード-TCP-

サーバーとクライアントのみの輸送の間にソケットストリーム入力と出力が、余分なバイトを書き込むことがときにソケットストリームの書き込み、保存されたとき、サーバはソケットストリームのストリームを読み込み、ファイルのバイトストリームの追加コンテンツがある読み出す必要がありますファイルへ

クライアント:ファイルのアップロード

public class tcp2 {

public static void main(String[]args) throws IOException
{
    System.out.println("客户端启动中");

    Socket client =new Socket("localhost",8888);

    //文件的拷贝
    InputStream is=new BufferedInputStream(new FileInputStream("src\\linux学习路线.png"));
    OutputStream os=new BufferedOutputStream(client.getOutputStream());

    byte[] data=new byte[1024*60];
    int len=-1;
    while((len=is.read(data))!=-1)
    {
        os.write(data,0,len);
    }
    os.flush();
    os.close();

    client.close();

}
}

サーバー:ストレージ・ファイル

public class tcp {

public static void main(String[]args) throws IOException
{
    System.out.println("服务器启动中...");
    ServerSocket server=new ServerSocket(8888);

    Socket client=server.accept();

    //文件的拷贝
    InputStream is=new BufferedInputStream(client.getInputStream());
    OutputStream os=new BufferedOutputStream(new FileOutputStream("D:/d/tu.jpg"));

    byte[] flush=new byte[1024*60];
    int len=-1;

    while((len=is.read(flush))!=-1)
    {
        os.write(flush,0,len);
    }

    is.close();
    os.close();

    client.close();

}
}

おすすめ

転載: blog.51cto.com/14437184/2433084