2011-10-28 222 views

回答

1

客户端 - 服务器架构最适合实现你想要的。

在第一台计算机上启动FileServer并在第二台上运行FileClient

Sending files over socket.

4

你打开一个套接字连接和文件的字节复制到插座和插座的另一端读取的字节数。与通过套接字连接发送任意流相同的方式。这么说,有任何数量的方式从一台计算机的文件复制到另一个(使用Java),包括复制到共享文件系统,FTP传送文件,HTTP发布文件到Web服务器...

0

如果您主要关注的是将文件从一台计算机发送到另一台计算机。而不是在使用专有协议构建自己的文件服务器和客户端时,可以在服务器端嵌入ftp-server,在您自己的Java应用程序中嵌入客户端的ftp client

1
import java.io.BufferedInputStream; 



import java.io.File; 

import java.io.FileInputStream; 

import java.io.IOException; 

import java.io.OutputStream; 

import java.net.ServerSocket; 

import java.net.Socket; 

public class Main 
{ 

public static void main(String[] args) throws IOException { 

ServerSocket servsock = new ServerSocket(123456); 

File myFile = new File("s.pdf"); 

while (true) 
{ 

    Socket sock = servsock.accept(); 

    byte[] mybytearray = new byte[(int) myFile.length()]; 

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile)); 

    bis.read(mybytearray, 0, mybytearray.length); 

    OutputStream os = sock.getOutputStream(); 

    os.write(mybytearray, 0, mybytearray.length); 

    os.flush(); 

    sock.close(); 

    } 

} 

} 

The client module 


import java.io.BufferedOutputStream; 

import java.io.FileOutputStream; 

import java.io.InputStream; 

import java.net.Socket; 

public class Main { 

public static void main(String[] argv) throws Exception 
{ 

Socket sock = new Socket("127.0.0.1", 123456); 

byte[] mybytearray = new byte[1024]; 

InputStream is = sock.getInputStream(); 

FileOutputStream fos = new FileOutputStream("s.pdf"); 

    BufferedOutputStream bos = new BufferedOutputStream(fos); 

    int bytesRead = is.read(mybytearray, 0, mybytearray.length); 

bos.write(mybytearray, 0, bytesRead); 

    bos.close(); 

    sock.close(); 

    } 

}