通过客户端服务器传输文件

时间:2019-02-20 11:26:07

标签: java html sockets server client

我正在尝试创建一个简单的协议,将客户端和服务器连接起来,并且客户端能够将文件(例如pdf文件)发送到服务器。我已经尝试了以下内容,但无法正常工作。它会显示错误消息:

Connecting...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at java.io.BufferedOutputStream.write(Unknown Source)
    at SimpleFileClient.main(SimpleFileClient.java:43)

客户代码

    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.Socket;

`     public class SimpleFileClient {

      public final static int SOCKET_PORT = 13267; // you may change this
      public final static String SERVER = "127.0.0.1"; // localhost
      public final static String
      FILE_TO_RECEIVED = "C:/hello/hello.pdf"; 
      // different name because i don't want to
      // overwrite the one used by server...

      public final static int FILE_SIZE = 6022386; 

      public static void main(String[] args) throws IOException {
        int bytesRead;
        int current = 0;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        Socket sock = null;
        try {
          sock = new Socket(SERVER, SOCKET_PORT);
          System.out.println("Connecting...");

          // receive file
          byte[] mybytearray = new byte[FILE_SIZE];
          InputStream is = sock.getInputStream();
          fos = new FileOutputStream(FILE_TO_RECEIVED);
          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();
          System.out.println("File " + FILE_TO_RECEIVED +
            " downloaded (" + current + " bytes read)");
        } finally {
          if (fos != null) fos.close();
          if (bos != null) bos.close();
          if (sock != null) sock.close();
        }
      }

    }

服务器端

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 SimpleFileServer {

  public final static int SOCKET_PORT = 13267; // you may change this
  public final static String FILE_TO_SEND = "C:/hello/hello.pdf"; // you may change this

  

          public static void main(String[] args) throws IOException {
          FileInputStream fis = null;
          BufferedInputStream bis = null;
          OutputStream os = null;
          ServerSocket servsock = null;
          Socket sock = null;
          try {
          servsock = new ServerSocket(SOCKET_PORT);
          while (true) {
          System.out.println("Waiting...");
          try {
          sock = servsock.accept();
          System.out.println("Accepted connection : " + sock);
          // send file
          File myFile = new File(FILE_TO_SEND);
          byte[] mybytearray = new byte[(int) myFile.length()];
          fis = new FileInputStream(myFile);
          bis = new BufferedInputStream(fis);
          bis.read(mybytearray, 0, mybytearray.length);
          os = sock.getOutputStream();
          System.out.println("Sending " + FILE_TO_SEND + "(" + 
          mybytearray.length + " bytes)");
          os.write(mybytearray, 0, mybytearray.length);
          os.flush();c
          System.out.println("Done.");
        } finally {
          if (bis != null) bis.close();
          if (os != null) os.close();
          if (sock != null) sock.close();
        }
      }
    } finally {
      if (servsock != null) servsock.close();
    }
  }
}

非常感谢您的帮助,因为我尝试了许多不同的选择。

0 个答案:

没有答案