繁体   English   中英

服务器套接字文件传输

[英]Server socket file transfer

我在java中使用了服务器套接字概念来传输图像和视频等文件。 但是当我在客户端收到时,我正在自定义文件名。 我可以获得文件的原始名称吗?

例如:

如果来自服务器端的用于传输的文件是“abc.txt”,我需要在客户端反映相同的名称(不单独传递名称)。

在服务器端:

public class FileServer {
  public static void main (String [] args ) throws Exception {
    // create socket
    ServerSocket servsock = new ServerSocket(13267);
    while (true) {
      System.out.println("Waiting...");

      Socket sock = servsock.accept();
      System.out.println("Accepted connection : " + sock);
      OutputStream os = sock.getOutputStream();
    new FileServer().send(os);
      sock.close();
      }
    }

  public void send(OutputStream os) throws Exception{
      // sendfile
      File myFile = new File ("C:\\User\\Documents\\abc.png");
      byte [] mybytearray  = new byte [(int)myFile.length()+1];
      FileInputStream fis = new FileInputStream(myFile);
      BufferedInputStream bis = new BufferedInputStream(fis);
      bis.read(mybytearray,0,mybytearray.length);
      System.out.println("Sending...");
      os.write(mybytearray,0,mybytearray.length);
      os.flush();
  }
}

在客户端:

    public class FileClient{
  public static void main (String [] args ) throws Exception {


    long start = System.currentTimeMillis();


    // localhost for testing
    Socket sock = new Socket("127.0.0.1",13267);
    System.out.println("Connecting...");
    InputStream is = sock.getInputStream();
    // receive file
    new FileClient().receiveFile(is);
       long end = System.currentTimeMillis();
    System.out.println(end-start);

    sock.close();
  }

  public void receiveFile(InputStream is) throws Exception{
      int filesize=6022386;
      int bytesRead;
      int current = 0;
      byte [] mybytearray  = new byte [filesize];

        FileOutputStream fos = new FileOutputStream("def");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = is.read(mybytearray,0,mybytearray.length);
        current = bytesRead;


        do {
           bytesRead =
              is.read(mybytearray, current, (mybytearray.length-current));
           if(bytesRead >= 0) current += bytesRead;
        } while(bytesRead > -1);

        bos.write(mybytearray, 0 , current);
        bos.flush();
        bos.close();
  }
}

要让接收者知道文件名,请:

a)它必须假设它知道名字,因为它要求它,

b)服务器首先将该名称作为流的一部分发送。

如果您发明了一种在不实际发送信息的情况下发送信息的方法,请告诉我们,我们可以成为亿万富翁。 我们可以称之为“计算机心灵感应”。

是的,只需在实际文件内容之前传输元数据(在您的情况下为myFile.getName() ),并使客户端和服务器读取并发出该元数据。 使用已建立的协议是个好主意,例如HTTP及其Content-Disposition标头。

有一种更简单的方法可以做到这一点。 只需将输入流包装在DataInputStream中,并使用.writeUTF(myFile.getName())等方法。 同样,您可以通过在DataInputStream上应用.readUTF来读取接收方的文件名。 这是一个示例代码:发件人:

FileInputStream fis = new FileInputStream(myFile);  
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);
    OutputStream os;
    try {
        os = socket.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeUTF(myFile.getName());  
.............////// catch exceptions

接收器:

InputStream in;
    try {
        bufferSize=socket.getReceiveBufferSize();
        in=socket.getInputStream();
        DataInputStream clientData = new DataInputStream(in);
        String fileName = clientData.readUTF();
        System.out.println(fileName);
................../////// catch exceptions

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM