0

我正在尝试构建一个发送文件的客户端/服务器应用程序,但我遇到了较大文件的问题。我正在使用 BufferedInputStream 从文件中读取信息,并使用 OutputStream 来写入套接字。我有一个从文件中读取 1 KB 然后发送它的循环,它在前 25 个循环中运行良好,然后由于套接字写入错误而崩溃。有任何想法吗?这是代码。

客户

import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TCPClient
{
public static void main(String[] args)
{
    /*Variables*/
    int serverPort = 8899;
    String ip = "localhost";
    File myFile = new File("GeneratedFile.txt"); //fileToBeSent.txt

    System.out.println(myFile.length());

    try
    {
        /*Connect to Server*/
        Socket sock = new Socket(ip, serverPort);
        System.out.println("Connection Made");

        /*Create Streams*/
        FileInputStream fis = new FileInputStream(myFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        OutputStream clientOutput = sock.getOutputStream();

        /*This is the old code for transfer*/
        /*Create Byte Array
        byte[] myByteArray = new byte[(int) myFile.length()];  //was 1024

        /*Send File
        bis.read(myByteArray, 0, 1024);
        clientOutput.write(myByteArray, 0, 1024);
        clientOutput.flush();
        */

        for(long i = 0; i <= myFile.length(); i += 1024)
        {
            byte[] myByteArray = new byte[1024];
            bis.read(myByteArray, 0, 1024);
            clientOutput.write(myByteArray, 0, 1024);
            clientOutput.flush();
            System.out.println("i is: " + i);
        }
        System.out.println("File Written");
        sock.close();
    } catch (IOException ex)
    {
        Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("You can't do that!");
    }
    System.out.println("Finished");
}
}

服务器

import java.io.*;
import java.net.*;

public class RequestHandler
{   
public void handleRequest()
{
    try
    {
        ServerSocket welcomeSocket = new ServerSocket(8899);

        while(true)
        {
            Socket socket = welcomeSocket.accept();
            System.out.println("Socket Open");

            /* Create byte array */
            byte[] mybytearray = new byte[1024 * 512];

            /* Create streams */
            InputStream is = socket.getInputStream();
            FileOutputStream fos = new FileOutputStream("newFile.txt",true);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            /*Write to file*/ 
            int bytesRead = is.read(mybytearray, 0, mybytearray.length);
            bos.write(mybytearray, 0, bytesRead);

            /*Close Stream and Socket*/
            bos.close();
            socket.close();
        }
    } catch (Exception e)
    {
        e.printStackTrace();
    }
}
public static void main(String args[])
{
    RequestHandler rq = new RequestHandler();
    rq.handleRequest();
    System.out.println("Here");
}
}
4

1 回答 1

1

您的复制技术不正确。这是在 Java 中复制流的方法:

byte[] buffer = new byte[8192]; // or whatever you like, but declare it outside the loop
int count;
while ((count = in.read(buffer)) > 0)
{
  out.write(buffer, 0, count);
}
out.flush();
// then in a finally block ...
out.close();
in.close();

你在两端都需要这个。您不能假设任何给定的读取都会填满缓冲区,因此您必须循环直到 EOS。请注意,您不会在循环内冲洗。

于 2012-11-30T00:24:46.837 回答