TCPServer / TCP客户端文件传输问题:0字节

时间:2016-05-08 16:17:15

标签: java tcp

我可以传输文件,但是当我想打开它们时,它说文件已损坏(因为它的0字节长)。 Ť

当我启动TCPServer时,它会等待客户端并接受它们,然后将文件发送给它们。客户端收回文件(但不是我假设的所有文件?)当我尝试使用10kb的picture.png时,它可以工作。除此之外,它没有。我也做了端口转发(否则客户端无法获取文件)

这是TCPSERVER:

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

class TCPServer {

    private final static String fileToSend = "C:/Users/Tim/Desktop/P&P/Background music for P&P/Rock.wav";

    public static void main(String args[]) {

        while (true) {
            ServerSocket welcomeSocket = null;
            Socket connectionSocket = null;
            BufferedOutputStream outToClient = null;

            try {
                welcomeSocket = new ServerSocket(3222);
                connectionSocket = welcomeSocket.accept();
                outToClient = new BufferedOutputStream(
                        connectionSocket.getOutputStream());
            } catch (IOException ex) {
                // Do exception handling
            }

            if (outToClient != null) {
                File myFile = new File(fileToSend);
                byte[] mybytearray = new byte[(int) myFile.length()];

                FileInputStream fis = null;

                try {
                    fis = new FileInputStream(myFile);
                } catch (FileNotFoundException ex) {
                    // Do exception handling
                }
                BufferedInputStream bis = new BufferedInputStream(fis);

                try {
                    bis.read(mybytearray, 0, mybytearray.length);
                    outToClient.write(mybytearray, 0, mybytearray.length);
                    outToClient.flush();
                    outToClient.close();
                    connectionSocket.close();

                    // File sent, exit the main method
                    return;
                } catch (IOException ex) {
                    // Do exception handling
                }
            }
        }
    }
}





HERE IS THE TCP CLIENT: 

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

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;

class TCPClient {

    private final static String serverIP = "123.123.123.123";
    private final static int serverPort = 3222;
    private final static String fileOutput = "C:/Users/Daniel/Desktop/check.wav";

    public static void main(String args[]) {

        while (true) {

            byte[] aByte = new byte[1024];
            int bytesRead;

            Socket clientSocket = null;
            InputStream is = null;

            try {
                clientSocket = new Socket(serverIP, serverPort);
                is = clientSocket.getInputStream();
            } catch (IOException ex) {
                // Do exception handling
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            if (is != null) {

                FileOutputStream fos = null;
                BufferedOutputStream bos = null;
                try {
                    fos = new FileOutputStream(fileOutput);
                    bos = new BufferedOutputStream(fos);
                    bytesRead = is.read(aByte, 0, aByte.length);

                    do {
                        baos.write(aByte);
                        bytesRead = is.read(aByte);
                    } while (bytesRead != -1);

                    bos.write(baos.toByteArray());
                    bos.flush();
                    bos.close();
                    clientSocket.close();
                } catch (IOException ex) {
                    // Do exception handling
                }
            }

            // Music is played here
            try {

                AudioInputStream input = AudioSystem
                        .getAudioInputStream(new File(fileOutput));
                SourceDataLine line = AudioSystem.getSourceDataLine(input
                        .getFormat());
                line.open(input.getFormat());
                line.start();
                byte[] buffer = new byte[1024];
                int count;
                while ((count = input.read(buffer, 0, 1024)) != -1) {
                    line.write(buffer, 0, count);
                }
                line.drain();
                line.stop();
                line.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

稍微重构了一下客户端代码:

  • 在使用BufferedOutputStream时不需要ByteArrayOutputStream
  • 使用bytesRead进行字节数组偏移

这对我有用:

if (is != null)
        {

            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            try
            {
                fos = new FileOutputStream(new File(fileOutput));
                bos = new BufferedOutputStream(fos);

                while ((bytesRead = is.read(aByte)) != -1)
                {
                    bos.write(aByte, 0, bytesRead);
                }
                bos.flush();
                bos.close();
                clientSocket.close();
            } catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
相关问题