将字符串和文件发送到 InputStream

Sending a string and file into InputStream

我正在尝试发送一个字符串,它是文件名,然后是文件本身到服务器。服务器正在接收该字符串并将其用于创建文件。但是,服务器实际上并没有向文件写入任何数据。

在将 Writer 的文件名(文件名被硬编码)添加到服务器和客户端之前,我让文件传输工作,但现在我无法让两者同时工作。

客户:

public class Client {

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        while (true) {
            String fileName = sc.nextLine();
            System.out.println(fileName);
            try {
                File file = new File(fileName);
                Socket socket = new Socket("localhost", 15000);
                OutputStream os = socket.getOutputStream();
                Writer w = new OutputStreamWriter(os, "UTF-8");
                w.write(fileName);
                w.close();
                os.flush();
                byte[] mybytearray = new byte[(int) file.length()];
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                bis.read(mybytearray, 0, mybytearray.length);
                os.write(mybytearray, 0, mybytearray.length);
                os.flush();
                os.close();
                socket.close();
            } catch (Exception e) { }
        }
        }
}

服务器:

public class Server extends Thread {

    public static final int PORT = 15000;

    @Override
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(PORT);
            while (true) {
                Socket sock = serverSocket.accept();
                readFile(sock);
            }
        } catch (Exception e) {
        }
    }

    private void readFile(Socket socket) throws Exception {
        InputStream ois = socket.getInputStream();
        Reader r = new InputStreamReader(ois, "UTF-8");
        String filename = "";
        int ch = r.read();
        while(ch != -1) {
            filename += (char) ch;
            System.out.println(filename);
            ch = r.read();
        }

        r.close();
        System.out.println(filename);

        FileOutputStream fos = new FileOutputStream(filename);

        byte[] bytearr = new byte[4096];
        System.out.println("Reading file...");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        while ((ois.read(bytearr)) > 0) {
            bos.write(bytearr);
        }
        bos.close();
        System.out.println("Writing file complete...");
    }

    public static void main(String[] args) {
        new Server().start();
    }
}

您还需要关闭文件输出流。有

bos.close;

添加

fos.close;

这是我的解决方案,此方法需要一些改进:

解释:

  • 位置0:文件名长度
  • 位置1到文件名的长度:filename as bytes.
  • 位置1+文件名长度til length:文件内容

基本上,我将所有信息一次发送到服务器(这是您需要弄清楚的一项改进)

另一个改进是分块发送文件,而不是一次发送所有文件。

客户端class:

public class Client {

public static void main(String[] args) throws Exception {
    Scanner sc = new Scanner(System.in);
    while (true) {
        String fileName = sc.nextLine();
        System.out.println(fileName);
        try {
            File file = new File(fileName);

            byte[] mybytearray = new byte[1 + fileName.getBytes().length + (int) file.length()];
            mybytearray[0] = (byte) fileName.length();

            System.arraycopy(fileName.getBytes(), 0, mybytearray, 1, fileName.getBytes().length);

            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            bis.read(mybytearray, fileName.getBytes().length + 1, (int) file.length());

            Socket socket = new Socket("localhost", 15000);
            OutputStream os = socket.getOutputStream();

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

            os.flush();
            os.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
}

服务器class:

public class Server extends Thread {

public static final int PORT = 15000;

public static void main(String[] args) {
    new Server().start();
}

@Override
public void run() {
    try {
        ServerSocket serverSocket = new ServerSocket(PORT);
        while (true) {
            Socket sock = serverSocket.accept();
            readFile(sock);
        }
    } catch (Exception e) {
    }
}

private void readFile(Socket socket) throws Exception {
    InputStream ois = socket.getInputStream();

    byte[] resultBuff = new byte[0];
    byte[] buff = new byte[1024];
    int k;
    while ((k = ois.read(buff, 0, buff.length)) > -1) {
        byte[] tbuff = new byte[resultBuff.length + k];
        System.arraycopy(resultBuff, 0, tbuff, 0, resultBuff.length);
        System.arraycopy(buff, 0, tbuff, resultBuff.length, k);
        resultBuff = tbuff;
    }

    byte lengthOfFileName = resultBuff[0];

    byte fileNameBytes[] = new byte[lengthOfFileName];
    System.arraycopy(resultBuff, 1, fileNameBytes, 0, lengthOfFileName);
    String filename = new String(fileNameBytes);

    FileOutputStream fos = new FileOutputStream(filename + System.currentTimeMillis());

    byte[] bytearr = new byte[resultBuff.length - (lengthOfFileName + 1)];
    System.out.println("Writing file...");

    System.arraycopy(resultBuff, lengthOfFileName + 1, bytearr, 0, bytearr.length);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bos.write(bytearr);

    bos.close();
    System.out.println("Writing file complete...");
}
}

希望对您有所帮助!

编码时间愉快!